Here's an example that I wish I had when I started.:
I'm creating the InputSources from files, you can create them from anything.
In practice, I create them from DOM Documents.

PdfServlet.java:
----------------------------------------------------------------
package org.sbyrne.pdfservlet;

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

// FOP
import org.apache.fop.apps.FOPException;
import org.xml.sax.InputSource;

// Logger
import org.apache.log4j.Logger;
import org.apache.log4j.Level;

import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.sax.SAXSource;

/**
 * A Servlet to return a PDF if the user is authenticated.
 *
 * @author Stephen Byrne
 * @version $Revision: 1.3 $
 */
public class PdfServlet implements Servlet {

    /* Location of xml files */
    static final String FILELOC="../../cygwin/home/sbyrne/xsl/";

    /* XSL file name */
    static final String XSL="data.xsl";

    /* XML file name */
    static final String XML="data.xml";

    /* Log4J Logger */
    static final Logger log = Logger.getLogger(PdfServlet.class);

    /* Servlet Config */
    private ServletConfig config;

    /**
     * Servlet initiation.
     * Configure and set log debug level.
     *
     * @param config ServletConfig
     *
     * @throws ServletException
     */
    public void init (ServletConfig config) throws ServletException {

        this.config = config;

        log.setLevel( Level.DEBUG );

    } // init()

    /**
     * Servlet destroy.
     */
    public void destroy() {
    } // destroy()

    /**
     * Return servlet configuration.
     *
     * @return ServletConfig Servlet Configuration
     */
    public ServletConfig getServletConfig() {
        return config;
    } // getServletConfig()

    /**
     * Return servlet Information
     *
     * @return String Servlet Information
     */
    public String getServletInfo() {
        return "A Simple Servlet";
    } // getServletInfo()

    /**
     * Service ServletRequest.
     * Check authorization and return PDF (or error)
     *
     * @param request ServletRequest
     * @param response ServletReponse
     *
     * @throws ServletException
     * @throws IOException
     */
    public void service (ServletRequest request, ServletResponse response )
        throws ServletException, IOException  {

        try {
            InputSource xmlSource = new InputSource(
                new FileInputStream( FILELOC + XML ) );

            InputSource xslSource = new InputSource(
                new FileInputStream( FILELOC + XSL ) );

            Pdf pdf = new Pdf( xmlSource, xslSource );

            response.setContentType( pdf.getContentType() );
            response.setContentLength( pdf.getContentLength() );
            response.getOutputStream().write( pdf.toByteArray() );

        }
        catch ( FOPException ex ) {
            log.error( "FOP processing error: " + ex.getMessage() );
        }
        catch ( FileNotFoundException ex ) {
            log.error( "Included XSL file not found: " + ex.getMessage() );
        }
        catch ( IOException ex ) {
            log.error( "I/O error in XSLT or FOP: " + ex.getMessage() );
        }

    } // service()

} // PdfServlet
---------------------------------
Pdf.java:
-----------------------------------
package org.sbyrne.pdfservlet;

import java.io.ByteArrayOutputStream;

// FOP
import org.xml.sax.InputSource;
import org.apache.fop.apps.Driver;
import org.apache.fop.apps.TraxInputHandler;
import org.apache.fop.apps.FOPException;

// Logger
import org.apache.avalon.framework.logger.Log4JLogger;
import org.apache.log4j.Logger;
import org.apache.log4j.Level;

/**
 * A PDF, created from XML and XSLT sources with FOP
 *
 * @author Stephen Byrne
 * @version $Revision: 1.1.1.1 $
 */
public class Pdf {

    /* ByteArrayOutputStream to store PDF in */
    private ByteArrayOutputStream pdfStream;

    /* Log4J Logger */
    final static Logger log = Logger.getLogger(Pdf.class);

    /* FOP Driver */
    private Driver driver;

    /**
     * Pdf Constructor
     * Create a Pdf and access it and its properties
     *
     * @param xmlSource Source XML
     * @param xslSource Source XSL
     *
     * @param log Logger
     */
    public Pdf( InputSource xmlSource, InputSource xslSource )
        throws FOPException {

        pdfStream = new ByteArrayOutputStream();

        driver = new Driver();
        log.setLevel(Level.DEBUG);
        driver.setLogger( new Log4JLogger(log) );
        driver.setRenderer( Driver.RENDER_PDF );

        driver.setOutputStream( pdfStream );

        log.debug("Create TraxInputHandler");
        TraxInputHandler input = new TraxInputHandler(
                xmlSource, xslSource );

        log.debug("Run Driver. . .");
        input.run(driver);
        log.debug("Done.");

    } // Pdf

    /**
     * Return the content type
     *
     * @return Content Type
     */
    public String getContentType() {
        return "application/pdf";
    } // getContentType()

    /**
     * Return the content length
     *
     * @return Content Length
     */
    public int getContentLength() {
        return pdfStream.size();
    } // getContentLength()

    /**
     * Return the pdf as a byte array
     *
     * @return PDF as byte array
     */
    public byte[] toByteArray() {
        return pdfStream.toByteArray();
    } // toByteArray

} // Pdf
-------------------------------------
Jar's required:
avalon-framework-cvs-20020806.jar
batik.jar
fop.jar
log4j-1.2.9.jar
xalan.jar (2.6.0)

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to