Python HTML to PDF libraries

Updated Nov 2026

Python HTML to PDF libraries render a web page or an HTML string as a PDF document. A dedicated renderer lays out HTML and CSS itself rather than printing the page from a headless browser. These libraries let apps generate invoices, reports, and receipts from the same templates they show on the web.

Popularity

PyMuPDF has the most monthly downloads in this comparison, at 168.7M, a count that covers all its PDF work.

LibraryMonthly downloadsStarsLast releaseLicense
PyMuPDF
168.7M
11.2KNov 2026AGPLv3
Playwright
134.2M
15.1KNov 2026Apache-2.0
WeasyPrint
44.5M
9.8KSep 2026BSD-3-Clause
Selenium
30.5M
34.6KNov 2026Apache-2.0
PDFKit
5.0M
2.0KNov 2021MIT
xhtml2pdf
4.3M
2.4KOct 2026Apache-2.0
SeleniumBase
1.3M
13.3KNov 2026MIT
pyppeteer
693.2K
3.9KFeb 2024MIT
Downloads: PyPI, last thirty days. Stars and releases: GitHub. Amber: no release in over a year.

Libraries and code examples

Extracts, analyzes, converts, and manipulates PDF and other documents.

Downloads / mo
168.7M
Stars
11.2K
Last release
2.1
License
AGPLv3

Quickstart

From recipes-stories.rst
$ pip install pymupdf
import pymupdf

HTML = """
<p style="font-family: sans-serif;color: blue">Hello World!</p>
"""

MEDIABOX = pymupdf.paper_rect("letter")  # output page format: Letter
WHERE = MEDIABOX + (36, 36, -36, -36)  # leave borders of 0.5 inches

story = pymupdf.Story(html=HTML)  # create story from HTML
writer = pymupdf.DocumentWriter("output.pdf")  # create the writer

more = 1  # will indicate end of input once it is set to 0

while more:  # loop outputting the story
    device = writer.begin_page(MEDIABOX)  # make new page
    more, _ = story.place(WHERE)  # layout into allowed rectangle
    story.draw(device)  # write on page
    writer.end_page()  # finish page

writer.close()  # close output file

PyMuPDF examples

From open-source projects on GitHub

Controls browser pages and exports their print layouts as PDF.

Downloads / mo
134.2M
Stars
15.1K
Last release
1.65.2
License
Apache-2.0
Runs on
Chromium

Strengths

  • Executes page JavaScript. Render script-driven content in Chromium before exporting a PDF.
  • Selects print or screen styles. Choose the media type before generating the PDF.

Limitations

  • Browser binaries need separate installation. Install Chromium before running the PDF example.

Quickstart

From library-python.md
$ pip install playwright playwright install chromium
from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch()
    page = browser.new_page()
    page.goto("https://playwright.dev")
    print(page.title())
    browser.close()

Playwright examples

From open-source projects on GitHub

WeasyPrint

Turns HTML pages into PDF documents.

Downloads / mo
44.5M
Stars
9.8K
Last release
70.0
License
BSD-3-Clause

Strengths

  • Supports paged print styles. Use CSS page rules, margins, and page counters for print layouts.
  • Renders HTML without browser automation. Generate PDFs with WeasyPrint’s own layout engine.

Limitations

  • JavaScript content needs another renderer. Render script-generated content before passing HTML to WeasyPrint.
  • Relative assets need a base. Set base_url for relative image and stylesheet paths in HTML strings.

Quickstart

From first_steps.html
$ pip install weasyprint
from weasyprint import HTML
HTML('https://weasyprint.org/').write_pdf('/tmp/weasyprint-website.pdf')

Provides the official Python bindings for Selenium WebDriver.

Downloads / mo
30.5M
Stars
34.6K
Last release
4.53.0
License
Apache-2.0

Quickstart

From first_script.py
$ pip install selenium
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()

driver.get("https://www.selenium.dev/selenium/web/web-form.html")

Selenium examples

From open-source projects on GitHub

Converts HTML to PDF through wkhtmltopdf and its WebKit rendering engine.

Downloads / mo
5.0M
Stars
2.0K
Last release
1.0.0
License
MIT

Quickstart

From README.rst
$ pip install pdfkit
import pdfkit

pdfkit.from_url('http://google.com', 'out.pdf')

PDFKit examples

From open-source projects on GitHub

Converts HTML and CSS into PDF documents through Python.

Downloads / mo
4.3M
Stars
2.4K
Last release
0.2.28
License
Apache-2.0

Strengths

  • Defines reusable page frames. Place fixed headers and footers in static frames.
  • Writes PDF into memory. Write PDF data into a BytesIO destination.

Limitations

  • Table rows split at boundaries. Split long content across rows when a cell exceeds its page space.

Quickstart

From quickstart.html
$ pip install xhtml2pdf[all]
import io

from xhtml2pdf import pisa

output = io.BytesIO()

pisa.CreatePDF(
    "<html><body><p>To PDF or not to PDF</p></body></html>",  # page data
    dest=output,                                              # destination "file"
)

# You can get the PDF file bytes with `.getbuffer()`
print(len(output.getbuffer()))

xhtml2pdf examples

From open-source projects on GitHub

SeleniumBase

Crawls, scrapes, and tests the web, with pytest support.

Downloads / mo
1.3M
Stars
13.3K
Last release
4.58.1
License
MIT

Quickstart

From README.md
$ pip install seleniumbase
from seleniumbase import sb_cdp

sb = sb_cdp.Chrome(fast=True)
sb.goto("https://hn.algolia.com/?query=seleniumbase")
elements = sb.find_elements("div.Story_title > a")
for element in elements:
    print("--> " + element.text)
sb.save_as_pdf("sb_news.pdf", folder="./downloaded_files/")
print("*** PDF saved to: ./downloaded_files/sb_news.pdf")

Automates headless Chrome or Chromium through an unofficial port of Puppeteer.

Downloads / mo
693.2K
Stars
3.9K
Last release
2.0.0
License
MIT

Quickstart

From README.md
$ pip install pyppeteer
import asyncio
from pyppeteer import launch

async def main():
    browser = await launch()
    page = await browser.newPage()
    await page.goto('https://example.com')
    await page.screenshot({'path': 'example.png'})
    await browser.close()

asyncio.get_event_loop().run_until_complete(main())

pyppeteer examples

From open-source projects on GitHub

FAQ

WeasyPrint suits invoices, reports, and other documents styled with print CSS. Playwright suits pages that need JavaScript to render. xhtml2pdf suits basic layouts converted without a browser.

WeasyPrint turns HTML and CSS into PDF documents with print features such as page sizes, headers, and footers. It does not execute JavaScript.

Open the page in headless Chromium with Playwright and call page.pdf. The call supports print CSS, backgrounds, page sizes, and headers and footers.

WeasyPrint is a maintained HTML and CSS renderer for paged documents. wkhtmltopdf, which PDFKit wraps, carries a deprecation warning along with PDFKit itself.

For coding agents

Found your HTML to PDF library? Help your agent use it.

Connect Exa so your coding agent can find the library’s documentation, check its API, and look up implementations while you build.

Run in your terminal

claude plugin install exa@claude-plugins-official