In this article, we will see how to convert HTML to PDF in python. Here, we will learn to generate HTML to PDF using wkhtmltopdf. wkhtmltopdf
and wkhtmltoimage
are open source command line tools to render HTML into PDF and various image formats using the Qt WebKit rendering engine.
Python 2 and 3 wrapper for wkhtmltopdf utility to convert HTML to PDF using Webkit. So, we will use pdfkit library and wkhtmltopdf.
So, let's see how to generate HTML to PDF in python, python convert HTML to PDF, and pdfkit python.
The Portable Document Format (PDF) is a file format developed by Adobe in 1992 to present documents, including text formatting and images, in a manner independent of application software, hardware, and operating systems.
There are two libraries required:
In this step, we will install pdfkit using the following code.
pip install pdfkit
See the below screenshot.
In this step, we will install wkhtmltopdf using the following code.
Ubuntu:
sudo apt-get install wkhtmltopdf
macOS:
brew install homebrew/cask/wkhtmltopdf
Windows:
Windows users can download wkhtmltopdf from this official page: Download. So, as per your windows version you can download and install it in your system.
Once downloaded, double-click on the binary file and continue with the installation. It would be mostly installed at the path C:\Program Files\wkhtmltopdf
. We should add a bin folder to the system PATH variable in Environment Variables. For example, C:\Program Files\wkhtmltopdf\bin
.
In this example, we will generate a PDF file from the URL. So, we will use the from_url() function.
import pdfkit
pdfkit.from_url('https://www.google.com/','test.pdf')
In this example, we will convert HTML to PDF. We will use the from_file() function.
import pdfkit
pdfkit.from_file('test.html', 'example.pdf')
In this example, we will create PDF from the string. So, you can use the from_string() function.
import pdfkit
pdfkit.from_string('Hello!', 'test.pdf')
OR
var htmlstr = '<h2>Generate PDF File</h2><p>create PDF file</p>'
pdfkit.from_string(htmlstr, 'test.pdf')
You can pass a list with multiple URLs or files.
pdfkit.from_url(['google.com', 'yandex.ru', 'engadget.com'], 'test.pdf')
pdfkit.from_file(['file1.html', 'file2.html'], 'test.pdf')
You might also like: