At the moment, I'm creating an XML file in Java and displaying it in a JSP page by transforming it with XSL/XSLT. Now I need to take that XML file and display the same information in a PDF. Is there a way I can do this by using some kind of XSL file? I've seen the iText Java-PDF library, but I can't find any way to use it with XML and a stylesheet. Any assistance would be much appreciated. Thanks in advance!
asked Oct 17, 2008 at 15:09
Philip Morton Philip Morton
132k 38 38 gold badges 89 89 silver badges 97 97 bronze badges
Now there is iText® XMLWorker, default implementation is HTML/CSS to pdf
Commented May 28, 2011 at 8:23
It is better to consider using Apache-FOP framework. I've added an answer down below, using apache-fop.
Commented Dec 8, 2016 at 21:52
11 Answers 11
A - Explanation
You should use Apache FOP framework to generate pdf output. Simply you provide data in xml format and render the page with an xsl-fo file and specify the parameters like margin, page layout in this xsl-fo file.
I'll provide a simple demo, I use mavenbuild tool to gather the needed jar files. Please notify that at the end of the page, there is an svg graphics embedded in pdf. I also want to demonstrate that you can embed svg graphics inside pdf.
B - Sample XML input data
User Bill DataThursday December 9 2016 00:04:29John Doe34239123AD32924817.84Michael Doe54823942KFDSCW32234.50Jane Brown66742ABDD324KKD869.36
C - The XSL-FO Template
Bill Id: , Date: XXX COMPANYPage of MONTHLY BILL REPORTFull NamePostal CodeNational IDPayment