PDFKit Module in Python – Convert HTML to PDF

Pdfkit

In this article let’s learn about a PDFKit, which is a PDF document generation open-source library. It is the easiest way to convert any webpage into a PDF using Python.

Prerequisites for Converting HTML to PDF

Before we start with the code python, make sure to install PDFKit. To install PDFKit below are the commands to be used.

$ pip install pdfkit  (or pip3 for python3)

Once the library is installed, install wkhtmltopdf. It is an open-source command line tool used to render HTML into PDF and various image formats using the Qt WebKit rendering engine.

For Debian/Ubuntu:

$ sudo apt-get install wkhtmltopdf

For macOS

$ brew install homebrew/cask/wkhtmltopdf

For Windows Click Here.

Remember to add the path wkhtmltopdf to environment variables to avoid errors.

Converting HTML to PDF using Python

While implementing if your editor throws an error like “ImportError: No module named pdfkit” try to configure the path in your current directory. This can be done by executing the following code in your editor beforehand.

path_wkthmltopdf = 'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe'
config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf)

To convert a website URL to PDF

import pdfkit

pdfkit.from_url("https://www.askpython.com/", "sample_url.pdf")

OUTPUT

Pdfkit 1
Pdfkit 1

To convert HTML files into PDF

  • Remember the file you want to convert into PDF should be in .HTML format.
  • Also, the file should be present in the same directory.
import pdfkit

pdfkit.from_file("sample.html", "sample_file.pdf")

OUTPUT

Pdfkit 2
Pdfkit 2

To convert a string into a PDF

import pdfkit

pdfkit.from_string('AskPython :)','sample_string.pdf')

OUTPUT

Pdfkit 3
Pdfkit 3

Summary

In this article, we have discussed the PDFKit wrapper or library. PDFKit for python is an adapted version of the ruby PDFKit library. Also, we learned the easiest way to convert web pages, HTML files, or strings into PDFs right in python.