xmlgraphics-fop-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Derrick Shoemake <dshoem...@protocolinc.com>
Subject Re: Servlet Example with inputStream
Date Tue, 08 Feb 2005 23:34:04 GMT
This isn't really exactly what you're looking for, but it should help, 
the XSL file is a URL, you just pass in the XML Document and it returns 
a PDF.  I create the XML in another method, but this is a start.

public void renderPDF(Document xmlDoc, HttpServletResponse response)
     throws ServletException

             // Lets get the XSL file in stream form
             // First get the Serlet Context so a true url can be
             // determined
             ServletContext sc = this.getServletContext();
             URL xslURL = sc.getResource(xslReportDirectory + 
             Source xslSource = new StreamSource(new 

             ByteArrayOutputStream out = new ByteArrayOutputStream();

             // We're returning a pdf so set it as the content type

             //String output = null;

             // Create a Document by creating the DocumentBuilderFactory
             // and the Document Builder
             DocumentBuilderFactory dbf = 
             DocumentBuilder db = dbf.newDocumentBuilder();
             Document docResult = db.newDocument();

             // Create a DOMResult object for the result of the
             // transformation, and set its node to be the result 
             DOMResult drResult = new DOMResult(docResult);
             Document foOutDoc = null;

             // Create the Transformer that takes the XML DOM and 
converts it
             // into XML in memory
             TransformerFactory transfact = 
             Transformer transformer = 

             // We need a DOMSource to pass into the transformer
             DOMSource dsSource = new DOMSource(xmlDoc);

             // if the transformer failed in it's creation don't create 
the new
             // FO Document
             if (transformer != null)
                 transformer.transform(dsSource, drResult);
                 foOutDoc = (Document) drResult.getNode();
                 //System.err.println("output: " + output);
             } else
                 System.err.println("No Transformer");

             //Serialize DOM So we can print it out if we need to
             OutputFormat format = new OutputFormat(foOutDoc);

             // Now convert the Serialized DOM to a XMLSerializer object
             // which has a toString() method
             StringWriter stringOut = new StringWriter();
             XMLSerializer serial = new XMLSerializer(stringOut, format);

             // debug statement to print out the FOXSL file
             //System.err.println("foOutDoc: ");

             // if the transformer didn't create fo Document
             // throw and exception
             if (foOutDoc == null)
                 throw new ServletException("foOutDoc is null");

             // Create the FOP Driver and set the Logger(required), then
             // set the Renderer to the output we want which is a pdf
             // then set the Output stream that is the 
             // we setup earlier in this method
             Driver driver = new Driver();

             // This is where the FOP creates the PDF

             // Now convert it to a byte array and
             // set it as the output stream to the servlet
             byte[] content = out.toByteArray();
         } catch (Exception ex)
             throw new ServletException(ex);

On Feb 8, 2005, at 5:19 PM, Frank Schaare wrote:

> Hi,
> i´m looking for an Servlet (or struts action) example, the builds the 
> pdf DIRECTLY from the response stream.
> I studied the servlet example and googled some time, but all examples 
> deal with files as input.
> Has anyone ever realised this task or knows such an example ?
> thx...
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: fop-user-unsubscribe@xml.apache.org
> For additional commands, e-mail: fop-user-help@xml.apache.org

To unsubscribe, e-mail: fop-user-unsubscribe@xml.apache.org
For additional commands, e-mail: fop-user-help@xml.apache.org

View raw message