Generate PDF documents with Charts using Java EE and ApachePDFBox

Last Updated:  June 15, 2020 | Published: February 24, 2019

Generating documents for e.g. invoices or reports is a central use case for enterprise applications. As a Java developer, you have a wide range of possible libraries to manipulate and create Word, Excel or PDF documents. To help you choose the right library, I'll demonstrate an example to generate PDF documents with Java EE. This requires Apache PDFBox and for creating charts the XChart library. The example is based on Java 11, Java EE 8 and deployed to Open Liberty.

Java EE project setup

The pom.xml looks like the following:

I've chosen Apache PDFBox (GitHub) as the PDF library as the library has active maintainer, is opensource, easy-to-learn and good enough for basic use cases. The charting library XChart (GitHub) is a light-weight Java library for plotting data with an intuitive developer API, is providing really good example charts and capable of plotting every important chart type (XYChart, Bar-, Pie-, Histogram-, Dial, Radar, Stick Chart …).

For this simple showcase, the additional dependencies are packed within the .war but they could and should be part of the application server to have a thin war with quick deployment cycles.

Creating a JAX-RS endpoint to download the PDF

The JAX-RS configuration for this application is quite simple:

For downloading the generated PDF document I've added the following JAX-RS endpoint:

To notify the client about the content type of the response the JAX-RS annotation @Produces(MediaType.APPLICATION_OCTET_STREAM) is required. In addition, I've added the Content-Disposition header so that regular browsers will directly download the incoming file with the given filename simplePdf.pdf.

The injected EJB PdfGenerator is responsible for generating the PDF document as a byte array:

PDDocument is the central class for creating new PDF documents with Apache PDFBox. In this example, I'm using a try-with-resources block to create a new document and close it afterward. The PDDocument object can contain several PDPage objects which represent a physical PDF page. To display the chart later on in landscape mode, we have to rotate the page for 90 degrees. Furthermore,  writing content to the PDPage  requires opening a PDPageContentStream .

Adding a chart to the PDF document

For including the chart as an image to the PDF page, I'm creating a PDImageXObject from a BufferedImage, transforming it (to also rotate the image) and drawing it to the page at position x=0 and y=0 (starting bottom left corner).

The chart is created as the following:

I'm using a simple XYChart with randomly generated numbers and creating it as high and wide as the underlying PDF page is:

The resulting PDF document looks like this: simplePdf.pdf

There are quite a lot more possibilities with Apache PDFBox and XChart as the plotting engine, but this example should give you a quick start to get in touch with these libraries.

You can find the whole codebase to generate a PDF document with Java EE including instructions on how to run it on your machine on GitHub.

Have fun generating PDF documents with Java EE,

Phil

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Join Our Mailing List To Get 3x Free Cheat Sheets

Free Java Cheat Sheets
>