Hi,

I�m trying to use this example to generate a pdf, but I�m not having
success with it.
How can I parse parameters from a jsp to this servlet, so this way I can
generate my pdf, not using the jsp but a servlet.

Thanks in advance.

Best Regards,

Roberto Godoy

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfWriter;

public class OutSimplePDF extends HttpServlet {
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
    }
    
    public void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
        
    /*
     *  This outputs a given message as PDF to illustrate the workaround
     *  for the MS Internet Explorer bug discussed at:
     *  http://www.lowagie.com/iText/faq.html#msie
     *
     */
        
        try {
            
            // take the message from the URL or create default message
            String msg = (String)request.getParameter("msg");
            if (msg == null || msg.trim().length() <= 0)
                msg = "[ specify a message in the 'msg' argument on the
URL ]";
            
            // create simple doc and write to a ByteArrayOutputStream
            Document document = new Document();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            PdfWriter.getInstance(document, baos);
            document.open();
            document.add(new Paragraph(msg));
            document.close();
            
            // write ByteArrayOutputStream to the ServletOutputStream
            response.setContentType("application/pdf");
            response.setContentLength(baos.size());
            ServletOutputStream out = response.getOutputStream();
            baos.writeTo(out);
            out.flush();
            
            // this is a workaround for a bug in MSIE
            // see http://www.lowagie.com/iText/faq.html#msie
            
        }
        catch (Exception e2) {
            System.out.println("Error in
"+getClass().getName()+"\n"+e2);
        }
    }
    
    public void destroy() {
    }
}

Roberto Godoy

-----Mensagem original-----
De: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] Em nome de
Christian Lauer
Enviada em: domingo, 1 de fevereiro de 2004 13:40
Para: iText Mailing List
Assunto: Re: [iText-questions] JSP problem

Hi Roberto,

It's hard to exactly control the output that's made from a JSP. For
example, if you have some white spaces in your code outside the <% %>
tags, the JSP will implicitely send them back to the client as well as
the output you have made through iText, this could lead into corrupted
PDF output because of leading / trailing white spaces or other content.
Why don't you use a servlet to perform this output?

Best Regards,
Christian

On Sun, 2004-02-01 at 12:26, Roberto Godoy wrote:
> Hi,
> 
>  
> 
> I create a jsp to print some reports for me and I cant make this
> work??? Could someone help me???
> 
> Acesso.jsp is just to open a connection to the database.
> 
> Thanks in advance.
> 
>  
> 
> Roberto Godoy
> 
>  
> 
> <%@
> 
> pageimport="java.io.*,
> 
>                    com.lowagie.text.*,
> 
>                    com.lowagie.text.pdf.*,
> 
>                    java.sql.*"
> 
> errorPage="error.jsp"
> 
> %><%@ include file="../../acesso/Acesso.jsp" %><%
> 
> response.setContentType( "application/pdf" );
> 
> Document document = new Document(PageSize.A4.rotate(), 50, 50, 50,
> 50);
> 
> ByteArrayOutputStreambuffer = new ByteArrayOutputStream();
> 
> PdfWriter.getInstance( document, buffer );
> 
> Image jpeg = Image.getInstance("/bla/images/bla4.jpg");
> 
> HeaderFooterfooter = new HeaderFooter(new Phrase("Brasil -  P�gina "),
> true);
> 
> HeaderFooterheader = new HeaderFooter(new Phrase("Relat�rio de
> Clientes"), false);
> 
> footer.setAlignment(Element.ALIGN_CENTER);
> 
> header.setAlignment(Element.ALIGN_CENTER);
> 
> jpeg.setAlignment(Image.LEFT);
> 
> jpeg.setAbsolutePosition(50, 540);
> 
> document.setFooter(footer);
> 
> document.setHeader(header);
> 
> document.open();
> 
>             document.add(jpeg);
> 
>             Table datatable = new Table(12);
> 
>             datatable.setPadding(3);
> 
>             datatable.setWidth(100);
> 
>             datatable.setAutoFillEmptyCells(true);
> 
>            int headerwidths[] = {15, 30, 30, 15, 10, 15, 15, 15, 15,
> 15, 15, 15};
> 
>             datatable.setWidths(headerwidths);
> 
>             datatable.setBorderWidth(1);
> 
>             datatable.addCell("Cod. do Cliente");
> 
>             datatable.addCell("Nome do Cliente");
> 
>             datatable.addCell("Endereco");
> 
>             datatable.addCell("Numero");
> 
>             datatable.addCell("Compl");
> 
>             datatable.addCell("Bairro");
> 
>             datatable.addCell("Cidade");
> 
>             datatable.addCell("Estado");
> 
>             datatable.addCell("Pais");
> 
>             datatable.addCell("CEP");
> 
>             datatable.addCell("CNPJ");
> 
>             datatable.addCell("IE");
> 
> ResultSet rec = stm.executeQuery("SELECT * from cliente");
> 
> while (rec.next()) {
> 
> String codcliente = rec.getString("cod_cliente");
> 
>             datatable.setBorderWidth(1);
> 
>             datatable.addCell(codcliente);
> 
>             datatable.addCell(rec.getString("nome_empresa"));
> 
>             datatable.addCell(rec.getString("endereco"));
> 
>             datatable.addCell(rec.getString("numero"));
> 
>             datatable.addCell(rec.getString("complemento"));
> 
>             datatable.addCell(rec.getString("bairro"));
> 
>             datatable.addCell(rec.getString("cidade"));
> 
>             datatable.addCell(rec.getString("estado"));
> 
>             datatable.addCell(rec.getString("pais"));
> 
>             datatable.addCell(rec.getString("cep"));
> 
>             datatable.addCell(rec.getString("cnpj"));
> 
>             datatable.addCell(rec.getString("ie"));
> 
> }
> 
> document.add(datatable);
> 
> rec.close();
> 
> document.close();
> 
> DataOutputoutput = new DataOutputStream(response.getOutputStream() );
> 
> byte[] bytes = buffer.toByteArray();
> 
> response.setContentLength(bytes.length);
> 
> for( int i = 0; i < bytes.length; i++ ) { output.writeByte( bytes[i]
> );}
> 
> %>
> 
>  
> 
>  
> 
> 
>  
-- 
Christian Lauer <[EMAIL PROTECTED]>



-------------------------------------------------------
The SF.Net email is sponsored by EclipseCon 2004
Premiere Conference on Open Tools Development and Integration
See the breadth of Eclipse activity. February 3-5 in Anaheim, CA.
http://www.eclipsecon.org/osdn
_______________________________________________
iText-questions mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/itext-questions

-- 
Esta mensagem foi verificada pelo E-mail Protegido Terra.
Scan engine: VirusScan / Atualizado em 29/01/2004 / Vers�o: 1.4.1
Proteja o seu e-mail Terra: http://www.emailprotegido.terra.com.br/




-------------------------------------------------------
SF.Net is sponsored by: Speed Start Your Linux Apps Now.
Build and deploy apps & Web services for Linux with
a free DVD software kit from IBM. Click Now!
http://ads.osdn.com/?ad_id56&alloc_id438&op=click
_______________________________________________
iText-questions mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Reply via email to