HTML to PDF: Python

This guide provides Python developers with step-by-step instructions on utilizing the IronPDF library to convert HTML content into high-quality PDF format (portable document format) files.

IronPDF is a comprehensive PDF converter and processing library that supports multiple programming languages, including .NET, Java, and Python programming languages. This tutorial focuses specifically on using IronPDF in Python scripts to convert HTML content, whether it's in the form of files or markup.

For a separate tutorial on how to convert HTML to PDF in .NET applications, please refer to the following here.


Overview


Getting Started

1. Installing IronPDF PDF Library for Python

Python Library for PDF

Install with pip

 pip install ironpdf
or
Python module

Download Module

Download Now

Manually install into your project

To install the IronPDF library for Python, you can use the popular package manager, pip. Simply execute the following command:

 pip install ironpdf

Tips
To install a specific version of IronPdf, please use the following syntax: "==2023.x.x". For example, you can run the command "pip install ironpdf==2023.x.x".

Please note
IronPDF Python relies on IronPDF .NET library, specifically .NET 6.0, as its underlying technology. Therefore, it is necessary to have the .NET 6.0 SDK installed on your machine in order to use IronPDF Python.


How-To Guide and Code Examples

2. Convert HTML to PDF

In the following section, we will delve into the impressive rendering capabilities of IronPDF for converting HTML to PDF.

The primary component for PDF document rendering is the ChromePdfRenderer class. Additionally, the PdfDocument class offers a range of manipulation features. IronPDF provides reliable methods for converting HTML content to PDF documents, catering to three key scenarios:

  • Convert HTML strings/markup to PDF
  • Convert HTML files/zips to PDF
  • Convert URLs to PDF

This section will provide a concise overview of each use case, accompanied by supplementary resources for further details.

2.1 Import the IronPDF Package

To import IronPDF, include the following import statements at the beginning of the source files where IronPDF will be utilized:

# Import statements for IronPDF Python
from ironpdf import *
PYTHON

2.2. Set the License Key (optional)

IronPDF for Python is free to use, but it adds a tiled background watermark to PDFs for free users.

Visit licensing page to obtain your license key and enjoy watermark-free PDF.

To generate PDFs without watermarks using IronPDF, it is necessary to provide a valid license key to the library. The following code snippet demonstrates how to configure the library with a license key:

# Apply your license key
License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01"
PYTHON

Ensure that the license key is set before generating PDF files or modifying their content. It is recommended to call the LicenseKey method before any other lines of code.

You can purchase a license key from our licensing page or contact us to obtain a free trial license key.

2.3 Set the Log File Location (optional)

IronPDF can generate log messages to a text file named Default.log in the same directory as your Python script.

If you want to customize the log file name and location, you can set the LogFilePath property using the code snippet below:

# Set a log path
Logger.EnableDebugging = True
Logger.LogFilePath = "Custom.log"
Logger.LoggingMode = Logger.LoggingModes.All
PYTHON

Please note
Logger.LogFilePath should be called before using any PDF conversion and manipulation methods.

2.4. Creating a PDF from HTML String

The RenderHtmlAsPdf method converts HTML string into a PDF format document.

The code snippet below demonstrates how to generate a PDF file from HTML string with a single headline element:

from ironpdf import *

# Instantiate Renderer
renderer = ChromePdfRenderer()

# Create a PDF from a HTML string using Python
pdf = renderer.RenderHtmlAsPdf("<h1>Hello from IronPDF!</h1>")

# Export to a file or Stream
pdf.SaveAs("output.pdf")
PYTHON

Convert HTML markup into PDF File using the RenderHtmlAsPdf method. This method can generate PDFs using all valid W3C-compliant HTML and CSS markup.

The RenderHtmlAsPdf method processes HTML, CSS, and JavaScript in the same way that modern browsers do, ensuring accurate rendering of the content. This feature enables software engineers to create PDFs that closely resemble their web browser counterparts.

Furthermore, the RenderHtmlAsPdf method can handle external resources such as images, stylesheets, and scripts located in local or network folders. The following example demonstrates the creation of a PDF document from HTML that references a CSS file and an image stored in an assets folder:

from ironpdf import *

