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-weasyprintVerified import paths — ran on the pinned version, not inferred.
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.
Update custom URL fetchers to implement the new `WeasyPrint.urlfetcher.URLFetcher` API and use `from django_weasyprint.urlfetcher import DjangoURLFetcher`.
Ensure your project uses Python >= 3.10 and WeasyPrint >= 68. Upgrade WeasyPrint if necessary (e.g., `pip install -U WeasyPrint`).
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.
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).
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.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.
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.
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`.
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.