Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
HTML to PDF Conversion in Java
// Create HTML load options
HtmlLoadOptions htmloptions = new HtmlLoadOptions();
// Load HTML file
Document doc = new Document("HTML-Document.html", htmloptions);
// Set passwords and encryption PDF document
doc.encrypt("userpassword", "ownerPassword", Permissions.ModifyContent , CryptoAlgorithm.AESx256);
// Save HTML file as PDF
doc.save("HTML-to-PDF.pdf");
// Create and initialize URL
URL oracleURL = new URL("https://docs.oracle.com/javase/tutorial/networking/urls/readingURL.html");
// Get web page as input stream
InputStream is = oracleURL.openStream();
// Initialize HTML load options
HtmlLoadOptions htmloptions = new HtmlLoadOptions();
// Load stream into Document object
Document pdfDocument = new Document(is, htmloptions);
// Save output as PDF format
pdfDocument.save("HTML-to-PDF.pdf");
// Create HTML load options
HtmlLoadOptions htmloptions = new HtmlLoadOptions();
// Load HTML file
Document doc = new Document("HTML-Document.html", htmloptions);
// Convert HTML file to PDF
doc.save("HTML-to-PDF.pdf");
// Create HTML load options
HtmlLoadOptions htmloptions = new HtmlLoadOptions();
// Set conversion to single page PDF
htmloptions.setRenderToSinglePage(true);
// Load HTML file
Document doc = new Document("HTML-Document.html", htmloptions);
// Save HTML file as PDF
doc.save("HTML-to-PDF.pdf");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.