How to create a PDF file from HTML using PDFBox?

I was looking for an HTML to PDF renderer. We were using iText. I was looking to do same with Apache PDFBox. But, it looks like it cannot be done.

I can either use Apache FOP or continue using iText.

Here is the iText solution if anyone is interested: Java Render XML Document as PDF

If you are looking for a solution for merging using PDF box, here it is Merge pdf files using Apache pdf box


Open HTML to PDF library uses PDFBox under the hood and hides all the conversion complexity.

Usage is quite simple:

try (OutputStream os = new FileOutputStream("/Users/me/output.pdf")) {
    PdfRendererBuilder builder = new PdfRendererBuilder();
    builder.withUri("file:////Users/me/input.html");
    builder.toStream(os);
    builder.run();
}

Tags:

Java

Pdf

Pdfbox