Skip to footer content

How to Convert HTML to PDF using IronPDF for Python

In this tutorial, we explore converting HTML to PDF using the IronPDF library in Python. Begin by installing IronPDF via pip and creating a virtual environment. Import the necessary IronPDF classes, set your license key, and configure debugging options. Create a sample HTML string for conversion to PDF. Use the Chrome PDF Renderer class to render HTML as a PDF, saving the result under a chosen filename. The tutorial provides examples of converting both simple and complex HTML strings into PDFs, showcasing IronPDF's capabilities. Experience IronPDF's functionality firsthand with a 30-day trial license, and easily integrate this conversion tool into your Python projects. Subscribe to Iron Software's channel for more tutorials.

Below is an example code snippet for converting HTML to PDF using IronPDF:

# Make sure to install the IronPDF library first
# pip install IronPDF

from ironpdf import ChromePdfRenderer

# Set your IronPDF license key
license_key = "YOUR_LICENSE_KEY_HERE"

# Create an instance of the ChromePdfRenderer
pdf_renderer = ChromePdfRenderer()

# Optionally, enable debugging options - helps in development
pdf_renderer.debug_mode = True

# Sample HTML string to be converted into PDF
html_content = """
    <html>
        <head>
            <title>Sample HTML</title>
        </head>
        <body>
            <h1>Hello, World!</h1>
            <p>This is a sample HTML converted to PDF using IronPDF.</p>
        </body>
    </html>
"""

# Convert the HTML content to a PDF and save it to a file
try:
    pdf_document = pdf_renderer.render_html_as_pdf(html_content)
    pdf_document.save_as("output.pdf")
    print("PDF successfully created and saved as 'output.pdf'.")
except Exception as e:
    print(f"An error occurred: {e}")
# Make sure to install the IronPDF library first
# pip install IronPDF

from ironpdf import ChromePdfRenderer

# Set your IronPDF license key
license_key = "YOUR_LICENSE_KEY_HERE"

# Create an instance of the ChromePdfRenderer
pdf_renderer = ChromePdfRenderer()

# Optionally, enable debugging options - helps in development
pdf_renderer.debug_mode = True

# Sample HTML string to be converted into PDF
html_content = """
    <html>
        <head>
            <title>Sample HTML</title>
        </head>
        <body>
            <h1>Hello, World!</h1>
            <p>This is a sample HTML converted to PDF using IronPDF.</p>
        </body>
    </html>
"""

# Convert the HTML content to a PDF and save it to a file
try:
    pdf_document = pdf_renderer.render_html_as_pdf(html_content)
    pdf_document.save_as("output.pdf")
    print("PDF successfully created and saved as 'output.pdf'.")
except Exception as e:
    print(f"An error occurred: {e}")
PYTHON

Further Reading: HTML to PDF: Python

Chaknith Bin
Software Engineer
Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience.