21

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?

Developer Guy
  • 2,318
  • 6
  • 19
  • 37

3 Answers3

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
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
  • 4
    I 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
  • Please provide code of your implementation @yngwietiger – Ever Think Aug 20 '18 at 07:42
  • 2
    Unfortunately Flying Saucer does not support CSS3 – Clément Poissonnier Sep 26 '19 at 14:06
  • 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");
}
}