· 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.
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:
Install python-pdfkit using pip:
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:
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:
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:
From URL:
Customization Options
You can customize the PDF output using options supported by wkhtmltopdf. For instance:
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.
and If we check response
we have the following
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 content with Templated