1. Products
  2.   PDF
  3.   Python
  4.   XHTML2PDF
 
  

Free Python Library for HTML and CSS Conversion to PDF

Open Source Python Library allows to Generate PDF Documents from HTML. It Supports Advanced Page Layout, Images & Tables Rendering, Adding Custom Fonts, Headers/Footers and so on.

What is XHTML2PDF Library?

In the world of web development, the need to generate PDF documents from HTML content is a common requirement. Whether it's for creating invoices, reports, or any other type of printable document, having a reliable tool to handle this conversion is crucial. This is where XHTML2PDF, a powerful open-source Python library, comes to the rescue. html2pdf (formerly known as pisa) is a pure-Python library that transforms HTML (including XHTML) and CSS into PDF documents. It’s built atop the ReportLab toolkit and html5lib, with optional support from PyPDF. Platform-agnostic and accessible, it lets developers turn their web-based content into printable PDFs quickly and effectively

XHTML2PDF is packed with features that make it a versatile tool for PDF generation. It supports HTML5 and CSS 2.1 support, seamless Django integration, powerful command-line interface, custom PDF-specific tags, advanced page layout and styling, better support for tables & images and so on. What makes XHTML2PDF so appealing to developers is its simplicity and flexibility. If you know how to write HTML and CSS, you can start generating PDFs with this library in no time. It's a pure Python library, which means it is platform-independent and can run on any operating system that supports Python. It's also important to be aware of library limitation, it does not support the entire CSS specification and does not execute JavaScript.

Previous Next

Getting Started with XHTML2PDF

XHTML2PDF can be installed using pip, the following commands will install from a Python wheel if one is available for your platform.

Install XHTML2PDF via pip

 pip install xhtml2pdf 

Clone Repository via GitHUB

 git clone https://github.com/xhtml2pdf/xhtml2pdf.git  

It is also possible to install it manually; download the latest release files directly from GitHub repository.

Convert HTML & CSS to PDF via Python

The primary function of the open source XHTML2PDF library is to parse HTML and CSS (including inline styles) and render them accurately into a PDF document. It supports a wide subset of HTML tags and CSS properties, allowing for significant design flexibility. It supports features like page break, headers and footers, page margins and size, embed fonts and so on. The following simple example shows how software developers can convert HTML content into a PDF file via Python code.

How to Convert HTML Content into a PDF file via Python?

from xhtml2pdf import pisa

# The HTML content to be converted
html_content = """


Hello, World!

This is my first PDF generated with XHTML2PDF.

""" # The output PDF file name output_filename = "hello_world.pdf" # Open the output file in binary write mode with open(output_filename, "w+b") as pdf_file: # Create the PDF pisa_status = pisa.CreatePDF( html_content, # the HTML to convert dest=pdf_file) # file handle to receive result # Check if PDF creation was successful if not pisa_status.err: print(f"Successfully created PDF: {output_filename}") else: print(f"Error creating PDF: {pisa_status.err}")

Advanced Page Layout & Custom Fonts Support

With XHTML2PDF library, software developers have fine-grained control over the layout of their PDF pages. They can define page size, margins, and orientation using the @page CSS rule. Developers can also create custom headers and footers for their documents. Moreover, if you need to use specific fonts in your PDFs, the library allows you to embed custom fonts. This is particularly useful for branding and ensuring consistent typography across all documents.

PDF Images & Tables Rendering via Python Library

The open source XHTML2PDF library provides robust support for rendering tables and images in your PDFs. You can style your tables using CSS, and for images, you can control their size and alignment. The library can easily and correctly resolve as well as embed images, fonts, and other static assets, which is critical for generating self-contained PDFs. You can automatically generate a table of contents by simply adding the tag to your HTML.

HTML5 and CSS 2.1 Support

If you're familiar with HTML and CSS, recreating PDFs with styling becomes intuitive—no new learning curve. The XHTML2PDF library has excellent support for modern web standards, including HTML5 and CSS 2.1, with some support for CSS 3 properties. This means you can use familiar HTML tags and CSS properties to structure and style your PDF documents, just like you would for a web page.