in Coding, Java

Generating PDFs with Java, Flying Saucer and Thymeleaf

Generating PDFs can by a tough job. If you ever worked with templating engines like Jasper you probably know what I mean. On the other side, HTML makes it extremely simple to describe documents. So why not use HTML to generate PDFs?

The following post shows a simple approach to generate PDFs with Thymeleaf templates and Flying Saucer in plain Java.

PDF rendering pipeline

Our PDF rendering pipeline consists of two basic steps:

  1. Using Thymeleaf, we populate XHTML templates with data in order to receive plain XHTML document.
  2. We save this XHTML document as a PDF using Flying Saucer.

Note that both steps are independent. You could easily use any other templating engine (just as Apache FreeMarker) or even plain XHTML. However, I would prefer Thymeleaf as it is a mature templating engine especially if you work in a Spring environment.

Project Setup

To start rendering, we need three different dependencies which we can find in Maven:

The project structure should look like this:

Thymeleaf Template

The Thymeleaf template which we are using is pretty simple. It just contains a sentence and variable to fill with data. Note that this is a very simple example. Thymeleaf is much more powerful and provides concepts like for-loops, if-statements and Java method calls out of templates. You will find a lot of tutorials on the internet.

To render this template with Thymeleaf, we can use the following code. It will substitute the variable ${name} with actual data. In the end, we will receive plain HTML which we could open in any browser.

PDF Generation

Generating the actual PDF is quite easy now. We basically put the HTML string to the PDF renderer which returns the byte stream of the generated PDF. The basic layout fits for our example, but we could also include some CSS in order to style the PDF.

Advanced Topics

A lot of important topics are not covered in the very basic example above. However, I want to give some external links and hints on a couple of points:

  • You can use CSS in order to generate page numbers with Flying Saucer as described here.
  • Thymeleaf templates can be divided into fragments. Those fragments can be included into other templates and provided reusable building blocks for documents (such as a header or footer). Read more here.


Best regards,

Write a Comment