Convert HTML to PDF in Java
Aspose.PDF for Java can convert local HTML files, archived MHTML content, and live web pages into PDF documents. You can control the conversion pipeline with HtmlLoadOptions and MhtLoadOptions to influence layout scaling, CSS media handling, page-rule priority, font embedding, resource resolution, and single-page rendering behavior.
Convert HTML to PDF
Use this example when a local HTML file should be converted directly into a PDF document.
- Create an
HtmlLoadOptionsinstance to configure how the HTML source is interpreted during import. - Set
HtmlPageLayoutOptiontoScaleToPageWidthso wide HTML content is scaled to the target PDF page width instead of being clipped. - Open the source HTML file by passing its path and the configured load options into the
Documentconstructor. - Save the generated
Documentas a PDF file at the target output path.
public static void convertHtmlToPdf(Path inputFile, Path outputFile) {
HtmlLoadOptions loadOptions = new HtmlLoadOptions();
loadOptions.setPageLayoutOption(HtmlPageLayoutOption.ScaleToPageWidth);
try (Document document = new Document(inputFile.toString(), loadOptions)) {
document.save(outputFile.toString());
}
System.out.println(inputFile + " converted into " + outputFile);
}
Convert HTML to PDF with media type options
Use this example when CSS media type handling should be controlled during HTML conversion.
- Create an
HtmlLoadOptionsinstance for the conversion settings. - Set
HtmlMediaTypetoScreenwhen the HTML should be rendered with CSS rules intended for on-screen display instead of print media. - Open the HTML file with the configured load options so media-query-dependent styles are applied during conversion.
- Save the resulting
Documentas a PDF file.
public static void convertHtmlToPdfMediaType(Path inputFile, Path outputFile) {
HtmlLoadOptions loadOptions = new HtmlLoadOptions();
loadOptions.setHtmlMediaType(HtmlMediaType.Screen);
try (Document document = new Document(inputFile.toString(), loadOptions)) {
document.save(outputFile.toString());
}
System.out.println(inputFile + " converted into " + outputFile);
}
Convert HTML to PDF with CSS page rule priority
Use this example when CSS @page rules should influence the final PDF page layout.
- Create an
HtmlLoadOptionsinstance before opening the HTML file. - Configure
setPriorityCssPageRule(false)when other layout settings should take precedence over CSS@pagedeclarations in the source markup. - Load the HTML content into a
Documentwith the configured options so the page layout is resolved during import. - Save the generated PDF file.
public static void convertHtmlToPdfPriorityCssPageRule(Path inputFile, Path outputFile) {
HtmlLoadOptions loadOptions = new HtmlLoadOptions();
loadOptions.setPriorityCssPageRule(false);
try (Document document = new Document(inputFile.toString(), loadOptions)) {
document.save(outputFile.toString());
}
System.out.println(inputFile + " converted into " + outputFile);
}
Convert HTML to PDF with embedded fonts
Use this example when the output PDF should preserve the HTML fonts by embedding them.
- Create an
HtmlLoadOptionsinstance for the HTML import configuration. - Enable
setEmbedFonts(true)so the fonts resolved during HTML rendering are stored in the output PDF. - Open the HTML source with these load options to keep the original typography available in the final document.
- Save the
Documentas a PDF with the embedded font resources included.
public static void convertHtmlToPdfEmbedFonts(Path inputFile, Path outputFile) {
HtmlLoadOptions loadOptions = new HtmlLoadOptions();
loadOptions.setEmbedFonts(true);
try (Document document = new Document(inputFile.toString(), loadOptions)) {
document.save(outputFile.toString());
}
System.out.println(inputFile + " converted into " + outputFile);
}
Render HTML content on a single PDF page
Use this example when long HTML content should be kept on one PDF page instead of flowing across multiple pages.
- Create an
HtmlLoadOptionsinstance for the conversion settings. - Enable
setRenderToSinglePage(true)so the imported HTML is laid out on one PDF page rather than split across several pages. - Open the source HTML with the configured load options and let Aspose.PDF build the page layout in a
Document. - Save the output PDF file.
public static void convertHtmlToPdfRenderContentToSamePage(Path inputFile, Path outputFile) {
HtmlLoadOptions loadOptions = new HtmlLoadOptions();
loadOptions.setRenderToSinglePage(true);
try (Document document = new Document(inputFile.toString(), loadOptions)) {
document.save(outputFile.toString());
}
System.out.println(inputFile + " converted into " + outputFile);
}
Convert HTML containing inline SVG
Use this example when the HTML source includes inline SVG data that must be rendered in the PDF.
- Create an
HtmlLoadOptionsinstance with the HTML file’s parent directory as the base path so related resources can be resolved consistently during conversion. - Open the HTML file that contains inline SVG markup by passing the source path and load options into the
Documentconstructor. - Let Aspose.PDF render the HTML DOM together with the embedded SVG elements into the PDF page content.
- Save the generated PDF document.
public static void convertHtmlToPdfWithSvgData(Path inputFile, Path outputFile) {
HtmlLoadOptions loadOptions = new HtmlLoadOptions(inputFile.getParent().toString());
try (Document document = new Document(inputFile.toString(), loadOptions)) {
document.save(outputFile.toString());
}
System.out.println(inputFile + " converted into " + outputFile);
}
Convert a web page to PDF
Use this example when a live web URL should be rendered and saved as a PDF document.
- Create an
HtmlLoadOptionsinstance with the target URL so relative resources such as stylesheets and images can be resolved against that address. - Convert the URL string into a
URLobject and open its input stream to fetch the live HTML content. - Create a
Documentfrom the response stream and the configured load options so the downloaded page is processed with the correct base URL. - Save the rendered web page as a PDF file and close the stream resources automatically with try-with-resources.
public static void convertWebPageToPdf(String urlString, Path outputFile) {
HtmlLoadOptions loadOptions = new HtmlLoadOptions(urlString);
try {
URL url = URI.create(urlString).toURL();
try (InputStream inputStream = url.openStream()) {
try (Document document = new Document(inputStream, loadOptions)) {
document.save(outputFile.toString());
}
}
System.out.println(url + " converted into " + outputFile);
} catch (Exception e) {
e.printStackTrace();
}
}
Convert MHTML to PDF
Use this example when an archived MHTML file should be converted into a PDF document.
- Create an
MhtLoadOptionsinstance to tell Aspose.PDF to load the source as MIME HTML content. - Open the
.mhtor.mhtmlfile by passing its path and the MHTML load options into theDocumentconstructor. - Let Aspose.PDF parse the archived HTML content and its embedded resources into the PDF document model.
- Save the generated PDF file.
public static void convertMhtmlToPdf(Path inputFile, Path outputFile) {
MhtLoadOptions loadOptions = new MhtLoadOptions();
try (Document document = new Document(inputFile.toString(), loadOptions)) {
document.save(outputFile.toString());
}
System.out.println(inputFile + " converted into " + outputFile);
}