Java | Converting HTML to PDF using iText
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
HtmlConverterclass is the main class to convert HTML to PDF.HtmlConverterclass has three main methods with different inputs and return types:- convertToDocument(): returns
Documentinstance. - convertToElements(): returns a list of iText
IElementinstances. - convertToPdf(): this method converts HTML to PDF.
- convertToDocument(): returns
convertToPdf() Method
- We will use convertToPdf() method from
HtmlConverterclass to convert HTML to PDF. - convertToPdf() has different variations that takes HTML as
String,FileorInputStream& writes the PDF content toFile,OutputStreamor existingPdfDocument. - In this post we will use the following variations of the convertToPdf()
- convertToPdf(
Stringhtml,OutputStreampdfStream) - convertToPdf(
InputStreamhtmlStream,OutputStreampdfStream)
- convertToPdf(
Converting HTML String to PDF
- The simplest example is converting a
Stringof 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

Converting HTML File to PDF
- We can convert HTML file to PDF using the same method convertToPdf() that takes HTML as
InputStreamand write the PDF content intoOutputStream. - 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
ConverterPropertiesto 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
