Recently I was in a situation where I have to convert a large number of HTML files or webpage HTML response to PDF files. Here I will share the python program that I used to convert those HTML to PDF.
We will be using two packages pdfkit and wkhtmltopdf to convert the HTML files to PDFs.
pdfkit used wkhtmltopdf itself internally so we have to install both the packages. Else you will get an error message “please install wkhtmltopdf”.
To install pdfkit use the following pip command.
$ pip install pdfkit
Installation of wkhtmltopdf package differs for windows and Ubuntu.
For Ubuntu use the pip command:
sudo apt-get install wkhtmltopdf
For Windows users, you need to download and install the executable file.
Set PATH VARIABLE to its binary files folder under Environment Variables as the program has to be accessed via commands.
Now you are all set to start the conversion of your HTML files.
Open your favorite IDE or command and try the following scripts.
- Python program to convert saved HTML files to pdf:
import pdfkit pdfkit.from_file('my.html', 'my_pdf_file.pdf')
- Convert Website Request HTML files to pdf:
import pdfkit pdfkit.from_url('https://www.developerminutes.com/','developerminutes.pdf')
- Python program to convert text file to PDF.
import pdfkit pdfkit.from_string('My PDF file','mypdf.pdf')
PDFKIT provides more options for conversion. For example, You can pass multiple URL or multiple HTML files in a list to form a single pdf.
Look for the official documentation for more details.
Happy Coding. Comment below if you have any doubt or issue with conversion.