pdforge logo

pdforge

Product

Resources

Integrations

PDF Libraries

PDF Libraries

Python

Python

How to Generate PDF from HTML with Playwright using Python (Updated 2025)

Marcelo Abreu, founder of pdforge

Marcelo | Founder

Marcelo | Founder

Aug 6, 2025

Aug 6, 2025

What is Playwright and How Can I Use It to Convert HTML to PDF?

Playwright is a robust, open-source automation toolkit built to streamline browser interactions, including automated testing, web scraping, and generating PDFs from web content. It supports diverse browsers such as Chromium, Firefox, and WebKit, ensuring reliable rendering and consistency across environments. Its intuitive APIs and impressive speed have made it a popular choice among software developers.

For HTML-to-PDF conversion, Playwright excels at capturing precisely what is rendered by the browser. Fully supporting complex CSS styles, JavaScript-generated elements, and responsive layouts. This capability is particularly advantageous for developers building SaaS solutions where accuracy and consistency in generated PDFs like invoices or technical documentation are critical.

Playwright’s extensive documentation further simplifies troubleshooting and accelerates implementation, making it an ideal library for generating PDFs programmatically in Python.


For additional programming languages, explore our guides:

Alternative Python PDF Libraries: How Playwright Stands Out

Numbers of download from playwright

In Python, there are multiple libraries for converting HTML to PDF, each with distinct pros and cons:

  • pdfkit (Python): Effective for simple PDF documents, but inadequate for complex or interactive HTML pages.

  • WeasyPrint: Robust CSS support but struggles with dynamic JavaScript content.

  • wkhtmltopdf: Powerful tool but limited modern browser feature support and challenging dependency management.

  • ReportLab: Excellent for structured PDF generation but lacks native HTML rendering.

  • xhtml2pdf: Lightweight but limited CSS and HTML5 feature support.

  • Pyppeteer: A Python port of Puppeteer, which can also control a browser for rendering HTML to PDF. However, Playwright offers more flexibility with multi-browser support and superior handling of dynamic content.

  • PyPDF2 : This is a Python library for manipulating existing PDFs. While it cannot convert HTML directly into PDF, it can be useful if you need to merge or split PDF documents after generation.

Compared to these options, Playwright uniquely combines comprehensive modern browser support, JavaScript-rendering capabilities, and high fidelity HTML-to-PDF conversion, placing it at the forefront of Python PDF libraries.

If you want to dig deeper on a comparison between playwright and other python pdf libraries, we also have a detailed article with a full comparison between the best PDF libraries for Python in 2025.

Guide to generate pdf from html using python playwright
Guide to generate pdf from html using python playwright

Step 01: Setting Up Playwright for HTML to PDF Conversion in Python

First, ensure Python is installed. If not, download and install it from python.org.

Then install Playwright using pip:



The second command downloads necessary browser binaries (Chromium, Firefox, WebKit).

Step 02: Generating Your First “Hello World” PDF with Playwright Python

Creating PDFs using Playwright in Python is straightforward. Here’s a basic introductory example:

Alternative 01: Generate PDF from a URL

Here’s how to create a PDF from a live URL:

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch()
    page = browser.new_page()
    page.goto('https://example.com')
    page.pdf(path='example.pdf', format='A4')
    browser.close()

This script:

  • Opens a headless Chromium browser

  • Navigates to the specified URL

  • Generates and saves the PDF document

Alternative 02: Generate PDF from Raw HTML

If you have raw HTML content, follow this method:

from playwright.sync_api import sync_playwright

html_content = """
<html>
<head><title>Sample PDF</title></head>
<body><h1>Hello, PDF!</h1></body>
</html>
"""

with sync_playwright() as p:
    browser = p.chromium.launch()
    page = browser.new_page()
    page.set_content(html_content)
    page.pdf(path='sample.pdf', format='Letter')
    browser.close()

Customizing PDF Output (Headers, Footers, Margins)

Playwright allows extensive customization options for PDF generation, including headers, footers, and margins:

page.pdf(
    path='custom.pdf',
    format='A4',
    margin={
        'top': '20mm',
        'bottom': '20mm'
    },
    header_template='<span style="font-size:12px">Header Content</span>',
    footer_template='<span style="font-size:12px">Page <span class="pageNumber"></span> of <span class="totalPages"></span></span>',
    display_header_footer=True
)

Detailed customization options can be found in the official Playwright documentation.

Using HTML Templating Engines

Dynamic content creation is simplified by templating engines such as Jinja2. Here’s an example:

from jinja2 import Template
from playwright.sync_api import sync_playwright

template = Template("<h1>Invoice for {{ client }}</h1>")
rendered_html = template.render(client="ACME Corp")

with sync_playwright() as p:
    browser = p.chromium.launch()
    page = browser.new_page()
    page.set_content(rendered_html)
    page.pdf(path='invoice.pdf')
    browser.close()

Step 03: Scaling PDF Generation with Serverless Architectures

Rendering PDFs with Playwright can be resource-heavy, especially with complex content. Implementing a serverless architecture, such as AWS Lambda, offers scalability, cost efficiency, and high performance by dynamically managing resources.

Note, the browser binaries (e.g., Chromium) must be installed on your Lambda environment. Utilizing Docker containers can simplify this setup process significantly.

A detailed, step-by-step guide for configuring AWS Lambda for Playwright is available here.

Alternative: Scale PDF Generation with Third-Party APIs

homepage of pdforge

For larger SaaS platforms requiring automated PDF generation at scale, if you're developing using no-code platforms like Bubble or creating automation workflows using platforms like n8n, make or zapier, integrating a PDF Generation API like pdforge can offload the heavy lifting.

With pdforge, you can create beautiful PDF documents in minutes using our AI-first PDF Generation. You can fine-tune de design with an easy-to-use opinionated no-code builder and start generating PDFs using our API or native integration with no-code tools. 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

url = 'https://api.pdforge.com/v1/pdf'
payload = {
    "templateId": "invoice-template",
    "data": {"client": "ACME Corp"}
}
headers = {"Authorization": "Bearer YOUR_API_KEY"}

response = requests.post(url, json=payload, headers=headers)

with open('invoice.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.

If you prefer a managed service, third-party APIs like pdforge offer scalable HTML-to-PDF generation without infrastructure setup or design changes management from you. These solutions manage everything from HTML template rendering to PDF delivery.

Conclusion

Playwright Python offers an advanced yet intuitive toolkit for generating PDFs from HTML, ensuring high-quality, pixel-perfect documents. Its growing popularity and robust feature set make it a prime choice for developers aiming to create precise, dynamic PDFs programmatically.

For teams seeking scalability without maintaining complex infrastructure or continuous PDF template management, leveraging specialized PDF APIs like pdforge can deliver significant efficiency gains.

Generating pdfs at scale can be annoying!

Generating pdfs at scale can be annoying!

Let us help you make it easier while you focus on what truly matters for your company.

Let us help you make it easier while you focus on what truly matters for your company.

7-day free trial

7-day free trial

Table of contents

Automate PDF Generation with pdforge

No code or design experience needed

AI creates your template in seconds

Fine tune the design in our no-code builder

Generate PDFs with our API or integrations