Generating PDF Report using Apache FOP, MS word (for template) and JAVA

Generating PDF Report using Apache FOP, MS word (for template) and JAVA
We often encounter the requirement for generating the custom PDF report. There are many open source tools like iText, Jasper etc. I was looking for some approach that ease the formatting task for the report.
Will follow below steps to generate the report:


For further understanding, we will take a task to generate the sample report. We will generate user bank account statement PDF. As this is the sample PDF, we will include limited details. The logo is
We will follow the above mentioned steps:
  1. Prepare the report template using MS word.
As a part of the bank statement report we will need to include below information in report.
Name
Address
Account Number
Bank Name
Month
Transaction List-> Desc, Credit/Debit, Amount


Based on the above mentioned parameter we generated the below PDF template in MS word. Generating this template in MS word and formatting it does not take long time. If you observe, we have mentioned the parameter with #VALUE. This will help us replace the #value with correct XPATH.


  1. Export or Save MS Word Template as XML-FO
In order to export the MS word as XML-FO we need to download and run “OfficeWordWordMLtoXSL-FO.exe”.
Run the exe, this will generate the folder with .XSL files


Open the word template,
Click on File,
Save As,
Select word 2003 XML Document,
Check Apply Transform,
Select “Word2FO.xsl” file under the folder “OfficeWordWordMLtoXSL-FO”
Click Save.
This will generate the StatementTemplate.xml file in the corresponding folder.


Next step is to convert the word XML to XSLT,
  • Create a new XSLT template file.
<?xml version="1.0" encoding="utf-8"?><xsl:stylesheet version="1.0" xmlns:lc="http://www.abc.in/bank" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format"> <xsl:output method="xml" indent="yes" /> <xsl:template match="/"> </xsl:template></xsl:stylesheet>
  • Copy the entire “<fo:root>” element from the generated statementTemplate.xml file and paste it inside “<xsl:template>” element.
  • Save the file as StatementTemplate.xsl
  1. Generate the XML for the date in report template
Now we will need to generate the XML for our report data. This is user specific based on user XSD. For this example, I will generate a simple XML.


  1. Now replace the #VALUE in XSL with the XPATH based on the XML.
We will use simple XSL syntax to replace the value at runtime.
Example:
Replace : #NAME with
<xsl:value-of select="bankstatment/name" />


The row of the table in FO is represented by “fo:table-row”. Here as we need to dynamically print the rows based on the number transaction of the user, we will apply XSL for-loop and repeat “fo:table-row”
Example: “<xsl:for-each select="bankstatment/transactionlist/transaction">”


  1. Generate the PDF using Apache FOP
First, you need to download Apace FOP libraries.
  • Create a new JAVA project.
  • Copy the XML and XSL file in to the java project.
  • Give all the JARs under FOP bin as a dependency to the project.
D:\ApacheFOB\fop-1.1-bin\fop-1.1\lib
  • Run the JAVA program mention below by replacing the correct file paths.
  • When you run for first time, the background color of the table in generate FO will be black, you need to manually change the color to white.
Look for the property “background-color="black"” and replace it with background-color="white"
If all run properly, PDF report will be generated as per the format mentioned in the WORD template.
Please find the generated PDF:

Please Note: you will need to manually edit the xsl:FO in order to adjust some alignment in the generated PDF.
Links:
Apache FOP:
MS word to FO Transform:

You can download the sample Application from below link:

Popular posts from this blog

JAVA embedding in Oracle SOA 12c

Passing/Receiving HTTP header in SOA BPEL

Integrating Weblogic with IBM MQ over JMS