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 "Varley, Roger" <Ro...@brake.co.uk> on 2002/10/30 15:26:07 UTC

Howto embed FOP processing wth XML and XSL?

Hi

I'm just starting to use FOP. I want to embed the processing within a
servlet using XML and XSLT to generate the FO file and send the PDF output
back to the client. All the examples that I can find, including the one in
the FAQ assume that the XML and XSL files are physically located as external
files (as does the constructor for XSLTInputHandler which require two
java.io.File objects). In my case, the XSL file is a physical file, but the
XML file is dynamically generated within servlet and is held as a
SAXInputSource object. How do I construct the FO file and the PDF output
from this, without having the additional overhead of writing the XML data to
a physical file first? 

Regards
Roger   

Re: Howto embed FOP processing wth XML and XSL?

Posted by Oleg Tkachenko <ol...@multiconn.com>.
Varley, Roger wrote:

> I'm just starting to use FOP. I want to embed the processing within a
> servlet using XML and XSLT to generate the FO file and send the PDF output
> back to the client. All the examples that I can find, including the one in
> the FAQ assume that the XML and XSL files are physically located as external
> files (as does the constructor for XSLTInputHandler which require two
> java.io.File objects). In my case, the XSL file is a physical file, but the
> XML file is dynamically generated within servlet and is held as a
> SAXInputSource object. How do I construct the FO file and the PDF output
> from this, without having the additional overhead of writing the XML data to
> a physical file first? 

That's jaxp basics, get some good jaxp/trax tutorial and look for examples, 
e.g. TraxExamples.java from saxon distribution is extremely useful.
If you want to avoid overhead, use sax. Here is small example you can adopt, 
just replace StreamSource object by your SAXSource.

	TransformerFactory tfactory = TransformerFactory.newInstance();
         try {
             Driver driver = new Driver();
             driver.setOutputStream(new FileOutputStream("d:/test.pdf"));
             driver.setRenderer(Driver.RENDER_PDF);

             // Does this factory support SAX features?
             if (tfactory.getFeature(SAXSource.FEATURE)) {

                 // If so, we can safely cast.
                 SAXTransformerFactory stfactory =
                         ((SAXTransformerFactory) tfactory);

                 Transformer transformer =
                         stfactory.newTransformer(new 
StreamSource("d:/fop/fop-0.20.4/docs/xml2pdf.xsl"));

                 // Set the result handling to be fop itself
                 Result result = new SAXResult(driver.getContentHandler());

                 //xml source for transformation
                 StreamSource xml = new 
StreamSource("d:/fop/fop-0.20.4/docs/xslfoRef.xml");

                 //Runs transformation
                 transformer.transform(xml, result);
             }
         } catch (Exception e) {
             e.printStackTrace();
         }

-- 
Oleg Tkachenko
eXperanto team
Multiconn Technologies, Israel