How to Convert HTML to PDF in Python?


In this digital age, we are constantly inundated with tons of information from a variety of sources. Oftentimes, this information is in the form of HTML documents. While HTML is a great format for sharing information electronically, it can be difficult to print or save for offline viewing. This is where PDF comes in. PDF is a portable document format that can be easily shared, printed, and saved for later viewing.

Converting HTML to PDF is a great way to make your electronic information more accessible and user-friendly. In this blog post, we will show you how to convert HTML to PDF using Python. We will also provide a brief overview of the advantages of using PDF over HTML.

How to Convert HTML to PDF in Python

There are a number of ways to convert HTML to PDF using Python. We will go over two methods: using the pdfkit library and using the wkhtmltopdf library.

Pdfkit is open-source library that can be used to convert HTML pages into PDF files. Pdfkit uses wkhtmltopdf behind the scenes but adds a number of useful features, such as the ability to encrypt PDFs, add watermarks, and attach files. Pdfkit also offers an API that can be used to build custom applications for converting HTML to PDF.

Installation

Install python-pdfkit:

$ pip install pdfkit # (or pip3 for python3) or from requirements.txt $ pip install -r requirements.txt # (or pip3 for python3)
Code language: PHP (php)

Install wkhtmltopdf:

Debian/Ubuntu:

$ sudo apt-get install wkhtmltopdf
Code language: JavaScript (javascript)

macOS:

$ brew install caskroom/cask/wkhtmltopdf

Script: Source code

#!/usr/bin/python # -*- coding: utf-8 -*- # a Python script to generate pdf from URL import pdfkit import sys def converttopdf(url): pdfkit.from_url(url, 'out.pdf') if __name__ == '__main__': converttopdf(str(sys.argv[1:]))
Code language: PHP (php)

To save this script with the name file: html2pdf.py

Usage
You run this script with simple tasks:

python html2pdf.py 'pc-savvy.com' (for python3) You can pass a list with multiple URLs or files: python html2pdf.py 'pc-savvy.com' 'pcworld.com' 'engadget.com' (for python3) Also you can pass an opened file.
Code language: JavaScript (javascript)

Wkhtmltopdf is an open-source command line tool that uses WebKit to convert HTML pages into PDF files. Since wkhtmltopdf uses WebKit, it supports all major browsers’ CSS3 and JavaScript rendering engines. Wkhtmltopdf is not tied to any one operating system and can be used on Windows, macOS, and Linux machines.

Both wkhtmltopdf and pdfkit are easy to use and produce high-quality results. However, wkhtmltopdf may be slightly easier to use since it does not require any additional dependencies or configuration.

Conclusion:

PDF is a great format for sharing documents electronically. It is compact, user-friendly, and can be easily shared, printed, and saved for later viewing. Converting HTML to PDF is a great way to make your electronic information more accessible and user-friendly. In this blog post, we showed you how to convert HTML to PDF using Python and the wkhtmltopdf and pdfkit libraries.

Andy Avery

I really enjoy helping people with their tech problems to make life easier, ​and that’s what I’ve been doing professionally for the past decade.

Recent Posts