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.

  1. Create an HtmlLoadOptions instance to configure how the HTML source is interpreted during import.
  2. Set HtmlPageLayoutOption to ScaleToPageWidth so wide HTML content is scaled to the target PDF page width instead of being clipped.
  3. Open the source HTML file by passing its path and the configured load options into the Document constructor.
  4. Save the generated Document as 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.

  1. Create an HtmlLoadOptions instance for the conversion settings.
  2. Set HtmlMediaType to Screen when the HTML should be rendered with CSS rules intended for on-screen display instead of print media.
  3. Open the HTML file with the configured load options so media-query-dependent styles are applied during conversion.
  4. Save the resulting Document as 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.

  1. Create an HtmlLoadOptions instance before opening the HTML file.
  2. Configure setPriorityCssPageRule(false) when other layout settings should take precedence over CSS @page declarations in the source markup.
  3. Load the HTML content into a Document with the configured options so the page layout is resolved during import.
  4. 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.

  1. Create an HtmlLoadOptions instance for the HTML import configuration.
  2. Enable setEmbedFonts(true) so the fonts resolved during HTML rendering are stored in the output PDF.
  3. Open the HTML source with these load options to keep the original typography available in the final document.
  4. Save the Document as 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.

  1. Create an HtmlLoadOptions instance for the conversion settings.
  2. Enable setRenderToSinglePage(true) so the imported HTML is laid out on one PDF page rather than split across several pages.
  3. Open the source HTML with the configured load options and let Aspose.PDF build the page layout in a Document.
  4. 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.

  1. Create an HtmlLoadOptions instance with the HTML file’s parent directory as the base path so related resources can be resolved consistently during conversion.
  2. Open the HTML file that contains inline SVG markup by passing the source path and load options into the Document constructor.
  3. Let Aspose.PDF render the HTML DOM together with the embedded SVG elements into the PDF page content.
  4. 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.

  1. Create an HtmlLoadOptions instance with the target URL so relative resources such as stylesheets and images can be resolved against that address.
  2. Convert the URL string into a URL object and open its input stream to fetch the live HTML content.
  3. Create a Document from the response stream and the configured load options so the downloaded page is processed with the correct base URL.
  4. 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.

  1. Create an MhtLoadOptions instance to tell Aspose.PDF to load the source as MIME HTML content.
  2. Open the .mht or .mhtml file by passing its path and the MHTML load options into the Document constructor.
  3. Let Aspose.PDF parse the archived HTML content and its embedded resources into the PDF document model.
  4. 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);
}