Browse DevX
Sign up for e-mail newsletters from DevX


Generate PDF Files Dynamically Using XSL-FO  : Page 3

Learn how to generate Adobe Portable Document Format (PDF) files from XML documents using XSL Formatting Objects (XSL-FO) and a Java Servlet.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Step 2: Generate a PDF File from a .fo File:
The second step is to create the PDF file from the .fo file-using FOP. Import the following FOP packages:

   import org.apache.fop.messaging.MessageHandler;
   import org.apache.fop.apps.*;
First get the FOP version with the getVersion() method.

   String version = org.apache.fop.apps.Version.getVersion();
You need the version string because it's one of the parameters you must pass to the render() method later on in the process. The sample code uses the driver.setRenderer() method.

   // org.xml.sax.helpers.XMLReaderFactory 
   // relies on a System Property.
Before calling the createXMLReader() method set the System property.

Next, instantiate a Driver object.

   org.apache.fop.apps.Driver driver = new org.apache.fop.apps.Driver();
After instantiating the Driver object, you call its methods to specify a Renderer to use and an OutputStream for the rendered result. You must supply the Renderer with a set of element mappings that specify how each element should be rendered to the output. You can do this by supplying either a preexisting ElementMappings object or by supplying the name of the class, in which case the Driver will instantiate the class. The advantage of using a class is that it enables you to specify the Renderer and ElementMapping(s) at runtime.

The org.apache.fop package contains a PDF Renderer implementation and some standard element mappings.

      ("org.apache.fop.render.pdf.PDFRenderer", version);
After initializing the Driver, call the buildFOTree() method. If you're using DOM, you invoke the method using buildFOTree(Document). If you're using the Simple API for XML (SAX), you invoke the method using the syntax buildFOTree(Parser, InputSource).

Figure 1. Generated Document: Here is a peek at the output PDF file from the sample code running in the free Adobe Acrobat Reader application.
You aren't limited to opening the files directly from disk with the PDF reader. You can send the generated PDF file directly to most browsers, which use the Adobe reader as a helper file to open the PDF document. The generated documents are fully compatible with the PDF reader. Users can read, search, print and save the files. If you have the Adobe PDF reader installed on your computer, you can see an output example. If not, click the thumbnail image below to see a full-size image of the output.

The procedure shown in this article lets you generate PDF files from any type of XML document. You can add custom element mappings to improve your layout capabilities.

Vikas Pandya is a Sun-certified Java sofware engineer. He currently works as an XSL/XML, Java developer for Bank of America in Charlotte NC.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date