iText – Convert HTML to PDF Using Java

itext-html-pdfiText “XML Worker” allows developers to convert XML files to PDF documents in a programmer-friendly way. iText can also convert HTML to PDF with the CSS style implemented on HTML elements.

 

Objective:

  • How to convert HTML file to PDF document using iText Java Library?

Environment & Tools

  • Eclipse (or any other IDE)
  • Maven (optional)

Library:

  • iText 5.4.2

( 1 ) HTML File

  • index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<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> 5.4.2 <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>
  • style.css
h1 {
  color:#ccc;
}
table tr td{
	text-align:center;
	border:1px solid gray;
	padding:4px;
}
table tr th{
	background-color:#84C7FD;
	color:#fff;
	width: 100px;
}
.itext{
	color:#84C7FD;
	font-weight:bold;
}
.description{
	color:gray;
}

( 2 ) Java App

  • App.java
package com.hmkcode;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;

public class App 
{
    public static void main( String[] args ) throws DocumentException, IOException
    {
      // step 1
    	Document document = new Document();
        // step 2
    	PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("pdf.pdf"));
        // step 3
        document.open();
        // step 4
        XMLWorkerHelper.getInstance().parseXHtml(writer, document,
                new FileInputStream("index.html"));	
        //step 5
         document.close();

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

( 3 ) Output “PDF”

itext-html-pdf-output

Source Code @ GitHub

5 thoughts on “iText – Convert HTML to PDF Using Java

  1. fluminis

    Nice article.

    But you do not show how the css file is loaded.
    If the html file is in an other directory (test/index.html in my case), the css file is not loaded correctly and the pdf file is created correctly but without css.

    Thanks in advance

  2. Yvonne

    I get an “the method parseXHtml(PdfWriter, Document,Reader) in the type XMLWorkerHelper is not applicable for the arguments (PdfWriter, Document, FileInputStream)” error

  3. sagar

    Exception in thread “main” java.lang.NoSuchMethodError: com.itextpdf.text.Paragraph.trim()Z
    at com.itextpdf.tool.xml.html.AbstractTagProcessor.currentContentToParagraph(AbstractTagProcessor.java:257)
    at com.itextpdf.tool.xml.html.Header.end(Header.java:93)
    at com.itextpdf.tool.xml.html.AbstractTagProcessor.endElement(AbstractTagProcessor.java:189)
    at com.itextpdf.tool.xml.pipeline.html.HtmlPipeline.close(HtmlPipeline.java:206)
    at com.itextpdf.tool.xml.XMLWorker.endElement(XMLWorker.java:141)
    at com.itextpdf.tool.xml.parser.XMLParser.endElement(XMLParser.java:395)
    at com.itextpdf.tool.xml.parser.state.ClosingTagState.process(ClosingTagState.java:70)
    at com.itextpdf.tool.xml.parser.XMLParser.parseWithReader(XMLParser.java:235)
    at com.itextpdf.tool.xml.parser.XMLParser.parse(XMLParser.java:213)
    at com.itextpdf.tool.xml.parser.XMLParser.parse(XMLParser.java:174)
    at com.itextpdf.tool.xml.XMLWorkerHelper.parseXHtml(XMLWorkerHelper.java:223)
    at com.itextpdf.tool.xml.XMLWorkerHelper.parseXHtml(XMLWorkerHelper.java:185)
    at com.itextpdf.App.main(App.java:24)

Comments are closed.