Free Python WeasyPrint Templates & Print-CSS Generator
Renders HTML5 + @page CSS Paged Media in Python using native Pango, HarfBuzz, and Cairo instead of headless Chrome. Generates single-page invoices in 144 ms (37.5 KB) and our dense 18-page 26-component custom-TTF/AcroForm stress test in 4.34s (1,059.8 KB).
Install: pip install weasyprint==63.1 | Runtime: Python (Python 3 + CSS) | Version: 63.1 | Popularity: 3.6M/mo installs
Works with: Django, FastAPI, Flask, Jinja2
How WeasyPrint (Python) Templates Work in CodePDF
Generates a self-contained Python 3 module that formats dynamic HTML + @page CSS and renders print-grade PDFs via weasyprint.HTML(string=...).write_pdf().
- Full CSS @page margin, web font, and modern Flexbox support in Python without running Chromium
- Self-contained Python function ready for Django views, FastAPI endpoints, or Jinja2 pipelines
- Switch between WeasyPrint, ReportLab, and fpdf2 from the exact same visual template
Quick Start: Generate PDFs with WeasyPrint
Install WeasyPrint (Python) (Python 3 + CSS), then run the code CodePDF exports for any template:
pip install weasyprint==63.1
- Install the native dependencies (Pango, cairo, GDK-PixBuf) for your OS first.
- Save the generated file as my_document.py.
- Run python my_document.py to render the HTML + CSS to PDF.
- Use @page CSS rules to control size, margins and headers.
Example WeasyPrint Output (Standard B2B Commercial Invoice)
Compiled directly from the Standard B2B Commercial Invoice template (Python 3 + CSS):
from html import escape as _html_escape
import re
import ipaddress
import os
import urllib.parse
import urllib.request
from weasyprint import HTML, default_url_fetcher
# This code is generated using codepdf.dev
TEMPLATE = """\
<!DOCTYPE html>
<!-- This code is generated using codepdf.dev -->
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Standard B2B Commercial Invoice</title>
<style>
* { box-sizing: border-box; }
body, p, h1, h2, h3, h4, h5, h6, ul, ol, li { margin: 0; padding: 0; }
body { font-family: Helvetica, Arial, sans-serif; background: #f3f4f6; }
@page { size: A4 portrait; margin: 0; }
.pdf-page { position: relative; display: flex; flex-direction: column; width: 210mm; min-height: 297mm; margin: 0 auto 16px; background: #fff; overflow: hidden; }
.pdf-page + .pdf-page { page-break-before: always; }
.pdf-list li { break-inside: avoid; page-break-inside: avoid; }
.pdf-text-flow { white-space: pre-wrap; overflow-wrap: break-word; }
@media print { .pdf-page { display: block; overflow: visible; margin: 0; box-shadow: none; page-break-inside: auto; } .pdf-list { display: block; }
.pdf-page > *:not(.pdf-page-bg):not(.pdf-hf-band):not(.pdf-hf-spacer):not(.pdf-page-number) + *:not(.pdf-page-bg):not(.pdf-hf-band):not(.pdf-hf-spacer):not(.pdf-page-number) { margin-top: 8pt; } }
// … (367 more lines in full generated file)
Custom Fonts in WeasyPrint
Load custom TTF, OTF, or WOFF2 fonts cleanly via CSS @font-face in WeasyPrint.
- Declare @font-face in your CSS with file:// paths or remote HTTPS URLs.
- Pass base_url='.' to HTML(string=html, base_url='.') when referencing relative ./fonts/ paths.
- Match font-weight and font-style values on @font-face and element selectors.
Official documentation: WeasyPrint docs
WeasyPrint Benchmark Results
Measured on CodePDF’s 26-component, 18-page All-Components Dynamic Multi-Page Flow Stress Test (same document across all 22 libraries).
| Metric | WeasyPrint | Rank (of 22) |
|---|---|---|
| Wall-clock time | 4.34 s | 22nd fastest |
| CPU time | 4.74 s | 22nd lowest |
| Peak RAM | 188.2 MB | 17th lowest |
| PDF file size | 1059.8 KB | 22nd smallest |
- Engine type
- CSS-to-PDF
- Under the hood
- Python HTML/CSS Paged Media engine backed by native Pango, HarfBuzz & Cairo libraries
- Runs in
- Python 3 Server / Docker (requires native Pango/Cairo OS libs)
- Verdict
- Modern CSS Paged Media without Chromium via Pango/Cairo: 144 ms single-page, 4.34s & 1,059.8 KB across 18 dense overflowing pages.
Under the Hood & Benchmark Highlights
- Renders real CSS Paged Media (@page rules, Flexbox, web fonts) natively via Pango, HarfBuzz, and Cairo — without spawning headless Chrome.
- Major stress-test gap: on our dense 26-component overflowing document, WeasyPrint took 80.8s (79.3s CPU) and produced a 1,022.2 KB PDF — the slowest and largest of all 22 libraries.
- Best reserved for editorial/print-CSS documents with moderate element counts rather than high-throughput multi-page transaction batches.
Good to Know (WeasyPrint Caveats)
- Requires native OS libraries: Depends on Pango, Cairo, and GDK-PixBuf at the OS level — straightforward in Debian/Ubuntu Docker images, but requires extra setup on Windows, macOS (Homebrew), or AWS Lambda layers.
- Higher layout cost on multi-page overflow: Full CSS Paged Media typesetting took 144 ms on a 1-page invoice and 4.34s (1,059.8 KB PDF) on our dense 18-page 26-component overflow suite — slower and larger than direct Python writers like ReportLab (170 ms) or fpdf2 (389 ms).
- Avoid multi-page vertical flex containers & set base_url: Deep vertical Flexbox containers can trigger costly min-content re-layouts across page breaks (CodePDF automatically converts column containers to block flow for WeasyPrint), and pass base_url='.' when loading local assets.
Popular WeasyPrint PDF Templates
- Standard B2B Commercial Invoice WeasyPrint template — Enterprise commercial tax invoice with QR payment link, Currency balance KPIs, line-item ledger, and ACH wire instructions.
- Modern Resume WeasyPrint template — Two-column executive resume with dark slate sidebar, impact KPI strip, skill pills, and portfolio QR.
- Hospital Discharge Summary & Care Plan WeasyPrint template — Inpatient transition-of-care summary featuring hospital course, repeating home medication regimen cards, and red-flag warnings.
- Customer Success & Impact Case Study WeasyPrint template — Quantified B2B ROI case study featuring 4 impact KPI cards, Before/After architecture columns, and executive pull-quote.
- Official University Academic Transcript WeasyPrint template — Certified university registrar transcript with Code-128 serial barcode, Latin honors pills, coursework ledger, and FERPA seal.
- Project Proposal WeasyPrint template — Enterprise project proposal with executive summary, deliverables, timeline and budget table.
All 100 CodePDF templates export to WeasyPrint (Python). Browse them by category: Career (5) · Business (6) · Education (7) · Marketing (6) · Human Resources (8) · Finance (7) · Legal (8) · Healthcare (10) · Logistics (6) · Real Estate (6) · Hospitality (4) · Operations (5) · Technology (5) · Banking (9) · Non-Profit (4) · Personal (3) · Compliance & Enterprise (1), or see the full template gallery.
WeasyPrint Template FAQs
When should I choose WeasyPrint vs ReportLab in Python?
Choose WeasyPrint when you prefer HTML/CSS styling and rich web typography; choose ReportLab or fpdf2 when you need sub-200ms execution speed and zero Pango/Cairo system packages.
Can I integrate these WeasyPrint templates with Jinja2 or Django templates?
Yes. You can either call the generated Python function directly with a dict or extract the embedded HTML/CSS template for Jinja2.
Are page sizes and @page margins pre-configured for WeasyPrint?
Yes. Every template sets explicit @page { size: A4/Letter; margin: ... } rules matched to the visual canvas.
WeasyPrint vs Other Python PDF Libraries
- ReportLab (Python) — 170 ms, 46.4 MB RAM, 179.5 KB PDF. Fastest & lowest-RAM Python engine (170 ms for 22p, 1.5 ms single-page, 46.4 MB RAM, 0.29s CPU) with BSD-3-Clause license.
- fpdf2 (Python) — 389 ms, 81.8 MB RAM, 135 KB PDF. Smallest-PDF Python engine (135.0 KB across 22p; 2.0 KB / 3.5 ms single-page) in 389 ms with 100% pure Python (zero C libs; LGPL-3.0).
- xhtml2pdf (Python) — 2.06 s, 159.6 MB RAM, 139 KB PDF. Pip-only pure-Python HTML/CSS 2.1 on ReportLab (61 ms single-page; 2.06s & 139.0 KB across 23p) — zero Pango/Cairo OS dependencies.
See the full 22-library benchmark rankings · Compare all supported PDF libraries