Registry / web-framework / django-weasyprint

django-weasyprint

JSON →
library2.5.0pypypi✓ verified 86d ago

django-weasyprint is a Django application that integrates WeasyPrint, allowing developers to generate PDF documents from HTML and CSS templates within their Django projects. It provides class-based views and response classes to streamline the process of converting rendered Django templates into PDF files. The current version is 2.5.0, with a release cadence tied to major WeasyPrint and Django releases, supporting modern Python and Django versions.

pip install django-weasyprint
INSTALL
IMPORT
SIG · DJANGO-WEASYPRINT
D
django-weasyprint
web-frameworkpythonv2.5.0
Install
6.2s avg
Import
Disk
130MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.5.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.915 runs
installs and imports cleanly · install 0.0s · import 0.000s · 128.6MB
glibc
py 3.103.915 runs
installs and imports cleanly · install 6.2s · import 0.000s · 130MB
130MB installed
● package 130MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

WeasyTemplateResponseMixin
from django_weasyprint.views import WeasyTemplateResponseMixin
WeasyTemplateResponse
from django_weasyprint.views import WeasyTemplateResponse
DjangoURLFetcher
from django_weasyprint.urlfetcher import DjangoURLFetcher
from django_weasyprint.utils import django_url_fetcher
The function `django_url_fetcher` was replaced by the `DjangoURLFetcher` class in v2.5.0 to align with WeasyPrint 68's API changes.

This quickstart demonstrates how to create a basic PDF view for a Django model using `django-weasyprint`. It defines a `Product` model, a `ProductDetailView` for regular HTML display, and a `ProductPDFView` that extends it with `WeasyTemplateResponseMixin` to render the same content as a PDF. Ensure `STATIC_ROOT` and `STATIC_URL` are configured in your Django settings for CSS to be applied correctly, and that WeasyPrint's system dependencies are installed.

