in Coding, Java

Generating PDFs with Java, Flying Saucer and Thymeleaf (Part 1)

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.

Example on GitHub: https://github.com/tuhrig/Flying_Saucer_PDF_Generation

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.

More

You find a running example on GitHub:

https://github.com/tuhrig/Flying_Saucer_PDF_Generation

Part 2

Go to part 2 for a deeper dive into styling with CSS and images.

Best regards,
Thomas

Write a Comment

Comment

    • Hey Manel, sorry for the late answer. An image is actually pretty easy. You can just add a normal HTML image tag to your template:

      However, it’s a little bit tricky to find the right place where the image must be located. My project setup looks like this:

      So the image logo.jpg is located directly in the root folder of the project.

      Best regards,
      Thomas

    • Hey Ketan. You can set the page size via CSS. To do so, you need to refer to a CSS file in your HTML. In the example below, a CSS file named style.css is used.

      In that CSS file, you can set the page properties. You can also change the font and so on.

      Here’s a link to a Stackoverflow question about the page properties:

      https://stackoverflow.com/questions/17529658/how-to-generate-a-pdf-in-land-scape-orientation-from-a-html-string-using-itext-2

      Hope that helps.

      Best regards,
      Thomas

Webmentions

  • Get your documents written perfectly January 29, 2018

    I’ve created a GitHub project with an example. The example generates a styled PDF including an image. It also shows a header and a footer can be implemented.

    https://github.com/tuhrig/Flying_Saucer_PDF_Generation