Web Pages to PDF Converter in Python Using pdfkit

Subhan khaliq
2 min readFeb 12, 2021

--

What is pdfkit ?

It is a PDF document generation library. It is one of the better approach to convert HTML into PDF with various image formats, HTML forms, and other complex printable documents. So, it is very useful for us in this way.

How to use pdfkit to convert a web page into PDF ?

Let’s talk about it in steps.

Step 1

First of all install pdfkit in Python by running the following code.

pip install pdfkit

Step 2

Now, Convert the web page into PDF.

import pdfkit
pdfkit.from_url('https://www.google.com')

So, it will convert the web page of google into pdf. You can also convert a local file of Html into PDF. Here, our focus is on the conversion of a web page by using its URL. If you want to know more about its functionality you can go to the following link.

Now, let’s talk about the conversion of PDF into different formats. Let’s suppose I want to convert the web page into four different formats i.e tabloid, mobile, Large web, and small web. We can do it easily by running the following script.

In this script, we write the formats first which we need and then in the option field of pdfkit we set the page formats which we have selected already. This script stores the PDF’s in four different files and you can use them if you want.

I hope this blog will help you in converting a web page into PDF.
Here you can find the complete code.

--

--