Thursday, October 2, 2025
HomeLanguagesPython Convert Html to PDF

Python Convert Html to PDF

Convert HTML/webpage to PDF

There are many websites that do not allow to download the content in form of pdf, they either ask to buy their premium version or don’t have such download service in form of pdf.

Conversion in 3 Steps from Webpage/HTML to PDF

Step1: Download library pdfkit

 $ pip install pdfkit

Step2: Download wkhtmltopdf
For Ubuntu/Debian:

 sudo apt-get install wkhtmltopdf

For Windows:
(a)Download link: WKHTMLTOPDF
(b)Set: PATH variable set binary folder in Environment variables.

Step3: Code in Python to Download:
(i) Already Saved HTML page




import pdfkit
pdfkit.from_file('test.html', 'out.pdf')


(ii) Convert by website URL




import pdfkit
pdfkit.from_url('https://www.google.co.in/','shaurya.pdf')


(iii) Store text in PDF




import pdfkit
pdfkit.from_string('Shaurya GFG','GfG.pdf')


Congratulations: Your pdf file would be created and saved in the same directory where python file exists.

Miscellaneous Knowledge Content:
1. You can pass a list with multiple URLs or files:




pdfkit.from_url(['google.com', 'geeksforgeeks.org', 'facebook.com'], 'shaurya.pdf')
pdfkit.from_file(['file1.html', 'file2.html'], 'out.pdf')


2. Save content in a variable




# Use False instead of output path to save pdf to a variable
pdf = pdfkit.from_url('http://google.com', False)


RELATED ARTICLES

Most Popular

Dominic
32331 POSTS0 COMMENTS
Milvus
85 POSTS0 COMMENTS
Nango Kala
6703 POSTS0 COMMENTS
Nicole Veronica
11867 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11927 POSTS0 COMMENTS
Shaida Kate Naidoo
6818 POSTS0 COMMENTS
Ted Musemwa
7079 POSTS0 COMMENTS
Thapelo Manthata
6775 POSTS0 COMMENTS
Umr Jansen
6776 POSTS0 COMMENTS