You are viewing a plain text version of this content. The canonical link for it is here.
Posted to fop-users@xmlgraphics.apache.org by JH...@rev.state.ne.us on 2003/04/17 22:39:15 UTC

How can I get from bean to PDF with FOP and Castor

I am trying to produce dynamic PDF documents from a JSP/Servlet (Struts)
application.

This is what I am trying to do:

1) from a servlet, generate a report bean.

2) use Castor to marshal the bean to an in-memory XML document (maybe a DOM
document?).

3) use FOP to merge the in-memory XML document with a pre-written XSL file
to produce a PDF doc.

4) send the PDF doc to browser client.

I am only starting to become familiar with the FOP and Castor APIs and I am
not sure how to accomplish steps 2 and 3.

I have successfully used Castor to marshal a bean to an XML text file.

I have also been able to produce PDF with with static XML and XSL files
from a servlet.

I would appreciate any pointers on accomplishing steps 2 and 3.

Has anybody done this?
Do you have any code samples?
Is there a better way to do this?

Thanks, Jack




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


Re: How can I get from bean to PDF with FOP and Castor

Posted by Jeremias Maerki <de...@greenmail.ch>.
Have a look at the examples on the embedding page:
http://xml.apache.org/fop/embedding.html#examples

The corresponding Java sources can be found in examples/embedding.

This shows you the basic patterns of working with FOP and JAXP. It even
shows how to use a Java object as data source similar to your bean. The
Java object there gets transformed to SAX events by hand. Now the only
thing you have to figure out is how to plug Castor's output into JAXP.

Then, you should have a look at the examples servlet in examples/servlet.
It is very easy to modify the servlet there to adjust it to your needs.

If you can you should try to use SAX instead of a DOM to transfer the
contents of the bean through XSLT to FOP. This will result in a lower
memory footprint and a faster system.

I'm not so familiar with Castor but I think you can do something along
these lines. I haven't tested it but I think this is how it could work:

        Object myobj = null; //your bean here
        
        //Construct driver
        Driver driver = new Driver();
        
        //Setup logger
        Logger logger = new ConsoleLogger(ConsoleLogger.LEVEL_INFO);
        driver.enableLogging(logger);
        driver.initialize();

        //Setup Renderer (output format)        
        driver.setRenderer(Driver.RENDER_PDF);
        
        //Setup output
        ByteArrayOutputStream out = new ByteArrayOutputStream(16384);
        driver.setOutputStream(out);

        //Setup XSLT
        SAXTransformerFactory factory = (SAXTransformerFactory)SAXTransformerFactory.newInstance();
        File xslt = new File("D:/FOP/temp/test.xsl");
        TransformerHandler thandler = factory.newTransformerHandler(new StreamSource(xslt));
    
        //Resulting SAX events (the generated FO) must be piped through to FOP
        thandler.setResult(new SAXResult(driver.getContentHandler()));

        //Start Castor, XSLT transformation and FOP processing
        Marshaller.marshal(myobj, thandler);

        //for example in a servlet:
        response.setContentType("application/pdf");
        final byte[] content = out.toByteArray();
        response.setContentLength(content.length);
        response.getOutputStream().write(content);
        response.getOutputStream().flush();

I hope this is not too much for you as this involves a SAX pipeline but
if the above works it's probably one of the best solutions to your
problem.

On 17.04.2003 22:39:15 JHardy wrote:
> I am trying to produce dynamic PDF documents from a JSP/Servlet (Struts)
> application.
> 
> This is what I am trying to do:
> 
> 1) from a servlet, generate a report bean.
> 
> 2) use Castor to marshal the bean to an in-memory XML document (maybe a DOM
> document?).
> 
> 3) use FOP to merge the in-memory XML document with a pre-written XSL file
> to produce a PDF doc.
> 
> 4) send the PDF doc to browser client.
> 
> I am only starting to become familiar with the FOP and Castor APIs and I am
> not sure how to accomplish steps 2 and 3.
> 
> I have successfully used Castor to marshal a bean to an XML text file.
> 
> I have also been able to produce PDF with with static XML and XSL files
> from a servlet.
> 
> I would appreciate any pointers on accomplishing steps 2 and 3.
> 
> Has anybody done this?
> Do you have any code samples?
> Is there a better way to do this?


Jeremias Maerki


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