html = """
<html>
   <head>
      <title>Hello world!</title>
      <link rel='stylesheet' href='assets/style.css'>
   </head>
   <body>
      <h1>Hello from IronPDF!</h1>
      <a href='https://ironpdf.com/python/'><img src='assets/logo.png' /></a>
   </body>
</html>
"""

renderer = ChromePdfRenderer()
pdf = renderer.RenderHtmlAsPdf(html)
pdf.SaveAs("output.pdf")
PYTHON

The result of the above code is shown in the image below.

RenderHtmlAsPdf method is capable of rendering various types of HTML content. If it can be displayed in Chrome, then RenderHtmlAsPdf will render it!

Additionally, developers have the option to provide a second argument to the RenderHtmlAsPdf method, allowing them to specify a base path for referencing web assets. This path can point to a local directory on the filesystem or even a URL path.

To gain a better understanding of how to use the RenderHtmlAsPdf method, you can refer to this code example or consult the API Reference pages for more detailed information.

2.5. Creating a PDF from a URL

To convert website URL into PDF documents, developers can utilize the RenderUrlAsPdf method provided by IronPDF.

Here's an example that demonstrates rendering a Wikipedia article into PDF content.

from ironpdf import *

# Instantiate Renderer
renderer = ChromePdfRenderer()

# Create a PDF from a URL or local file path
pdf = renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/PDF")

# Export to a file or Stream
pdf.SaveAs("url.pdf")
PYTHON

The generated PDF file format is shown below.

For more information you can refer to code example demonstrating how to convert webpage into PDF.

2.6. Creating a PDF from an HTML File

IronPDF provides the capability to convert HTML files to PDF and store them on a local filesystem. It directly renders the HTML content into its equivalent PDF format.

For a real-world demonstration of this functionality, the following code example showcases the conversion of an invoice HTML file. You can access the HTML markup of the invoice.

This HTML markup is provided for your convenience:

<html>
<head>
    <meta charset="utf-8">
    <title>Invoice</title>
    <link rel="stylesheet" href="style.css">
    <link rel="license" href="https://www.opensource.org/licenses/mit-license/">
    <script src="script.js"></script>
</head>
<body>
<header>
    <h1>Invoice</h1>
    <address contenteditable>
        <p>Jonathan Neal</p>
        <p>101 E. Chapman Ave<br>Orange, CA 92866</p>
        <p>(800) 555-1234</p>
    </address>
    <span><img alt="" src="http://www.jonathantneal.com/examples/invoice/logo.png"><input type="file" accept="image/*"></span>
</header>
<article>
    <h1>Recipient</h1>
    <address contenteditable>
        <p>Some Company<br>c/o Some Guy</p>
    </address>
    <table class="meta">
        <tr>
            <th><span contenteditable>Invoice #</span></th>
            <td><span contenteditable>101138</span></td>
        </tr>
        <tr>
            <th><span contenteditable>Date</span></th>
            <td><span contenteditable>January 1, 2012</span></td>
        </tr>
        <tr>
            <th><span contenteditable>Amount Due</span></th>
            <td><span id="prefix" contenteditable>$</span><span>600.00</span></td>
        </tr>
    </table>
    <table class="inventory">
        <thead>
        <tr>
            <th><span contenteditable>Item</span></th>
            <th><span contenteditable>Description</span></th>
            <th><span contenteditable>Rate</span></th>
            <th><span contenteditable>Quantity</span></th>
            <th><span contenteditable>Price</span></th>
        </tr>
        </thead>
        <tbody>
        <tr>
            <td><a class="cut">-</a><span contenteditable>Front End Consultation</span></td>
            <td><span contenteditable>Experience Review</span></td>
            <td><span data-prefix>$</span><span contenteditable>150.00</span></td>
            <td><span contenteditable>4</span></td>
            <td><span data-prefix>$</span><span>600.00</span></td>
        </tr>
        </tbody>
    </table>
    <a class="add">+</a>
    <table class="balance">
        <tr>
            <th><span contenteditable>Total</span></th>
            <td><span data-prefix>$</span><span>600.00</span></td>
        </tr>
        <tr>
            <th><span contenteditable>Amount Paid</span></th>
            <td><span data-prefix>$</span><span contenteditable>0.00</span></td>
        </tr>
        <tr>
            <th><span contenteditable>Balance Due</span></th>
            <td><span data-prefix>$</span><span>600.00</span></td>
        </tr>
    </table>
