Convert HTML to PDF with Python

WebTutPro
techiediaries.com
Published in
4 min readNov 4, 2020

There is a wide variety of choices when it comes to creating a PDF from HTML in Python. In this article, we’ll look at some open-source and commercial tools that are available and share our recommendations to help you find the tool that works best for you.

According to this article:

The Portable Document Format (PDF) is a file format developed by Adobe in 1993 to present documents, including text formatting and images, in a manner independent of application software, hardware, and operating systems.

Continue reading Convert HTML to PDF with Python on Techiediaries.

Why Converting HTML to PDF

HTML is used to create layouts in web applications and sometimes you’ll need to provide the information that’s already presented to users in HTML to a PDF document. For instance, in eCommerce apps, you most likely need to provide documents such as statistical reports, tickets, and invoices to users as PDF documents so they can save them locally for records, have them emailed, or print them.

Let’s take the example of an invoice, the application may already display the information in the invoice with HTML and CSS, then provide a button that users can click to download the PDF document of the invoice for their records. That means the information is already formatted and available to the users. You only need to provide that in PDF format for download and printing. Therefore, a good solution to consider would be to check if it is possible to use that HTML and CSS to generate a PDF version.

PDF is a portable document format that’s good for printing. CSS also has a specification for print, referred to as the Paged Media module. This makes CSS convenient for formatting things like books, catalogs, and brochures which are not designed to be a web page in the first place.

The biggest difference, and conceptual shift, is that printed documents refer to a page model that is of a fixed size. Whereas on the web we are constantly reminded that we have no idea of the size of the viewport, in print the fixed size of each page has a bearing on everything that we do. Due to this fixed page size, we have to consider our document as a collection of pages, paged media, rather than the continuous media that is a web page. Source

You may also want to check out this article for more details about designing for print with CSS.

Using IronPDF Python

When working with Python, IronPDF emerges as a reliable solution for converting HTML into PDFs. This powerful PDF library allows Python developers to effortlessly transform HTML content into high-quality PDF files, ensuring the formatting and layout remain consistent. Ideal for creating reports, invoices, or any document that needs to be in PDF format with precision, IronPDF stands out for its effectiveness. If you’re seeking a straightforward way to create PDFs from HTML in Python, I recommend exploring IronPDF. Their detailed tutorials offer a comprehensive guide to getting started.

Implementation:

Implementing IronPDF to convert HTML to PDF is very easy. You’ve to follow some simple steps, and your HTML will be converted into the perfect PDF within a matter of seconds. Let’s go through the steps:

Step 1: Install IronPDF

First, ensure that IronPDF is installed in your Python environment. You can do it using the following command:

pip install ironpdf

Step 2: Import IronPDF Library

First, import the IronPDF modules in the Python Script. Use the following command to import it. Make sure that you use this statement at the top:

Import statement for IronPDF Python

from ironpdf import *

Step 3: Add License Key

Before starting the conversion, it’s important to add your IronPDF license key to work it at its full potential in the production stage. You can get your license key from the license page of IronPDF Python.

License.LicenseKey = "IRONPDF-LICENSE-KEY"

Step 4: Convert HTML To PDF

Here comes the main point of this guide. We’ll use the RenderHtmlAdPdf method to convert HTML to the PDF document. We’ll provide the HTML String as the parameter. This string can contain CSS and JavaScript, too, to beautify the generated PDF. Here is the sample code:

import sys
from ironpdf import *License.LicenseKey = "IronPDF-License-Key"html_string = """<!DOCTYPE html><html><head><title>Bootstrap HTML to PDF Test Document</title><link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"></head><body><div class="container"><h1 class="mt-5 mb-3">Main Heading</h1><p>This is a sample paragraph to test text rendering in PDF with Bootstrap styles.</p><h2>Lists</h2><ol class="list-group mb-4"><li class="list-group-item">First Ordered Item</li><li class="list-group-item">Second Ordered Item</li></ol><h2>Table</h2><table class="table table-bordered table-striped mb-4"><thead><tr><th>Header 1</th><th>Header 2</th></tr></thead><tbody><tr><td>Row 1, Cell 1</td><td>Row 1, Cell 2</td></tr><tr><td colspan="2">Row 2, Single Cell spanning two columns</td></tr></tbody></table><h2>Images and Links</h2><img src="https://via.placeholder.com/150" class="img-fluid mb-3" alt="Placeholder Image"><a href="https://www.example.com" class="btn btn-primary mb-4">External Link</a></div></body></html>"""

Using IronPDF to convert HTML to PDF

renderer = ChromePdfRenderer()
pdf = renderer.RenderHtmlAsPdf(html_string)pdf.SaveAs("HTML String To PDF.pdf")

By executing this program, you’ll get the following output, which matches our HTML input.

If you don’t want to mess up your code, have the code in an HTML file. Then, you can use the RenderHtmlFileAsPdf method to convert the HTML file to a PDF document.

--

--