---
title: "Converting HTML to PDF | Nutrient Python SDK"
canonical_url: "https://www.nutrient.io/guides/python/conversion/html-to-pdf/"
md_url: "https://www.nutrient.io/guides/python/conversion/html-to-pdf.md"
last_updated: "2026-06-09T19:34:32.777Z"
description: "Convert HTML files to PDF documents using Nutrient Python 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/python/html-to-pdf.zip)

## How Nutrient supports this workflow

Nutrient Python SDK converts HTML to PDF through a single API call. 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/python/deployment/headless-modes-google-chrome.md) guide.

You don’t need to handle:

- HTML parsing and layout

- Rendering fonts, images, and CSS styling

- Page sizing and pagination

Use the SDK API to handle the conversion in your application.

## Complete implementation

The following example converts a local `input.html` file to PDF.

Import the required Nutrient classes:

```python

from nutrient_sdk import Document
from nutrient_sdk import NutrientException

```

Then open the HTML file with a Python [context manager](https://docs.python.org/3/reference/datamodel.html#context-managers). The context manager closes the document automatically:

```python

def main():
    try:
        with Document.open("input.html") as document:

```

Export the file to PDF and handle conversion errors with `NutrientException`:

```python

            document.export_as_pdf("output.pdf")
            print("Successfully converted to output.pdf")
    except NutrientException as e:
        print(f"Error: {e}")

if __name__ == "__main__":
    main()

```

## Summary

The conversion has two steps:

1. Open the HTML file.

2. Export it as PDF.

Nutrient renders the HTML through Chrome and writes the result to `output.pdf`, so you don’t need to implement format-specific rendering logic.

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

## Related pages

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

