How to Create PDF From XML in Java Using Apache FOP

This post shows how to create PDF from XML in Java using Apache FOP.

Apache FOP

Apache™ FOP (Formatting Objects Processor) is a print formatter driven by XSL formatting objects (XSL-FO) and an output independent formatter. It is a Java application that reads a formatting object (FO) tree and renders the resulting pages to a specified output. Output formats currently supported include PDF, PS, PCL, AFP, XML (area tree representation), Print, AWT and PNG, and to a lesser extent, RTF and TXT. The primary output target is PDF.

Maven dependency

To get the Apache FOP related jars in your application’s classpath you need to add following dependency in the pom.xml file.

Note that java.xml package in xml-apis was conflicting with the java.xml package in JDK 10 that’s why it is excluded.

Alternatively you can download Apache FOP from here – https://xmlgraphics.apache.org/fop/download.html and copy the required jars yourself.

How to create PDF from XML using Apache FOP

  1. First you need to transform your XML file to XSL-FO using XSLT.
  2. Then using FOP you transform the XSL-FO to PDF.

XML to PDF in Java using FOP

Creating PDF from XML in Java using Apache FOP – Hello World example

First let’s create a hello world PDF using Apache FOP and Java which just shows the passed name in the PDF.

XML used for this example is a very simple one having just one element name.

name.xml

You also need a style sheet which is used to transform the XML to XSL-FO.
style.xsl

Few things to note in this XSL are-

  1. Namespace for fo is added in the XSL – xmlns:fo=”http://www.w3.org/1999/XSL/Format”
  2. Here matching is done using “/” root itself.
  3. The value of the name element is extracted from the XML, there are also font and text color settings.

Java program that do the transformation from XML to XSL-FO and from XSL-FO to PDF.

Created PDF
XML to PDF using Java

Creating PDF from XML using Apache FOP – PDF table example

Here is another example where data from the XML is shown as a table in PDF. In the XSL you need to specify the matching element and the program loops through that element to create one row in the table for each repetition of the matched element.
organization.xml

In the PDF we need one row per branch element and show data for the child elements with in that row.

organization.xsl

In the XSL you can see initial matching is for the root element “organization” then again there is template matching for element “branch”. For <branch> element value of the child elements are extracted and displayed with in the table cells in PDF.
Java program that do the transformation from XML to XSL-FO and from XSL-FO to PDF.

created PDF
XML to PDF using Java and Apache FOP

Creating PDF from XML using Apache FOP – In web application

If you want to render the generated PDF on the browser in your web application then you can use something similar to as given below-

That’s all for the topic How to Create PDF From XML in Java Using Apache FOP. If something is missing or you have something to share about the topic please write a comment.


You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.