Apache Fop Pdf Generation Example Java
In this post we'll see how to create PDF from XML in Java using Apache FOP.
What is 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.
FOP uses the standard XSL-FO file format as input, lays the content out into pages, then renders it to the requested output.
Read more about it here- https://xmlgraphics.apache.org/fop/
How to get Apache FOP
Get the FOP download from here.
https://xmlgraphics.apache.org/fop/download.html
I have used fop-2.0 for this example code.
Needed jars (found in the lib and build directory in the fop download)-
- Commons-io
- Commons-logging
- Xml-apis
- Xmlgraphics-commons
- Fop
- Batik-all
- Avalon-framework
If you want to add maven dependency for Apache FOP then it is as following.
<dependency> <groupId>org.apache.xmlgraphics</groupId> <artifactId>fop</artifactId> <version>2.7</version> <exclusions> <exclusion> <groupId>xml-apis</groupId> <artifactId>xml-apis</artifactId> </exclusion> </exclusions> </dependency>
Here we have an exclusion for xml-apis because of the javax.xml package conflict, this package is present in Java too and that's what will be used.
Steps for creating a PDF from XML in Java using Apache FOP
To produce a PDF file from a XML file, first step is that we need an XSLT stylesheet that converts the XML to XSL-FO. Created XSL-FO file is also an XML file which contains formatted objects.
The second step will be done by FOP when it reads the generated XSL-FO document and formats it to a PDF document.
Creating PDF using FOP Java example
XML used for creating PDF is as follows.
<?xml version="1.0"?> <employees> <companyname>ABC Inc.</companyname> <employee> <id>101</id> <name>Ram</name> <designation>Manager</designation> </employee> <employee> <id>102</id> <name>Prabhu</name> <designation>Executive</designation> </employee> <employee> <id>103</id> <name>John</name> <designation>Executive</designation> </employee> </employees>
Stylesheet Used
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" exclude-result-prefixes="fo"> <xsl:template match="employees"> <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"> <fo:layout-master-set> <fo:simple-page-master master-name="simpleA4" page-height="29.7cm" page-width="21cm" margin-top="2cm" margin-bottom="2cm" margin-left="2cm" margin-right="2cm"> <fo:region-body/> </fo:simple-page-master> </fo:layout-master-set> <fo:page-sequence master-reference="simpleA4"> <fo:flow flow-name="xsl-region-body"> <fo:block font-size="16pt" font-weight="bold" space-after="5mm">Company Name: <xsl:value-of select="companyname"/> </fo:block> <fo:block font-size="10pt"> <fo:table table-layout="fixed" width="100%" border-collapse="separate"> <fo:table-column column-width="4cm"/> <fo:table-column column-width="4cm"/> <fo:table-column column-width="5cm"/> <fo:table-body> <xsl:apply-templates select="employee"/> </fo:table-body> </fo:table> </fo:block> </fo:flow> </fo:page-sequence> </fo:root> </xsl:template> <xsl:template match="employee"> <fo:table-row> <xsl:if test="designation = 'Manager'"> <xsl:attribute name="font-weight">bold</xsl:attribute> </xsl:if> <fo:table-cell> <fo:block> <xsl:value-of select="id"/> </fo:block> </fo:table-cell> <fo:table-cell> <fo:block> <xsl:value-of select="name"/> </fo:block> </fo:table-cell> <fo:table-cell> <fo:block> <xsl:value-of select="designation"/> </fo:block> </fo:table-cell> </fo:table-row> </xsl:template> </xsl:stylesheet>
If you see the XSL, first I am looking for the employees element to get the company Name and also there some formatting is done like how many columns are needed and what should be the width. Then I am looking for the employee element and printing the values, also some logic is there to print the field values in bold if the designation is manager.
Copying the output of PDF I got, that will make it easy to understand the XSL.
Java code
import java.io.File; import java.io.IOException; import java.io.OutputStream; import javax.xml.transform.Result; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.sax.SAXResult; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; import org.apache.fop.apps.FOPException; import org.apache.fop.apps.FOUserAgent; import org.apache.fop.apps.Fop; import org.apache.fop.apps.FopFactory; import org.apache.fop.apps.MimeConstants; public class FOPPdfDemo { public static void main(String[] args) { FOPPdfDemo fOPPdfDemo = new FOPPdfDemo(); try { fOPPdfDemo.convertToFO(); } catch (FOPException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TransformerException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * Method that will convert the given XML to PDF * @throws IOException * @throws FOPException * @throws TransformerException */ public void convertToPDF() throws IOException, FOPException, TransformerException { // the XSL FO file File xsltFile = new File("D:\\NETJS\\xml\\template.xsl"); // the XML file which provides the input StreamSource xmlSource = new StreamSource(new File("D:\\NETJS\\xml\\Employees.xml")); // create an instance of fop factory FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI()); // a user agent is needed for transformation FOUserAgent foUserAgent = fopFactory.newFOUserAgent(); // Setup output OutputStream out; out = new java.io.FileOutputStream("D:\\NETJS\\xml\\employee.pdf"); try { // Construct fop with desired output format Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out); // Setup XSLT TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(new StreamSource(xsltFile)); // Resulting SAX events (the generated FO) must be piped through to FOP Result res = new SAXResult(fop.getDefaultHandler()); // Start XSLT transformation and FOP processing // That's where the XML is first transformed to XSL-FO and then // PDF is created transformer.transform(xmlSource, res); } finally { out.close(); } } /** * This method will convert the given XML to XSL-FO * @throws IOException * @throws FOPException * @throws TransformerException */ public void convertToFO() throws IOException, FOPException, TransformerException { // the XSL FO file File xsltFile = new File("D:\\NETJS\\xml\\template.xsl"); /*TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(new StreamSource("F:\\Temp\\template.xsl"));*/ // the XML file which provides the input StreamSource xmlSource = new StreamSource(new File("D:\\NETJS\\xml\\Employees.xml")); // a user agent is needed for transformation /*FOUserAgent foUserAgent = fopFactory.newFOUserAgent();*/ // Setup output OutputStream out; out = new java.io.FileOutputStream("D:\\NETJS\\xml\\temp.fo"); try { // Setup XSLT TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(new StreamSource(xsltFile)); // Resulting SAX events (the generated FO) must be piped through to FOP //Result res = new SAXResult(fop.getDefaultHandler()); Result res = new StreamResult(out); //Start XSLT transformation and FOP processing transformer.transform(xmlSource, res); // Start XSLT transformation and FOP processing // That's where the XML is first transformed to XSL-FO and then // PDF is created transformer.transform(xmlSource, res); } finally { out.close(); } } }
In the code there are two methods convertToPDF() and convertToFO(), convertToPDF() method is used to convert XML to PDF. convertToFO() method will create the XSL-FO from the XML using the XSLT. If you want to see the created FO which in turn is used to create PDF please call this method.
Create PDF in web application using Apache FOP
In case of web application if you want to provide PDF as a download, here is a Servlet method as reference-
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try{ FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI()); //Setup a buffer to obtain the content length ByteArrayOutputStream out = new ByteArrayOutputStream(); Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(new StreamSource(PATH_TO_XSL)); //Make sure the XSL transformation's result is piped through to FOP Result res = new SAXResult(fop.getDefaultHandler()); //Setup input Source src = new StreamSource(new File("./resources/Employees.xml")); //Start the transformation and rendering process transformer.transform(src, res); //Prepare response response.setContentType("application/pdf"); response.setContentLength(out.size()); //Send content to Browser response.getOutputStream().write(out.toByteArray()); response.getOutputStream().flush(); }catch(Exception e){ e.printStackTrace(); } }
Recommendations for learning (Udemy Courses)
- Java Programming Masterclass Course
- Java In-Depth: Become a Complete Java Engineer!
- Spring Framework Master Class Course
- Complete Python Bootcamp Course
- Python for Data Science and Machine Learning
That's all for this topic How to Create PDF From XML in Java Using Apache FOP. If you have any doubt or any suggestions to make please drop a comment. Thanks!
Related Topics
You may also like-
Source: https://www.netjstech.com/2015/07/how-to-create-pdf-from-xml-using-apache-fop.html
0 Response to "Apache Fop Pdf Generation Example Java"
Post a Comment