Python Convert Html to PDF

Configurare noua (How To)

Situatie

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.

Solutie

Pasi de urmat

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')

Store text in PDF

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

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

Tip solutie

Permanent

Voteaza

(8 din 13 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?