Skip to main content

Django WeasyPrint CBV

Project description

A Django class-based view generating PDF responses using WeasyPrint.

TravisCI Build PyPI Download PyPI Python Versions PyPI License


Install and update using pip:

pip install -U django-weasyprint

WeasyPrint is automatically installed as a dependency of this package. If you run into any problems be sure to check their install instructions for help!


In version 53 WeasyPrint switched to pydyf as PDF generator instead of Cairo. With that change PNG output was dropped and you might encounter other changes in the generated PDF.

You can continue using WeasyPrint/Cairo by installing django-weasyprint 1.x!


Use WeasyTemplateView as class based view base class or the just the mixin WeasyTemplateResponseMixin on a TemplateView (or subclass thereof).


import functools

from django.conf import settings
from django.views.generic import DetailView

from django_weasyprint import WeasyTemplateResponseMixin
from django_weasyprint.views import WeasyTemplateResponse
from django_weasyprint.utils import django_url_fetcher

class MyDetailView(DetailView):
    # vanilla Django DetailView
    template_name = 'mymodel.html'

def custom_url_fetcher(url, *args, **kwargs):
    # rewrite requests for CDN URLs to file path in STATIC_ROOT to use local file
    cloud_storage_url = ''
    if url.startswith(cloud_storage_url):
        url = 'file://' + url.replace(cloud_storage_url, settings.STATIC_URL)
    return django_url_fetcher(url, *args, **kwargs)

class CustomWeasyTemplateResponse(WeasyTemplateResponse):
    # customized response class to pass a kwarg to URL fetcher
    def get_url_fetcher(self):
        # disable host and certificate check
        context = ssl.create_default_context()
        context.check_hostname = False
        context.verify_mode = ssl.CERT_NONE
        return functools.partial(custom_url_fetcher, ssl_context=context)

class PrintView(WeasyTemplateResponseMixin, MyDetailView):
    # output of MyDetailView rendered as PDF with hardcoded CSS
    pdf_stylesheets = [
        settings.STATIC_ROOT + 'css/app.css',
    # show pdf in-line (default: True, show download dialog)
    pdf_attachment = False
    # custom response class to configure url-fetcher
    response_class = CustomWeasyTemplateResponse

class DownloadView(WeasyTemplateResponseMixin, MyDetailView):
    # suggested filename (is required for attachment/download!)
    pdf_filename = 'foo.pdf'

class DynamicNameView(WeasyTemplateResponseMixin, MyDetailView):
    # dynamically generate filename
    def get_pdf_filename(self):
        return 'foo-{at}.pdf'.format(
<!-- mymodel.html -->
<!doctype html>
        <!-- Use "static" template tag and configure STATIC_URL as usual. -->
        <link rel="stylesheet" href="{% static 'css/app.css' %}" />
        Hello PDF-world!


By default WeasyTemplateResponse determines the base_url for weasyprint.HTML and weasyprint.CSS automatically using the request path.

To disable that set WEASYPRINT_BASEURL to a fixed value, e.g.:

# Disable prefixing relative URLs with request.path, handle as absolute file paths



Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-weasyprint-2.2.0.tar.gz (11.9 kB view hashes)

Uploaded source

Built Distribution

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page