language guide

language guide

Python

Python

The Best Python Libraries for PDF Generation in 2025

Marcelo Abreu, founder of pdforge

Marcelo | Founder

Marcelo | Founder

Jan 5, 2025

Jan 5, 2025

Overview: top pdf python libraries

This article covers top pdf libraries in python to help developers improve pdf generation workflows. It introduces how html to pdf transforms static or dynamic content into portable documents. It also outlines best libraries, from browser-driven conversions to third-party APIs, helping you find a tailored solution for your SaaS needs.

Browser-based PDF libraries convert web pages into pdf documents by leveraging a headless browser’s print API.

Non-browser-based methods rely on native Python systems like wkhtmltopdf or a drawing canvas.

Third-party pdf generation API can lighten operational load while delivering advanced features, including collaborative editing.

Browser-based PDF libraries

Browser-based tools operate by rendering your HTML in a headless environment, then invoking a print-like method to create a final pdf. This ensures high-fidelity layouts consistent with real-world browser behavior.

Playwright and Pyppeteer dominate this category, each depending on browser engines to capture complex CSS or JavaScript. They excel in scenarios like form creation or interactive UI where visual accuracy is paramount.

Non-browser-based PDF libraries

Non-browser-based solutions can operate without launching a headless browser. They either leverage a command-line utility like wkhtmltopdf or rely on native Python drawing APIs. This approach typically consumes fewer system resources.

PDFKit and pypdf2 sit in this realm. PDFKit seamlessly transforms HTML to PDF with minimal setup, while pypdf2 focuses on merging, splitting, or watermarking existing files. Meanwhile, ReportLab uses a canvas model for entirely custom design, allowing you to position text, images, or shapes with absolute precision.

Third party pdf generation API

Third-party APIs such as pdforge integrate easily with existing code bases and scale to large volume pdf generation. They reduce infrastructural strain and provide easy ways to embed dynamic data into HTML or no-code templates, making them ideal for projects requiring robust collaboration or custom branding.

Best pdf libraries python 2025
Best pdf libraries python 2025

In-depth look at top pdf libraries python

Generation PDF with Playwright

download status for playwright from pypstats.org

Playwright is a cross-browser automation library from Microsoft. Install with:



It supports Chromium, Firefox, and WebKit. Suppose you have an invoice.html describing client details and itemized charges:

import asyncio
from playwright.async_api import async_playwright

async def generate_invoice():
    async with async_playwright() as p:
        browser = await p.chromium.launch()
        page = await browser.new_page()
        await page.goto("file:///path_to/invoice.html")
        await page.pdf(
            path="invoice_playwright.pdf",
            format="A4",
            print_background=True
        )
        await browser.close()

asyncio.run(generate_invoice())

Playwright is ideal for dynamic html to pdf tasks. For deeper details, see our full guide on how to transform html into pdf with playwright in Python.

Generation PDF with Pyppeteer

download status for pyppeteer from pypstats.org

Pyppeteer, an unofficial port of Puppeteer, offers browser-based pdf generation in Python. Install with:

Same invoice.html example:

import asyncio
from pyppeteer import launch

async def generate_invoice():
    browser = await launch()
    page = await browser.newPage()
    await page.goto("file:///path_to/invoice.html")
    await page.pdf(path="invoice_pyppeteer.pdf", format="A4")
    await browser.close()

asyncio.get_event_loop().run_until_complete(generate_invoice())

It accurately reflects any JavaScript or styling on the page. You can check out our guide for HTML into PDF transformation using pyppeteer.

Generation PDF with PDFKit

download status for pdfkit from pypstats.org

PDFKit converts HTML content into PDFs using wkhtmltopdf. Install:

pip install pdfkit
sudo

Using the same invoice.html:

import pdfkit
  
options = {
    "page-size": "A4",
    "print-media-type": "",
    "enable-local-file-access": None
}

pdfkit.from_file("invoice.html", "invoice_pdfkit.pdf", options=options)

PDFKit delivers simplicity and stable results with minimal overhead. We also have a full guide on generating pdf from html with pdfkit.

Generation PDF with pypdf2

download status for pypdf2 from pypstats.org

pypdf2 focuses on modifying existing PDF documents. Install with:

pypdf2 primarily enables reading, merging, splitting, or adding pages to existing PDFs. Though it lacks robust HTML conversion capabilities, you can still assemble a new invoice PDF by using a base template and placing text annotations on the page.

Below is an example of creating an invoice from a predefined invoice_template.pdf. The code adds a free text annotation, effectively writing text into the PDF:

from PyPDF2 import PdfReader, PdfWriter