</article>
<aside>
    <h1><span contenteditable>Additional Notes</span></h1>
    <div contenteditable>
        <p>A finance charge of 1.5% will be made on unpaid balances after 30 days.</p>
    </div>
</aside>
</body>
</html>
<html>
<head>
    <meta charset="utf-8">
    <title>Invoice</title>
    <link rel="stylesheet" href="style.css">
    <link rel="license" href="https://www.opensource.org/licenses/mit-license/">
    <script src="script.js"></script>
</head>
<body>
<header>
    <h1>Invoice</h1>
    <address contenteditable>
        <p>Jonathan Neal</p>
        <p>101 E. Chapman Ave<br>Orange, CA 92866</p>
        <p>(800) 555-1234</p>
    </address>
    <span><img alt="" src="http://www.jonathantneal.com/examples/invoice/logo.png"><input type="file" accept="image/*"></span>
</header>
<article>
    <h1>Recipient</h1>
    <address contenteditable>
        <p>Some Company<br>c/o Some Guy</p>
    </address>
    <table class="meta">
        <tr>
            <th><span contenteditable>Invoice #</span></th>
            <td><span contenteditable>101138</span></td>
        </tr>
        <tr>
            <th><span contenteditable>Date</span></th>
            <td><span contenteditable>January 1, 2012</span></td>
        </tr>
        <tr>
            <th><span contenteditable>Amount Due</span></th>
            <td><span id="prefix" contenteditable>$</span><span>600.00</span></td>
        </tr>
    </table>
    <table class="inventory">
        <thead>
        <tr>
            <th><span contenteditable>Item</span></th>
            <th><span contenteditable>Description</span></th>
            <th><span contenteditable>Rate</span></th>
            <th><span contenteditable>Quantity</span></th>
            <th><span contenteditable>Price</span></th>
        </tr>
        </thead>
        <tbody>
        <tr>
            <td><a class="cut">-</a><span contenteditable>Front End Consultation</span></td>
            <td><span contenteditable>Experience Review</span></td>
            <td><span data-prefix>$</span><span contenteditable>150.00</span></td>
            <td><span contenteditable>4</span></td>
            <td><span data-prefix>$</span><span>600.00</span></td>
        </tr>
        </tbody>
    </table>
    <a class="add">+</a>
    <table class="balance">
        <tr>
            <th><span contenteditable>Total</span></th>
            <td><span data-prefix>$</span><span>600.00</span></td>
        </tr>
        <tr>
            <th><span contenteditable>Amount Paid</span></th>
            <td><span data-prefix>$</span><span contenteditable>0.00</span></td>
        </tr>
        <tr>
            <th><span contenteditable>Balance Due</span></th>
            <td><span data-prefix>$</span><span>600.00</span></td>
        </tr>
    </table>
</article>
<aside>
    <h1><span contenteditable>Additional Notes</span></h1>
    <div contenteditable>
        <p>A finance charge of 1.5% will be made on unpaid balances after 30 days.</p>
    </div>
</aside>
</body>
</html>
HTML

Assume we have a local HTML file along with its associated CSS and JavaScript files saved in a folder named "invoices," we can use IronPDF to convert the sample HTML file to PDF with the following Python code:

# Instantiate Renderer
renderer = ChromePdfRenderer()

# Create a PDF from an existing HTML file using Python
pdf = renderer.RenderHtmlFileAsPdf("invoices/TestInvoice1.html")

# Export to a file or Stream
pdf.SaveAs("htmlfile_to_pdf.pdf")
PYTHON

Similar to the conversion of HTML strings to PDF, IronPDF automatically resolves relative URLs in the sample HTML file, ensuring that any referenced stylesheets and scripts are correctly applied to the resulting PDF document. This ensures that the visual appearance of the web page is accurately captured in the PDF file.

3. Further Reading

Explore the extensive capabilities of IronPDF's HTML to PDF rendering by delving into our Code Examples section.

  1. Read this code example to discover how to customize the appearance of PDF documents during the conversion process.
  2. Learn how to generate PDF files with personalized headers and footers, adjust margin sizes and page dimensions, add watermarks, and more.
  3. Additionally, explore techniques for extracting text, optimizing file sizes, and programmatically printing PDFs.

Download the software product.