How to convert HTML to PDF in Java ?

There is so common requirement to generate PDF for some online content or reporting data. Also there is number of ways doing it. For example you could deliver it by leveraging headless browser (like Chrome or an older, already not supported but verified by time – PhantomJs) or use some dedicated back-end library.

Differences between the latter are most likely in flexibility. Headless browser exports to PDF or an image exactly the content which is rendered by browser engine. And there is quite limited means to alter content inside when it comes to splitting content into pages. There some methods which you can use to achieve page splitting related features by using CSS printer directives. Although headless browser approach is less flexible comparing to advantages of full featured backend library. There are some robust but costly libraries on the market like Aspose or IText.

In this post I’d like to provide very tiny sample to generate PDF out from XHTML in Java. I’ll use couple of free libraries:

  • Flying Saucer – for rendering XHTML and CSS content .
  • OpenPDF – for creating PDF document out of rendered content .

OpenPDF actually is the fork of IText and is under LGPL license. It is an older version and not capable for supporting the latest features, but still does the job quite well and is sufficient for even advanced requirements.

Lets create a maven project and declare dependencies in pom.xml of two mentioned libraries:

    <!-- // -->

    <!-- // -->


Pay attention to versions used, because Flying Saucer and OpenPDF needs to be compatible.

And then create just as simple as it can be main method which does the stuff:

<pre>public static void main(String args[]) throws Exception {
    final String baseDir = System.getProperty("user.dir");
    System.out.println("About to convert html to pdf");
    File input = new File(baseDir + "\\src\\main\\resources\\source-data.html");
    File output = new File(baseDir + "\\output.pdf");
    ITextRenderer renderer = new ITextRenderer();
    OutputStream outputStream = new FileOutputStream(output);

One important thing to keep in mind, that Flying Saucer is able to process only XHTML, not HTML. So be aware of that by sticking to strict html syntax.

Tiny, but complete Maven project can be downloaded here.


About Danas Tarnauskas

1 thought on “How to convert HTML to PDF in Java ?

Leave a Reply

Your email address will not be published.