Java HTML to PDF Conversion using Itext PDF

Itext PDF library lets you convert HTML to PDF documents. In this post we will take a look at simple example for HTML to PDF conversion.

itext pdf dependencies

To add support for itext-pdf dependencies, you need to add the following kernel and html 2 pdf dependencies.

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>kernel</artifactId>
    <version>7.1.14</version>
</dependency>
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>html2pdf</artifactId>
    <version>3.0.3</version>
</dependency>

The kernel dependency provides the core module for PDF support. The html2pdf gives utility methods that helps us to convert HTML file or content into PDF.

Using html2pdf

To convert HTML to PDF, the library provides plenty of utility methods which we can use in our JAVA code.

HTML file to PDF

To convert HTML file to PDF, you need to use the following java method.

HtmlConverter.convertToPdf(new File("./simple-input.html"),new File("simple-output.pdf"));

HTML String to PDF

If you have the HTML content as a String, then you may want to use the following approach.

OutputStream fileOutputStream = new FileOutputStream("string-output.pdf");
HtmlConverter.convertToPdf("<h1>Hello String Content!</h1>", fileOutputStream);

This way of conversion would be ideal along with a template engine like thymeleaf.

InputStream to PDF

If you have the HTML content as an InputStream, you convert the stream in to PDF as shown below.

HtmlConverter.convertToPdf(htmlStringStream, fileOutputStream);

The above set of methods are most common ones. But there are variations to these methods which you can find from the doumentation.

methods to convert HTML to PDF
all supported methods to convert HTML to PDF

Customizing HTML to PDF conversion

The generated PDF will mostly resemble its HTML counterpart. However, there is no guarantee that the results will always be good. For the following reasons, you should provide a ConverterProperties.

  • Dealing with resources with relative urls
  • Handling Custom Fonts referred in the HTML
  • To change the The resulting PDF size and format

For instance, take a look at this HTML content.

<!doctype html>
<html lang="en">
<head>
    <title>Hello there</title>
    <link href="/css/main.css" rel="stylesheet">
</head>
<body >
<h2 style="font-family: 'Pacifico',serif; font-size: 56px">Hello there....!</h2>
<img src="html-logo.png" alt="Hello World">
</body>
</html>

On converting this HTML file, the CSS for font is not applied properly.

html2pdf not able to load CSS
main.css is not applied due to relative path

In the above case, the image was loaded because it was found in the same path of the html file. However, the CSS file is referred from server root. As the library cannot detect where this css is from, it is our responsibility to point from where the relative paths are calculated.

For instance, the server base URL is https://somedomain.com. In this case, you can configure the converter to load every base path as shown below.

ConverterProperties converterProperties = new ConverterProperties();
converterProperties.setBaseUri("https://springhow.com/");
HtmlConverter.convertToPdf(new File("./input.html"), new File("output.pdf"), converterProperties);

If you want to use a specific font in your HTML content, you could use the fontProvider option in the converterProperties.

ConverterProperties converterProperties = new ConverterProperties();

FontProvider fontProvider  = new FontProvider();
fontProvider.addFont("/path/to/my-font.ttf");
fontProvider.addStandardPdfFonts();
fontProvider.addSystemFonts(); //for fallback
converterProperties.setFontProvider(fontProvider);

HtmlConverter.convertToPdf(htmlString, outputStream, converterProperties);

Conclusion

So, we learned how to use itext html2pdf to convert HTML content into rich pdf files. If you find this topic interesting, you may also want to read the following titles.

Leave a Comment