---
title: "Converting HTML to PDF | Nutrient Java SDK"
canonical_url: "https://www.nutrient.io/guides/java/conversion/html-to-pdf/"
md_url: "https://www.nutrient.io/guides/java/conversion/html-to-pdf.md"
last_updated: "2026-06-09T21:11:56.021Z"
description: "Convert HTML files to PDF documents using Nutrient Java SDK."
---

# Converting HTML to PDF

HTML content can render differently across browsers, devices, and viewport sizes. Converting HTML to PDF gives you a fixed output format for sharing, archiving, and printing.

Use this workflow to:

- Preserve layout and styling in a stable document format

- Distribute web content for offline access

- Archive rendered pages for audit or compliance workflows

[Download sample](https://www.nutrient.io/downloads/samples/java/html-to-pdf.zip)

## Use the Java SDK for conversion

Use the Java SDK to convert HTML to PDF directly in your app. Nutrient renders the HTML through Chrome, so the PDF preserves the page layout, fonts, images, and styling.

HTML-to-PDF conversion on Linux relies on Chrome for rendering. If you run this workflow in Docker on Linux, make sure the container includes Chrome and runs as a non-root user. For the packaged Docker setup, refer to the [running Chrome-based conversions in Docker](https://www.nutrient.io/guides/java/deployment/headless-modes-google-chrome.md) guide.

## Preparing the project

Define a package and create a class for the conversion flow:

```java

package io.nutrient.Sample;

```

Import Nutrient Java SDK classes. Prefer explicit imports for the classes you use:

```java

import io.nutrient.sdk.Document;
import io.nutrient.sdk.exceptions.NutrientException;

public class HtmlToPDF {

```

Create a `main` method and declare `NutrientException`:

```java

    public static void main(String[] args) throws NutrientException {

```

Then add the SDK-specific conversion logic.

## Proceeding with the conversion

The next step is to open the HTML file and export it as PDF.

This guide uses the `Document` class. Initialize it with a [try-with-resources statement](https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html) so Java closes resources correctly:

```java

        try (Document document = Document.open("input.html")) {

```

Use an absolute or relative path. This example reads from the working directory.

Export the rendered page as PDF. This example writes `output.pdf` to the working directory:

```java

            document.exportAsPdf("output.pdf");
        }
    }
}

```

You now have a PDF file.

## Error handling

The SDK throws `NutrientException` when conversion fails. Handle this exception in your app for custom logging, retries, or fallback logic.

## Conclusion

You now have a complete HTML-to-PDF conversion flow in Java. Nutrient renders the HTML through Chrome and writes the result to `output.pdf`.

To run this example as-is, download the [sample package](https://www.nutrient.io/downloads/samples/java/html-to-pdf.zip).
---

## Related pages

- [Converting CAD files to PDF](/guides/java/conversion/cad-to-pdf.md)
- [Converting PDF documents to Excel format for data analysis](/guides/java/conversion/pdf-to-excel-document.md)
- [Nutrient Java SDK conversion guides](/guides/java/conversion.md)
- [Converting a document from XLSX to PDF format](/guides/java/conversion/excel-document-to-pdf.md)
- [Converting email files to PDF](/guides/java/conversion/email-to-pdf.md)
- [Converting PDF documents to HTML format for web publishing](/guides/java/conversion/pdf-to-html.md)
- [Converting PDF documents to PDF/A format](/guides/java/conversion/pdf-to-pdf-a.md)
- [Converting a document from Markdown to PDF format](/guides/java/conversion/markdown-to-pdf.md)
- [Converting PDF documents to image format](/guides/java/conversion/pdf-to-image.md)
- [Converting PDF documents to PDF/UA format](/guides/java/conversion/pdf-to-pdf-ua.md)
- [Converting PDF documents to Markdown format](/guides/java/conversion/pdf-to-markdown.md)
- [Converting a document from PPTX to PDF format](/guides/java/conversion/powerpoint-document-to-pdf.md)
- [Converting a document from PDF to DOCX format](/guides/java/conversion/pdf-to-word-document.md)
- [Converting a document from DOCX to PDF format](/guides/java/conversion/word-document-to-pdf.md)
- [Converting a document from DOCX to PDF/UA format](/guides/java/conversion/word-document-to-pdf-ua.md)
- [Converting PDF documents to PowerPoint presentations](/guides/java/conversion/pdf-to-powerpoint-document.md)
- [Converting a Word document to PDF while preserving comments](/guides/java/conversion/word-document-to-pdf-including-comments.md)

