I am looking for a way to convert an HTML file to PDF using a Java library that is preferably free. I have done some searching online to look for tools to use, but haven't found a solution that sticks out (I have seen some mention of iText, but it looked like that would have a charge to use it). Is there an existing library that I can utilize to accomplish the conversion of HTML to PDF?
Asked
Active
Viewed 6.6k times
3 Answers
17
You have a few options:
- openhtmltopdf - new code, still brewing, but has some great results
- Apache FOP - can convert XML, not HTML, but might be usefull
- itext the older version (version 2)
- Wkhtmltopdf - can call it from Java via external process, used it with great success so far

ieugen
- 2,293
- 2
- 18
- 19
-
1I changed itext5 to openhtmltopdf and it worked like a charm – Sebastian D'Agostino Jul 19 '19 at 18:33
-
I would like to mention MIT license java library which can be used https://github.com/wooio/htmltopdf-java – USM May 04 '20 at 10:22
11
UPDATE:
I ended up using Flying-Saucer from the Maven repo: https://mvnrepository.com/artifact/org.xhtmlrenderer/flying-saucer-pdf
It was very simple to get this to work for me, here is a method I created to use this:
public static void generatePDF(String inputHtmlPath, String outputPdfPath)
{
try {
String url = new File(inputHtmlPath).toURI().toURL().toString();
System.out.println("URL: " + url);
OutputStream out = new FileOutputStream(outputPdfPath);
//Flying Saucer part
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(url);
renderer.layout();
renderer.createPDF(out);
out.close();
} catch (DocumentException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
And here is the usage:
public static void main(String[] args){
String inputFile = "C:/Users/jrothst/Desktop/TestHtml.htm";
String outputFile = "C:/Users/jrothst/Desktop/TestPdf.pdf";
generatePDF(inputFile, outputFile);
System.out.println("Done!");
}
It worked very well to output the PDF and was very simple to use. It also handled the CSS in the html pretty well. Didn't use it for external CSS, but I believe that is possible too.

Developer Guy
- 2,318
- 6
- 19
- 37
-
4I like Flying Saucer, too. Just be aware that while FS has a friendly license, it uses iText 5 (e.g. that ITextRenderer in your code sample), which has an AGPL license. Because of that, I ended up going with openhtmltopdf, because it has NO iText dependencies (it uses Apache PDFBox). It played quite nicely with CSS, and did everything that I needed it to do, so far. – yngwietiger Aug 01 '18 at 23:03
-
-
2
-
How about "org.xhtmlrenderer:flying-saucer-pdf-openpdf" https://github.com/flyingsaucerproject/flyingsaucer. Does this also use itext? – Satish Patro Mar 08 '21 at 15:16
-4
Here is the complete conversion of html file to pdf file working example.
import com.itextpdf.text.Document;
import com.itextpdf.text.html.simpleparser.HTMLWorker;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.IOException;
import java.io.FileReader;
import java.io.Reader;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.StringReader;
import org.jsoup.Jsoup;
public class Html2pdf2 {
private Html2pdf2() {}
public static String extractText(Reader reader) throws IOException {
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(reader);
String line;
while ( (line=br.readLine()) != null) {
sb.append(line);
}
String textOnly = Jsoup.parse(sb.toString()).text();
return textOnly;
}
public final static void main(String[] args) throws Exception{
FileReader reader = new FileReader
("example.html");
try {
OutputStream file = new FileOutputStream(new File("D:\\Test.pdf"));
Document document = new Document();
PdfWriter.getInstance(document, file);
document.open();
HTMLWorker htmlWorker = new HTMLWorker(document);
htmlWorker.parse(new StringReader(ht));
document.close();
file.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("finished converting");
}
}

yatheendra k v
- 99
- 1
- 7
-
Can you explain your code with some comments to understand the process plz? – IgniteCoders Jul 06 '20 at 14:07
-
@IgniteCoders The above code makes use of the iText library using which you can have the conversion done. Currently am using it to succeed. HTMLWorker class does the Job. – yatheendra k v Aug 24 '20 at 06:56
-
Can you tell use of extractText(Reader reader). You have not used it – Anuj Kumar Nov 14 '22 at 14:32