O c�digo abaixo gera o arquivo pdf com base num arquivo xml montado
em tempo de requisi��o.
/*
* geradorPdf.java
*
* Created on March 31, 2002, 4:05 PM
*/
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.fop.apps.Driver;
import org.apache.fop.apps.Version;
/**
*
* @author Administrator
* @version
*/
public class geradorPdf extends HttpServlet {
/** Initializes the servlet.
*/
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
/** Destroys the servlet.
*/
public void destroy() {
}
/** Processes requests for both HTTP and POSTmethods.
* @param request servlet request
* @param response servlet response
*/
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, java.io.IOException {
ByteArrayOutputStream arquivoFo = new ByteArrayOutputStream();
ByteArrayOutputStream arquivoPdf = new ByteArrayOutputStream
();
ServletOutputStream out = response.getOutputStream();
try{
// Settings do mime da p�gina.
response.setContentType("application/pdf");
response.setHeader ("Pragma", "no-cache");
response.setHeader ("Cache-Control", "no-cache");
response.setDateHeader ("Expires",0);
// Encontrar o arquivo xsl para convers�o.
String arquivoXSL = null;
String arquivoXML = null;
try{
arquivoXSL = request.getParameter("xsl");
arquivoXML = request.getParameter("xml");
}catch(Exception e){
try{out.close();}catch(Exception ex){}
throw new ServletException("Par�metros xsl/xml
incorretos");
}
// Realiza a transforma��o de xsl/xml para fo.
StreamSource xsltSource = new StreamSource(new File
(arquivoXSL));
TransformerFactory transFact =
TransformerFactory.newInstance();
Transformer transf = transFact.newTransformer(xsltSource);
StreamSource dataSource = new StreamSource(new File
(arquivoXML));
// Transforma as tables em XLS-FO usando XLST
transf.transform(dataSource, new StreamResult(arquivoFo));
// Abaste�e uma nova stream de entrada com dados da
transforma��o. Aqui poder�amos economizar no contentFo !!!!
byte contentFo[] = arquivoFo.toByteArray();
ByteArrayInputStream arquivoFoEntrada = new
ByteArrayInputStream(contentFo);
// Realizar a convers�o de fo para pdf.
Driver driver = new Driver(new InputSource
(arquivoFoEntrada),
arquivoPdf);
driver.setRenderer(Driver.RENDER_PDF);
driver.run();
// Ajusta o tamanho do mime para browser.
byte contentPdf[] = arquivoPdf.toByteArray();
response.setContentLength(contentPdf.length);
// Escreva para stream de sa�da e encerra a opera��o.
out.write(contentPdf);
out.flush();
out.close();
}catch(Exception ex){
try{
out.close();
// Envia erro para uma p�gina que consegue trat�-lo.
request.setAttribute
("javax.servlet.jsp.jspException", ex);
RequestDispatcher rd = getServletContext
().getRequestDispatcher("Erro.jsp");
rd.forward(request, response);
}catch(Exception e){
}
}
}
/** Handles the HTTP GET method.
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, java.io.IOException {
processRequest(request, response);
}
/** Handles the HTTP POST method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, java.io.IOException {
processRequest(request, response);
}
/** Returns a short description of the servlet.
*/
public String getServletInfo() {
return "Short description";
}
}
> ---------- Mensagem original -----------
>
> De : "Marcelo Alves" <[EMAIL PROTECTED]>
> Para : <[EMAIL PROTECTED]>
> Cc :
> Data : Mon, 1 Apr 2002 14:36:59 -0300
> Assunto : Re: [java-list] Arquivo pdf
>
> Oi,
>
> Vc cria o PDF usando o lowagie, muda o content type do servlet
para
> application/pdf (eu acho q � isso) e ao inves de usar um
FileOutputStream
> para saida do PDF, vc usa o response.getOutputStream (se nao estou
> enganado...)... Daqui a pouco mando um exemplo pra lista...
>
> t+
>
> .: marcelo :.
>
>
> ----- Original Message -----
> From: "Viviane" <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>
> Sent: Monday, April 01, 2002 10:45 AM
> Subject: Re: [java-list] Arquivo pdf
>
>
> > Marcelo,
> >
> > A minha d�vida � a seguinte: � poss�vel a partir de uma p�gina na
internet
> o
> > usu�rio escolher uma determinada op��o e o arquivo pdf ser gerado
na
> m�quina
> > dele, ou seja, no cliente ???
> >
> > Grata,
> > Viviane
> > ----- Original Message -----
> > From: "Marcelo Alves" <[EMAIL PROTECTED]>
> > To: <[EMAIL PROTECTED]>
> > Cc: <[EMAIL PROTECTED]>
> > Sent: Monday, April 01, 2002 9:28 AM
> > Subject: Re: [java-list] Arquivo pdf
> >
> >
> > > www.lowagie.com/iText
> > >
> > >
> > >
> > > Trabalhei bastante com isso, posso at� tirar umas d�vidas.
> > >
> > > .: xfer :.
> > >
> > > ----- Original Message -----
> > > From: "Jose Ferreira de Souza Filho"
<[EMAIL PROTECTED]>
> > > To: <[EMAIL PROTECTED]>
> > > Sent: Monday, April 01, 2002 8:15 AM
> > > Subject: [java-list] Arquivo pdf
> > >
> > >
> > > >
> > > > Ol� lista,
> > > >
> > > > algu�m sabe como gerar um arquivo em pdf em Java? Existe
alguma
> > > > classe que cotenha m�todos que fa�am isso?
> > > >
> > > > Desde j� agrade�o a aten��o de todos.
> > > >
> > > > ------------------------------ LISTA
> > SOUJAVA ----------------------------
> > > > http://www.soujava.org.br - Sociedade de Usu�rios Java da
Sucesu-SP
> > > > d�vidas mais comuns: http://www.soujava.org.br/faq.htm
> > > > regras da lista: http://www.soujava.org.br/regras.htm
> > > > historico: http://www.mail-archive.com/java-list%
40soujava.org.br
> > > > para sair da lista: envie email para
> > [EMAIL PROTECTED]
> > >
> >
> > ------------------------------------------------------------------
-------
> > > >
> > >
> > >
> > > ------------------------------ LISTA
> SOUJAVA ----------------------------
> > > http://www.soujava.org.br - Sociedade de Usu�rios Java da
Sucesu-SP
> > > d�vidas mais comuns: http://www.soujava.org.br/faq.htm
> > > regras da lista: http://www.soujava.org.br/regras.htm
> > > historico: http://www.mail-archive.com/java-list%
40soujava.org.br
> > > para sair da lista: envie email para
> [EMAIL PROTECTED]
> >
> > ------------------------------------------------------------------
-------
> >
> >
> > ------------------------------ LISTA SOUJAVA ---------------------
-------
> > http://www.soujava.org.br - Sociedade de Usu�rios Java da
Sucesu-SP
> > d�vidas mais comuns: http://www.soujava.org.br/faq.htm
> > regras da lista: http://www.soujava.org.br/regras.htm
> > historico: http://www.mail-archive.com/java-list%40soujava.org.br
> > para sair da lista: envie email para java-list-
[EMAIL PROTECTED]
> > ------------------------------------------------------------------
-------
> >
>
>
> ------------------------------ LISTA SOUJAVA -----------------------
-----
> http://www.soujava.org.br - Sociedade de Usu�rios Java da Sucesu-
SP
> d�vidas mais comuns: http://www.soujava.org.br/faq.htm
> regras da lista: http://www.soujava.org.br/regras.htm
> historico: http://www.mail-archive.com/java-list%40soujava.org.br
> para sair da lista: envie email para java-list-
[EMAIL PROTECTED]
> --------------------------------------------------------------------
-----
>
>
>
------------------------------ LISTA SOUJAVA ----------------------------
http://www.soujava.org.br - Sociedade de Usu�rios Java da Sucesu-SP
d�vidas mais comuns: http://www.soujava.org.br/faq.htm
regras da lista: http://www.soujava.org.br/regras.htm
historico: http://www.mail-archive.com/java-list%40soujava.org.br
para sair da lista: envie email para [EMAIL PROTECTED]
-------------------------------------------------------------------------