· tutorials · 5 min read

How to Convert HTML to PDF with python-pdfkit

Uncover the straightforward approach to converting HTML to PDF in Python using the python-pdfkit library, accompanied by practical code examples.

Uncover the straightforward approach to converting HTML to PDF in Python using the python-pdfkit library, accompanied by practical code examples.

Introduction

Python-pdfkit is a Python wrapper for the wkhtmltopdf command-line tool, which converts HTML to PDF using WebKit. It’s a powerful tool for Python developers who need to generate PDFs from HTML content. This guide will walk you through converting HTML to PDF using python-pdfkit, with easy-to-understand code examples.

Why Python-pdfkit?

  • Simplicity: Python-pdfkit provides a simple Pythonic interface to wkhtmltopdf, making PDF generation straightforward.
  • Flexibility: It supports various customization options through wkhtmltopdf, such as page size, orientation, and margins.
  • Quality Rendering: Leveraging WebKit, it renders HTML content with high fidelity, including styles and media.

Note: If you’re interested in exploring various Python libraries for PDF generation, including options beyond HTML conversion, check out our comprehensive guide: Generate PDFs in Python with 6 Popular Libraries.

Getting Started with Python-pdfkit

Step 1: Install Python-pdfkit and wkhtmltopdf

Before using python-pdfkit, you must install both the library and the wkhtmltopdf binary:

  1. Install python-pdfkit using pip:

    Terminal window
    pip install pdfkit
  2. Install wkhtmltopdf:

    • For Windows and macOS, download the appropriate installer from the wkhtmltopdf website.
    • For Linux, you might be able to install it via your package manager. For example, on Ubuntu:
    Terminal window
    sudo apt-get install wkhtmltopdf

Step 2: Convert HTML to PDF

With python-pdfkit, you can convert HTML strings, files, or URLs directly to PDF. Here’s a basic example of converting an HTML string to a PDF:

import pdfkit
html_content = """
<!DOCTYPE html>
<html>
<head>
<title>PDF Generation with Python-pdfkit</title>
</head>
<body>
<h1>Python-pdfkit Demo</h1>
<p>This is a simple demonstration of converting HTML to PDF in Python.</p>
</body>
</html>
"""
# Convert HTML to PDF
pdfkit.from_string(html_content, 'output.pdf')
print("PDF created successfully.")

In this example, pdfkit.from_string takes the HTML content and the desired output PDF file name, generating a PDF document from the provided HTML.

Converting HTML Files and URLs to PDF

Python-pdfkit also allows you to convert HTML files or web pages directly to PDF:

  • From File:

    pdfkit.from_file('input.html', 'output.pdf')
  • From URL:

    pdfkit.from_url('http://example.com', 'output.pdf')

Customization Options

You can customize the PDF output using options supported by wkhtmltopdf. For instance:

options = {
'page-size': 'Letter',
'margin-top': '0.75in',
'margin-right': '0.75in',
'margin-bottom': '0.75in',
'margin-left': '0.75in',
'encoding': "UTF-8",
'no-outline': None
}
pdfkit.from_file('input.html', 'output.pdf', options=options)

Other Python libraries

There are other Python libraries capable of converting HTML to PDF and you can find more information about it in this article on How To Convert HTML to PDF with Python.

A better approach: HTML to PDF using Templated

The examples above demonstrate how to convert HTML to PDF and web pages to PDF using libraries. However, when it comes to tasks like generating PDFs using templates or keeping track of generated PDFs, additional steps are required.

For instance, to keep track of generated PDFs, you’ll need to develop your own system for tracking the files created. Similarly, if you want to use custom templates, such as those for invoice or certificates generation, you must create and manage those templates manually.

An alternative solution is to utilize Templated, an API-based platform designed for PDF and Image generation, which is ideal for handling such use cases. Their PDF generation API is powered by a Chromium-based rendering engine that fully supports JavaScript, CSS, and HTML.

This approach simplifies the process and eliminates the need for extensive manual template management and tracking. To get started with PDF generation using Templated, follow the documentation and harness the power of this efficient solution.

1. Create PDFs with templates

Templated allows you to design and manage your templates.
After logging in you will see your Dashboard where you can manage your templates or create new ones:

From your Dashboard, you can design your own templates or customize a existing one from our Template Gallery. Bellow is the Certificate of Achievement Template you can use.
There are 100+ free templates available that you can pick and customize to your needs.

To start using Templated API, you need to get your API key that can be found on the API Integration tab on your dashboard.

Now that you have your Templated account ready, let’s see how you can integrate your application with the API. In this example we will be using a certificate template to generate PDFs.

import requests
import json
# Initialize HTTP client
client = requests.Session()
# API URL
url = "https://api.templated.io/v1/render"
# Set headers
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer `${YOUR_API_KEY}`",
}
# Payload data
payload = {
"template": template_id,
"format": "pdf",
"modifications": [
{ "layer": "date", "text": "02/10/2024" }
{ "layer": "name", "text": "John Doe" }
{ "layer": "signature", "text": "Dr. Mark Brown" }
{
"layer": "details",
"text":
"This certificate is awarded to John Doe in recognition of their successful " +
"completion of Computer Sciente Degree on 02/10/2024."
}
],
}
# Serialize payload to JSON
json = json.dumps(payload)
# Make the POST request
response = client.post(url, data=json, headers=headers)
# Read the response
response = response.text
# Print the response
print(response)

and If we check response we have the following

{
"renderUrl":"PDF_URL",
"status":"success",
"template_id":"YOUR_TEMPLATE_ID"
}

In the code above, using Templated to convert HTML to PDF is pretty simple. No additional libraries need to be installed. You only need to make a single API call, providing your data as the request body. That’s all there is to it!

You can use the renderUrl from the response to download or distribute the generated PDF.

To get started, sign up for a free account and begin automating your PDFs today!

Conclusion

Python-pdfkit offers a straightforward and flexible way to convert HTML to PDF in Python applications, making it an excellent tool for generating reports, invoices, and other documents from web content. Its reliance on wkhtmltopdf ensures high-quality rendering, and its Pythonic interface simplifies the PDF generation process.

Automate your images and PDFs with a powerful API

Automate your marketing, social media visuals, banners, PDFs and more with our
 API and no-code integrations

Learn More
Back to Blog

Ready to start generating your images and PDFs?

Sign up to our free trial and try it for yourself

See our latest posts

View all posts »
How To Automate Job Posting Images for LinkedIn

How To Automate Job Posting Images for LinkedIn

Learn how to automate job posting images for LinkedIn using Templated, Google Sheets, and Zapier, saving time while keeping your visuals consistent.

How To Automate Instagram Posts using Google Sheets & Make

How To Automate Instagram Posts using Google Sheets & Make

Learn how to automate your Instagram posts using Templated and Google Sheets, saving time and ensuring a steady flow of engaging content for your audience.

How to Generate Images from Google Sheets with Zapier

How to Generate Images from Google Sheets with Zapier

Learn how to automate image generation using data from Google Sheets and Templated's powerful API with Zapier. This step-by-step guide will help you create a no-code workflow to streamline your visual content creation process.

How to Generate Images from Google Sheets with Make

How to Generate Images from Google Sheets with Make

Learn how to automate image generation using data from Google Sheets and Templated's powerful API with Make (formerly Integromat). This step-by-step guide will help you create a no-code workflow to streamline your visual content creation process.