1. Products
  2.   Conversion
  3.   Python
  4.   WeasyPrint
 
  

Open Source HTML to PDF Conversion Library in Python

Try this Free & Open Source Python API as an HTML to PDF conversion library.

What is WeasyPrint?

WeasyPrint is an open-source Python library and command-line tool that converts HTML and CSS files into PDF documents with high-quality rendering, making it a versatile tool for generating PDFs from web content. WeasyPrint allows converting inline HTML code, local HTML files and web URLs to PDF documents with a single line of code.

A brief list of features supported by WeasyPrint are listed below:

  • WeasyPrint supports many of the CSS specifications written by the W3C.
  • It provides comprehensive support for URLs, HTML, and supported HTML tags.
  • WeasyPrint can handle external resources such as images or CSS stylesheets through a URL fetcher.
  • The default fetcher can open file and HTTP URLs.
  • It supports various features related to PDF generation, including fonts, CSS, CSS Level 2 Revision 1, Selectors Level 3/4, and CSS Text.
GitHub

GitHub Stats

Name:
Language:
Stars:
Forks:
License:
Repository was last updated at

Getting Started with WeasyPrint

You need Python version 3.7.0 or higher to install and use WeasyPrint. So, first install Python and then use below commands to install WeasyPrint on your machine using pip and virtual environment:

Install on Linux


python3 -m venv venv
source venv/bin/activate
pip install weasyprint
weasyprint --info

Install on Ubuntu 20.04 or higher


apt install python3-pip libpango-1.0-0 libharfbuzz0b libpangoft2-1.0-0 libffi-dev libjpeg-dev libopenjp2-7-dev

Install on MacOS using Homebrew


brew install weasyprint

Install on Windows 11 64-bit


python3 -m venv venv
venv\Scripts\activate.bat
python3 -m pip install weasyprint
python3 -m weasyprint --info

Note: For installation on Windows, install GTK3 before running above commands.

Convert HTML to PDF via Web URL

We can convert any HTML web page to PDF with a single line of code using WeasyPrint Python library. We just need to import HTML class from the library, create an object of HTML class with a website URL and call write_pdf method to convert it into PDF document with a desired name.

Convert HTML to PDF via Web URL with Inline CSS

We can also convert a website URL to PDF with inline stylesheet using WeasyPrint library.

Conclusion

WeasyPrint is a simple to use free and open source Python library that converts HTML and CSS to PDF documents. You can create HTML based templates for required documents and then quickly export them to PDF. WeasyPrint gives flexible conversion options to developers by allowing them to convert HTML to PDF from a website URL, local HTML file, string containing HTML code and file object etc.

Even WeasyPrint is a great free option of converting HTML to PDF documents for Python developers though in case of complex HTML & CSS structure, generated PDF documents may not render at high fidelity which can create issues for the users with strict requirements.

Similar Products

 English