Q: Do you have any sample code that takes an HTML page and outputs it as a PDF file?

A: We provide a static method to load an HTML document in Qoppa’s Free Java PDF library jPDFWriter.

The support for HTML is limited to the support offered by Java classes to load and render the HTML page, specifically the HTMLEditorKit and HTMLDocument Java classes. This means that HTML support only goes as far as Java supports it in their classes.

See Java Sample Programs below.

Create a PDF from an HTML page loaded from URL:

import com.qoppa.pdfWriter.PDFDocument;
....
URL url = new URL ("https://www.qoppa.com/files/pdfwriter/demo/htmlsamplepage.html");
PageFormat pf = new PageFormat();
PDFDocument pdfDoc = PDFDocument.loadHTML (url, pf, true);
pdfDoc.saveDocument ("c:\\output.pdf");

Create a PDF from an HTML page loaded from a file:

import com.qoppa.pdfWriter.PDFDocument;
....
File f1 = new File ("c:/htmlsamplepage.html");
pdfDoc = PDFDocument.loadHTML(f1.toURI().toURL(), new PageFormat (), false);
pdfDoc.saveDocument ("c:\\output.pdf");

Download Sample HTML Page
Download Output PDF

Pagination Support
The output PDF document will have as many pages as needed to draw the full HTML content.