Welcome folks today in this blog post we will be converting html5 website url to pdf document
using pdfkit library in python. All the full source code of the application is shown below.
Get Started
In order to get started you need to install the below library using the pip
command as shown below
pip install pdfkit
After that you need to make an app.py
file and copy paste the following code
app.py
1 2 3 4 5 6 7 8 9 10 11 |
import pdfkit # Create a PDF document object options = { 'page-size': 'A4', 'margin-top': '0.75in', 'margin-right': '0.75in', 'margin-bottom': '0.75in', 'margin-left': '0.75in', } pdfkit.from_url('http://micropyramid.com', 'micro.pdf', options=options) |
As you can see we are importing the pdfkit
library at the very top and then we are declaring an object which contains the properties of the pdf document that we will be generating such as the page size and different margins from different directions. And after that we need to generate the pdf document
from the url. For this we are using the from_url()
method and inside this we are passing the url of the html5 website that we want to export to pdf document and then we are passing the output file name and then we are passing the options as the third argument
Now if you try to run this python script in the command line you will see it will generate the pdf
file as shown below