How to convert HTML to PDF using Python


Python provides Pdfcrowd API v2 which is convert HTML documents to PDF. This API is very easy to use and the integration takes only a couple of lines of code.

Installation

Install the client library from PyPI
$ pip install pdfcrowd

Conversion will be completed in following 3 Steps from Webpage/HTML to PDF

Step 1 − Download the library pdfkit

$ pip install pdfkit

Step 2 − Now 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.

Step 3 − Code in Python to Download −

(i) Already Saved HTML page

import pdfkit 
pdfkit.from_file('my_test.html', my_'output.pdf')

(ii) Convert by website URL

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

(iii) Store text in PDF

import pdfkit
pdfkit.from_string('my_testpdf ABC','testpdf.pdf')

Updated on: 30-Jul-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements