Converting XML to PDF

0
206
4 min read

XML is the most suitable format for data exchange, but not for data presentation. Adobe’s PDF and Microsoft Excel’s spreadsheet are the commonly used formats for data presentation. If you receive an XML file containing data that needs to be included in a PDF or Excel spreadsheet file, you need to convert the XML file to the relevant format. Some of the commonly used XML-to-PDF conversion tools/APIs are discussed in the following table:

Tool/API

Description

iText

iText is a Java library for generating a PDF document. iText may be downloaded from http://www.lowagie.com/iText/.

Stylus Studio XML Publisher

XML Publisher is a report designer, which supports many data sources, including XML, to generate PDF reports.

Stylus Studio XML Editor

XML Editor supports XML conversion to PDF.

Apache FOP

The Apache project provides an open source FO processor called Apache FOP to render an XSL-FO document as a PDF document. We will discuss the Apache FOP processor in this chapter.

XMLMill for Java

XMLMill may be used to generate PDF documents from XML data combined with XSL and XSLT. XMLMill may be downloaded from http://www.xmlmill.com/.

RenderX XEP

RenderX provides an XSL-FO processor called XEP that may be used to generate a PDF document.

 

We can convert an XML file data to a PDF document using any of these tools/APIs in JDeveloper 11g.

Here, we will use Apache FOP API.

The XSL specification consists of two components: a language for transforming XML documents (XSLT), and XML syntax for specifying formatting objects (XSL-FO). Using XSL-FO, the layout, fonts, and representations of the data may be formatted. Apache FOP (Formatting Objects Processor) is a print formatter for converting XSL formatting objects (XSL-FO) to an output format such as PDF, PCL, PS, SVG, XML, Print, AWT, MIF, or TXT. In this  article, we will convert an XML document to PDF using XSL-FO and the FOP processor in Oracle JDeveloper 11g.The procedure to create a PDF document from an XML file using the Apache FOP processor in JDeveloper is as follows:

  1. Create an XML document.
  2. Create an XSL stylesheet.
  3. Convert the XML document to an XSL-FO document.
  4. Convert the XSL-FO document to a PDF file.

Setting the environment

We need to download the FOP JAR file fop-0.20.5-bin.zip (or a later version) from http://archive.apache.org/dist/xmlgraphics/fop/binaries/ and extract the ZIP file to a directory. To develop an XML-to-PDF conversion application, we need to create an application (ApacheFOP, for example) and a project (ApacheFOP for example) in JDeveloper. In the project add an XML document, catalog.xml, with File | New. In the New Gallery window select Categories | General | XML and Items | XML Document. Click on OK. In the Create XML File window specify a File Name, catalog.xml, and click on OK. A catalog.xml file gets added to the ApacheFOP project. Copy the following catalog.xml listing to catalog.xml:

<?xml version="1.0" encoding="UTF-8"?>
<catalog title="Oracle Magazine" publisher="Oracle Publishing">
<journal edition="September-October 2008">
<article>
<title>Share 2.0</title>
<author>Alan Joch</author>
</article>
<article>
<title>Restrictions Apply</title>
<author>Alan Joch</author>
</article>
</journal>
<journal edition="March-April 2008">
<article>
<title>Oracle Database 11g Redux</title>
<author>Tom Kyte</author>
</article>
<article>
<title>Declarative Data Filtering</title>
<author>Steve Muench</author>
</article>
</journal>
</catalog>

We also need to add an XSL stylesheet to convert the XML document to an XSL-FO document. Create an XSL stylesheet with File | New. In the New Gallery window, select Categories | General | XML and Items | XSL Stylesheet. Click on OK. In the Create XSL File window specify a File Name (catalog.xsl) and click on OK. A catalog.xsl file gets added to the ApacheFOP project. To convert the XML document to an XSL-FO document and subsequently create a PDF file from the XSL-FO file, we need a Java application. Add a Java class,  XMLToPDF.java, with File | New. In the New Gallery window select Categories | General and Items | Java Class. Click on OK. In the Create Java Class window specify a class Name (XMLToPDF for example) and click on OK. A Java class gets added to the ApacheFOP project. The directory structure of the FOP application is shown in the following illustration:

Converting XML to PDF

Next, add the FOP JAR files to the project. Select the project node (ApacheFOP node) and then Tools | Project Properties. In the Project Properties window, select Libraries and Classpath. Add the Oracle XML Parser v2 library with the  Add Library button. The JAR files required to develop an FOP application are listed in the following table:

JAR File

Description

<FOP>/fop-0.20.5/build/fop.jar

Apache FOP API

<FOP>/fop-0.20.5/lib/batik.jar

Graphics classes

<FOP>/fop-0.20.5/lib/ avalon-framework-cvs-20020806.jar

Logger classes

<FOP>/fop-0.20.5/lib/ xercesImpl-2.2.1.jar

The DOMParser and the SAXParser classes

LEAVE A REPLY

Please enter your comment!
Please enter your name here