Convert HTML to PDF file in Java
Overview
This article explains how to convert HTML to PDF using Java. The code is very simple, just load HTML to Document class and save it as output PDF. Converting MHTML to PDF in Java is also similar. It covers the following topics
- Java HTML to PDF
- Java MHTML to PDF
- Java Convert HTML to PDF
- Java Convert MHTML to PDF
- Java PDF from HTML
- Java PDF from MHTML
- Java HTML to PDF Converter - How to Convert WebPage to PDF
- Java HTML to PDF Library, API or Code to Render, Save, Generate or Create PDF Programmatically from HTML
Java HTML to PDF Converter Library
Aspose.PDF for Java is a PDF manipulation API that lets you convert any existing HTML documents to PDF seamlessly. The process of converting HTML to PDF can be flexibly customized.
Convert HTML to PDF
The following Java code sample shows how to convert an HTML document to a PDF.
- Create an instance of the HtmlLoadOptions class.
- Initialize Document object.
- Save output PDF document by calling Document.save(String) method.
// Open the source PDF document
Document document = new Document(DATA_DIR + "PDFToHTML.pdf")
// Instantiate HTML SaveOptions object
HtmlSaveOptions htmlsaveOptions = new HtmlSaveOptions();
// Save the document
document.save(DATA_DIR + "MultiPageHTML_out.html", htmlsaveOptions);
Try to convert HTML to PDF online
Aspose presents you online free application “HTML to PDF”, where you may try to investigate the functionality and quality it works.
Advanced conversion from HTML to PDF
The HTML Conversion engine has several options that allow us to control the conversion process.
Media Queries Support
- Create a HTML LoadOptions.
- Set Print or Screen mode.
- Initialize Document object.
- Save output PDF document.
Media queries are a popular technique for delivering a tailored style sheet to different devices. We can set device type using HtmlMediaType property.
// Create a HTML LoadOptions
HtmlLoadOptions options = new HtmlLoadOptions();
// Set Print or Screen mode
options.setHtmlMediaType(HtmlMediaType.Print);
// Initialize document object
String htmlFileName = Paths.get(DATA_DIR.toString(), "test.html").toString();
Document document = new Document(htmlFileName, options);
// Save output PDF document
document.save(Paths.get(DATA_DIR.toString(), "HTMLtoPDF.pdf").toString());
document.close();
Enable (disable) font embedding
- Add new Html LoadOptions.
- Enable/Disable font embedding.
- Save a new Document.
HTML pages often use fonts (i.g. fonts from local folder, Google Fonts, etc). We can also control the embedding of fonts in a document using a IsEmbedFonts property.
HtmlLoadOptions options = new HtmlLoadOptions();
// Enable/Disable font embedding
options.setEmbedFonts(true);
Document document = new Document(DATA_DIR + "test_fonts.html", options);
document.save(DATA_DIR + "html_test.PDF");
document.close();
Manage external resource loading
The Conversion Engine provides a mechanism that allows you to control the loading of certain resources associated with the HTML document. The HtmlLoadOptions class has the property CustomLoaderOfExternalResources with which we can define the behavior of the resource loader.
HtmlLoadOptions options = new HtmlLoadOptions();
options.setCustomLoaderOfExternalResources(
new LoadOptions.ResourceLoadingStrategy() {
public LoadOptions.ResourceLoadingResult invoke(String resourceURI) {
// Creating clear template resource for replacing:
LoadOptions.ResourceLoadingResult res = new LoadOptions.ResourceLoadingResult(new byte[] {});
// Return empty byte array in case i.imgur.com server
if (resourceURI.contains("i.imgur.com")) {
return res;
} else {
// Process resources with default resource loader
res.setLoadingCancelled(true);
return res;
}
}
});
Document document = new Document(DATA_DIR + "test.html", options);
document.save(DATA_DIR + "html_test.PDF");
document.close();
Convert MHTML to PDF
Try to convert MHTML to PDF online
Aspose.PDF for Java presents you online free application “MHTML to PDF”, where you may try to investigate the functionality and quality it works.
MHTML, short for MIME HTML, is a web page archive format used to combine resources that are typically represented by external links (such as images, Flash animations, Java applets, and audio files) with HTML code into a single file. The content of an MHTML file is encoded as if it were an HTML email message, using the MIME type multipart/related.
Next code snippet show how to covert MHTML files to PDF format with Java:
// Create an instance of MhtLoadOptions to specify the load options for the
// MHTML file.
MhtLoadOptions options = new MhtLoadOptions();
// Set the path of the MHTML file.
String mhtmlFileName = Paths.get(DATA_DIR.toString(), "samplefile.mhtml").toString();
// Load the MHTML file into a Document object.
Document document = new Document(mhtmlFileName, options);
// Save the document as a PDF file.
document.save(Paths.get(DATA_DIR.toString(), "MarkdowntoPDF.pdf").toString());
// Close the document.
document.close();