def create_invoice_with_pypdf2(customer, description, amount):
    # Load a base template PDF page
    template_reader = PdfReader("invoice_template.pdf")
    writer = PdfWriter()
    
    # Get the first page from the template
    invoice_page = template_reader.pages[0]
    
    # Define a free text annotation
    annotation = {
        "/Type": "/Annot",
        "/Subtype": "/FreeText",
        "/Rect": [100, 700, 300, 750],  # adjust positioning as needed
        "/Contents": f"Invoice for: {customer}\nDescription: {description}\nAmount: ${amount}"
    }
    
    # Ensure the page has an annotations array
    if "/Annots" not in invoice_page:
        invoice_page["/Annots"] = []
    
    # Attach the annotation to the page
    invoice_page["/Annots"].append(annotation)
    
    # Add the modified page to the writer and save
    writer.add_page(invoice_page)
    with open("invoice_pypdf2.pdf", "wb") as output_file:
        writer.write(output_file)
create_invoice_with_pypdf2("Alice", "Design Services", 2500)

In practice, this approach works best when you already have a PDF with placeholders or background graphics. Although pypdf2 can’t transform raw HTML, it complements your pipeline if you often deal with watermarks, bookmarks, or merging multi-page PDFs in a SaaS environment. For more powerful layout and direct HTML-to-PDF needs, consider pairing it with other libraries or template engines. If you want to know more about pypdf2, check out our guide here.

Generation PDF with reportlab

download status for reportlab from pypstats.org

ReportLab builds PDFs from scratch using a drawing canvas. Install:

Generating an invoice:

from reportlab.lib.pagesizes import A4
from reportlab.pdfgen import canvas

def generate_invoice():
    c = canvas.Canvas("invoice_reportlab.pdf", pagesize=A4)
    c.drawString(100, 800, "Invoice for: Jane Smith")
    c.drawString(100, 780, "Description: Design services")
    c.drawString(100, 760, "Amount: $2500")
    c.showPage()
    c.save()

generate_invoice()

ReportLab supports text flow, shapes, and custom styles for professional branding. Here's a guide on how to generate pdf documents with reportlab.

Generation PDF with pdforge

pdforge is a third-party pdf generation API. You can create beautiful reports with flexible layouts and complex components with an easy-to-use opinionated no-code builder. Let the AI do the heavy lifting by generating your templates, creating custom components or even filling all the variables for you.

You can handle high-volume PDF generation from a single backend call.

Here’s an example of how to generate pdf with pdforge via an API call:

import requests
import json

url = 'https://api.pdforge.com/v1/pdf/sync'
headers = {
    'Authorization': 'Bearer your-api-key',
    'Content-Type': 'application/json'
}
data = {
    'templateId': 'your_invoice_template_id_here',
    'data': {
          "invoice_number": "your invoice_number here",
          "invoice_date": "your invoice_date here",
          # ...other invoice variables here
    }
}

response = requests.post(url, headers=headers, data=json.dumps(data))
with open('output.pdf', 'wb') as f:
        f.write(response.content)

You can create your account, experience our no-code builder and create your first layout template without any upfront payment clicking here.

Comparison between libraries

Each library addresses different top pdf libraries python priorities:

| Library      | Ease of Use | Ease of Implementation | Complex Layouts | Resource Heavy | Recommended Usage        |
|--------------|-------------|------------------------|-----------------|----------------|--------------------------|
| Playwright   | Medium      | Moderate               | High            | High           | Interactive forms        |
| Pyppeteer    | Medium      | Moderate               | High            | High           | Browser fidelity         |
| PDFKit       | Easy        | Medium                 | Medium          | Medium         | Quick html to pdf        |
| pypdf2       | Medium      | Medium                 | Low             | Low            | Merging & editing        |
| ReportLab    | Medium      | Medium                 | High            | Low            | Custom drawn documents   | 
| pdforge      | Easy        | Easy                   | High            | Low            | Scalable SaaS workflows

Conclusion

The best pdf generation solution depends on your requirements.

If your primary goal is precise browser rendering, choose browser-based tools such as Playwright or Pyppeteer. Those seeking simpler pipelines might favor PDFKit. Browser-based pdf generation provides high fidelity for visually dynamic pages, whereas non-browser-based solutions focus on performance and ease of setup.

Choose third-party pdf generation APIs, like pdforge, if you don't want to waste time maintaining pdfs layouts and their infrastructure or if you don't want to keep track of best practices to generate PDFs at scale.

Generating pdfs at scale can be quite complicated!

Generating pdfs at scale can be quite complicated!

We take care of all of this, so you focus on what trully matters on your Product!

We take care of all of this, so you focus on what trully matters on your Product!

Try for free

7-day free trial

Table of contents

Title