import os from django.db import models from django.shortcuts import render from django.urls import path from django.views.generic import DetailView from django_weasyprint.views import WeasyTemplateResponseMixin, WeasyTemplateResponse # models.py (example) class Product(models.Model): name = models.CharField(max_length=255) description = models.TextField() price = models.DecimalField(max_digits=10, decimal_places=2) def __str__(self): return self.name # views.py class ProductDetailView(DetailView): model = Product template_name = 'products/product_detail.html' context_object_name = 'product' class ProductPDFView(WeasyTemplateResponseMixin, ProductDetailView): # PDF-specific settings pdf_stylesheets = [ os.path.join(os.environ.get('STATIC_ROOT', ''), 'css/pdf_styles.css') ] pdf_attachment = True # Forces download pdf_filename = 'product_details.pdf' # urls.py (in your app) # urlpatterns = [ # path('product/<int:pk>/', ProductDetailView.as_view(), name='product_detail'), # path('product/<int:pk>/pdf/', ProductPDFView.as_view(), name='product_pdf'), # ] # products/product_detail.html (example template) # In your template directory: # <!DOCTYPE html> # <html> # <head> # <title>{{ product.name }} PDF</title> # <style> # @page { size: A4; margin: 2cm; } # body { font-family: sans-serif; } # h1 { color: #333; } # .price { color: green; font-weight: bold; } # </style> # <link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}css/pdf_styles.css"> # </head> # <body> # <h1>{{ product.name }}</h1> # <p>{{ product.description }}</p> # <p class="price">Price: ${{ product.price }}</p> # </body> # </html> # settings.py (important for static files) # STATIC_URL = '/static/' # STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
Debug
Known issues
breakingVersion 2.5.0 replaced the `django_url_fetcher` function with a `DjangoURLFetcher` class to align with WeasyPrint 68's URLFetcher API. Existing custom URL fetcher implementations using the old function signature will break.
fix
Update custom URL fetchers to implement the new `WeasyPrint.urlfetcher.URLFetcher` API and use `from django_weasyprint.urlfetcher import DjangoURLFetcher`.
affects: 2.5.0 and later
breakingVersion 2.5.0 dropped support for WeasyPrint versions older than 68 and Python versions 3.8 and 3.9.
fix
Ensure your project uses Python >= 3.10 and WeasyPrint >= 68. Upgrade WeasyPrint if necessary (e.g., `pip install -U WeasyPrint`).
affects: 2.5.0 and later
breakingVersion 2.0.0 required WeasyPrint >= 53.0 and removed `PNGView` and `CONTENT_TYPE_*` constants. This means direct PNG output is no longer supported through `django-weasyprint`.
fix
Upgrade WeasyPrint to version 53.0 or higher. If PNG output is required, consider using an older `django-weasyprint` 1.x release (which uses WeasyPrint/Cairo) or an alternative library for PNG generation.
affects: 2.0.0 and later
gotchaWeasyPrint, the underlying rendering engine, requires external system libraries (like Pango, Cairo, and their GTK+ dependencies) to be installed on the operating system, not just via pip. This is a common source of installation issues, especially on Windows or macOS.
fix
Refer to the official WeasyPrint installation documentation (e.g., `https://weasyprint.org/docs/install/`) for detailed, OS-specific instructions on installing these system dependencies (e.g., using Homebrew on macOS, `apt` on Ubuntu, or MSYS2/GTK+ runtime on Windows).
affects: All versions
gotchaImages and CSS stylesheets referenced with relative paths or Django's static/media URLs might not render in the generated PDF unless the `base_url` is correctly configured or a custom URL fetcher is used. WeasyPrint often requires absolute paths or URLs.
fix
Set `pdf_baseurl` in your `WeasyTemplateResponseMixin` or `WeasyTemplateResponse` to `request.build_absolute_uri('/')` to resolve relative URLs. For static files, ensure `STATIC_URL` is correctly configured and accessible, or implement a `DjangoURLFetcher` subclass to resolve them from disk.
affects: All versions
Errors
Common errors & fixes
OSError: dlopen() failed to load a library: cairo / cairo-2
WeasyPrint depends on system-level libraries like Cairo and Pango which are not installed via Python's pip. This error typically occurs on Windows, macOS, or Linux if these graphical dependencies are missing or incorrectly configured.
fix
Follow the official WeasyPrint installation guide for your operating system to install the necessary system dependencies (e.g., GTK+ runtime on Windows, Homebrew for macOS, `apt` packages on Linux). Ensure their paths are correctly set in system environment variables if required.
Fontconfig error: Cannot load default config file
WeasyPrint uses Fontconfig to manage fonts. This error indicates that Fontconfig cannot find its configuration file, often due to missing or improperly configured system font packages or environment variables, especially in headless environments or on Windows.
fix
Install `fontconfig` system packages (e.g., `fontconfig` on Linux). On Windows, ensure GTK+ runtime is installed and its `etc/fonts` directory is accessible, or manually configure a `fonts.conf` file and set the `FONTCONFIG_PATH` environment variable.
NameError: name 'django_url_fetcher' is not defined
Attempting to use the old `django_url_fetcher` function after upgrading `django-weasyprint` to version 2.5.0 or later. This function was removed and replaced by a class.
fix
Replace calls to `django_url_fetcher` with an instance of `DjangoURLFetcher` or a custom class that implements the `WeasyPrint.urlfetcher.URLFetcher` API. Import it using `from django_weasyprint.urlfetcher import DjangoURLFetcher`.
PDF output not showing images or CSS styles
WeasyPrint is unable to resolve the URLs for static assets (images, stylesheets) referenced in your HTML template. This often happens because relative URLs or Django's static URL patterns are not correctly resolved to absolute paths or accessible URLs during PDF generation.
fix
Set `pdf_baseurl = request.build_absolute_uri('/')` in your `WeasyTemplateResponseMixin` to ensure relative URLs are resolved correctly. For Django static files, ensure `STATIC_URL` is properly configured, `STATIC_ROOT` is collected, and consider implementing a custom `DjangoURLFetcher` if static files are not served via HTTP.
Upgrade
Version history
2.5.0latest on PyPI · released Apr 10, 2026
Audit
Dependencies
DjangorequiredWeb framework integration
WeasyPrintrequiredCore HTML to PDF rendering engine. Requires system dependencies like Pango and GTK+ on the host OS.
Agent activity
4 hits · last 30 days
node
4
Resources
django-weasyprint — pip install django-weasyprint · libregistry