PdfCreater.java:


Client.java:


Now we are all set; simply run Client.java and you can see the generated PDF file named as 'TestReport.pdf'.

Happy Learning!

Like This Article? Read More From DZone

java ,itext ,pdf in java ,open source ,tutorial
Opinions expressed by DZone contributors are their own.
Active9 months ago
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!
Philip MortonPhilip Morton
58.4k36 gold badges82 silver badges94 bronze badges

11 Answers

You can use XSL Formatting objects.Here are some good articles on how to do it:
Gray
99.3k15 gold badges240 silver badges308 bronze badges
Bogdan MaximBogdan Maxim
3,4913 gold badges19 silver badges34 bronze badges

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.

How To Create Pdf In Java

I'll provide a simple demo, I use maven build 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

C - The XSL-FO Template

D - Project Directory Structure

E - Pom file

F - Demo Code: PdfGenerationDemo.java

G - Sample Output: output.pdf

Create
Levent DiviliogluLevent Divilioglu
7,1283 gold badges38 silver badges89 bronze badges
hakanhakan
BIRT has a GUI for Eclipse that lets you define the PDF from XML, DB, CSV, etc etc.
bluish
15.2k19 gold badges96 silver badges155 bronze badges
jeff porterjeff porter
3,91811 gold badges53 silver badges105 bronze badges
You might want to look at the XSL-FO libraries that are out there that can do PDF creation as a transformation. I'll try to find a link.
Mitchel SellersMitchel Sellers
53.3k12 gold badges100 silver badges161 bronze badges
Try the xhtmlrenderer project. See the article 'Generating PDFs for Fun and Profit with Flying Saucer and iText'.
axelclkaxelclk
You can apply XSL-Fo to your XML and transform it with Java transformer:
Tim Cooper
126k32 gold badges259 silver badges233 bronze badges
YaroslavYaroslav
Use JasperReports. You can either pull the data from Database or XML.You can export to many formats : pdf, excel, html, etc...
raffimdraffimd
Coming in late, you can create a static PDF with Adobe's designer with editable fields, then create a matching XDP XML document.
Dean JDean J
22.4k13 gold badges56 silver badges90 bronze badges

Create Pdf In Java Spring Boot

There are two ways to do this.
Based on your requirements, you can use either of the approaches mentioned above.
Shriram Kalpathy MohanShriram Kalpathy Mohan
XML, CSS, XHTML, etc. consist in an 'alive ecosystem' of open standards, while XSL-FO is an isolated standard.
... Historically XSL-FO and XSLT was created as twins brothers, but only XSLT remains an 'alive standard', XSL-FO concentrates a lot of DNA in proprietary (Adobe) standards... now is obsolete.
Strictly speaking, XSL-FO is part of an 'abandoned way' that will not evolve, it ignores CSS, the 'new way' to express layout in the 'alive ecosystem'.

It is not a Java problem

See this answer about the use of CSS-page with XML or XHTML.

Not the answer you're looking for? Browse other questions tagged javaxmlpdfxsltitext or ask your own question.