Java | Converting HTML to PDF using iText

html to PDF Creating a PDF file from HTML can be done using iText Java library. iText has an add-on that enables converting HTML to PDF document. This post shows how to use iText to convert HTML to PDF.

“When using iText PDF in a closed source environment, you will need to purchase an iText PDF commercial license.”

Environment, Tools & Libraries used in this post

  • Maven (build tool)
  • iText 7.1.9
  • pdfHTML iText PDF Add-On 2.1.6

Depenedenceis

  • We need to add iText core library and pdfHTML add-on to our project dependencies.

pom.xml

<dependencies>
    <!-- iText Core -->
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itext7-core</artifactId>
        <version>7.1.9</version>
        <type>pom</type>
    </dependency>

    <!-- iText pdfHTML add-on -->
    <dependency>
	    <groupId>com.itextpdf</groupId>
	    <artifactId>html2pdf</artifactId>
	    <version>2.1.6</version>
	</dependency>
</dependencies>

HtmlConverter Class

  • HtmlConverter class is the main class to convert HTML to PDF.
  • HtmlConverter class has three main methods with different inputs and return types:
    • convertToDocument(): returns Document instance.
    • convertToElements(): returns a list of iText IElement instances.
    • convertToPdf(): this method converts HTML to PDF.

convertToPdf() Method

  • We will use convertToPdf() method from HtmlConverter class to convert HTML to PDF.
  • convertToPdf() has different variations that takes HTML as String, File or InputStream & writes the PDF content to File, OutputStream or existing PdfDocument.
  • In this post we will use the following variations of the convertToPdf()
    • convertToPdf(String html, OutputStream pdfStream)
    • convertToPdf(InputStream htmlStream, OutputStream pdfStream)

Converting HTML String to PDF

  • The simplest example is converting a String of HTML to PDF.
package com.hmkcode;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import com.itextpdf.html2pdf.HtmlConverter;

public class App 
{
	public static final String HTML = "<h1>Hello</h1>"
			+ "<p>This was created using iText</p>"
			+ "<a href='hmkcode.com'>hmkcode.com</a>";
	
	
    public static void main( String[] args ) throws FileNotFoundException, IOException  
    {
    	HtmlConverter.convertToPdf(HTML, new FileOutputStream("string-to-pdf.pdf"));
    	
        System.out.println( "PDF Created!" );
    }
}

Output: The code above will create a PDF file string-to-pdf.pdf

String to PDF

Converting HTML File to PDF

  • We can convert HTML file to PDF using the same method convertToPdf() that takes HTML as InputStream and write the PDF content into OutputStream.
  • The HTML file can contain CSS file and images. However, they need to be in the same location of the HTML file. If CSS and images are located in different directories, we need to use ConverterProperties to set the base URL.

index.html

<html>
	<head>
		<title>HTML to PDF</title>
		<link href="style.css" rel="stylesheet" type="text/css" />
	</head>
	<body>
		<h1>HTML to PDF</h1>
		<p>
            <span class="itext">itext</span> 7.1.9 
            <span class="description"> converting HTML to PDF</span>
		</p>
		<table>
		  <tr>
				<th class="label">Title</th>
				<td>iText - Java HTML to PDF</td>
			</tr>
			<tr>
				<th>URL</th>
				<td>http://hmkcode.com/itext-html-to-pdf-using-java</td>
			</tr>
		</table>
	</body>
</html>

package com.hmkcode;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import com.itextpdf.html2pdf.HtmlConverter;

public class App 
{
	
    public static void main( String[] args ) throws FileNotFoundException, IOException  
    {
    	HtmlConverter.convertToPdf(new FileInputStream("index.html"), 
            new FileOutputStream("index-to-pdf.pdf"));

        System.out.println( "PDF Created!" );
    }
}

Output: The code above will create a PDF file string-to-index.pdf

HTML to PDF

Source Code @ GitHub