Skip to main content

A Django app for rendering PDF documents with WeasyPrint

Project description

Django BootyPrint

A Django app for rendering PDF documents with WeasyPrint and BootyPrint.

Installation

pip install django-bootyprint
# or with uv
uv add django-bootyprint

Requirements

  • Python 3.13+
  • Django 5.1.7+
  • WeasyPrint 65.0+

Quick Start

  1. Add 'bootyprint' to your INSTALLED_APPS setting:
INSTALLED_APPS = [
    ...
    'bootyprint',
    ...
]
  1. Customize settings in your settings.py (optional):

See WeasyPrint PDF generation options for the options in PDF_OPTIONS

BOOTYPRINT = {
    'DEFAULT_TEMPLATE': 'myapp/my_template.html',  # Override default template
    'PDF_OPTIONS': {
        'media_type': 'print',
        'pdf_identifier': None,
        'pdf_variant': None,
        'pdf_version': None,
        'pdf_forms': False,
        'uncompressed_pdf': False,
        'custom_metadata': False,
        'srgb': True,
        'optimize_images': True,
        'jpeg_quality': 95,
        'presentational_hints': False,
        'dpi': 96,
        'full_fonts': True,
        'hinting': True,
    },
    'CACHE_ENABLED': True,      # Enable caching (default: True)
    'CACHE_TIMEOUT': 3600,      # Cache timeout in seconds (default: 86400 - 24 hours)
}

Usage

Basic Usage

from bootyprint.utils import generate_pdf
from django.http import HttpResponse

def my_pdf_view(request):
    # Generate PDF from template
    context = {'title': 'My Document', 'content': 'Hello World'}
    pdf_content = generate_pdf(
        template_name='myapp/my_template.html',
        context=context,
        encoding='utf-8'
    )

    # Return as response
    response = HttpResponse(pdf_content, content_type='application/pdf')
    response['Content-Disposition'] = 'attachment; filename="my_document.pdf"'
    return response

Using PDFResponse

from bootyprint.views import PDFResponse
from bootyprint.utils import generate_pdf

def my_pdf_view(request):
    context = {'title': 'My Document', 'content': 'Hello World'}
    pdf_content = generate_pdf(
        template_name='myapp/my_template.html',
        context=context
    )

    return PDFResponse(pdf_content, filename='my_document.pdf')

Using PDFTemplateResponse

from django.views.generic import DetailView
from bootyprint.views import PDFTemplateResponse

def my_pdf_view(request):
    context = {'title': 'My Document', 'content': 'Hello World'}

    return PDFTemplateResponse(
        request=request,
        template='myapp/my_template.html',
        context=context,
        filename='my_document.pdf'
    )

class MyPDFView(DetailView):
    model = MyModel
    template_name = "myapp/my_template.html"
    response_class = PDFTemplateResponse

Template Tags

Load BootyPrint

The latest BootyPrint is included in this library. The bootyprint_css template tag loads it into the html template.

{% load bootyprint %}
<head>
    {% bootyprint_css %}
    <style>
        :root {
            --primary: #3f51b5;
            --secondary: #2196f3;
            --font-size-base: 11px;
            --page-size: A4;
            --page-margin: 10mm;
        }
    </style>
</head>

Local paths to static files

Usually it's the easiest way to provide local, absolute file paths to resources to WeasyPrint. The template tag local_static is like django's static tag, but returns a local path.

{% load bootyprint %}
<img src="{% local_static "img/default_avatar" %}" alt="My Profile Image">

Templates

Bootyprint comes with a default template (bootyprint/default.html) that you can extend or override. Your templates should be valid HTML that WeasyPrint can render to PDF.

Example template:

{% extends "bootyprint/default.html" %}

{% block content %}
<h1>{{ title }}</h1>
<p>{{ content }}</p>

<table>
    <thead>
        <tr>
            <th>Item</th>
            <th>Price</th>
        </tr>
    </thead>
    <tbody>
        {% for item in items %}
        <tr>
            <td>{{ item.name }}</td>
            <td>${{ item.price }}</td>
        </tr>
        {% endfor %}
    </tbody>
</table>
{% endblock %}

{% block footer %}
    Generated on {{ generation_date|date:"F j, Y" }}
{% endblock %}

License

MIT

Project details


Download files

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

Source Distribution

django_bootyprint-0.0.3.tar.gz (72.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_bootyprint-0.0.3-py3-none-any.whl (71.0 kB view details)

Uploaded Python 3

File details

Details for the file django_bootyprint-0.0.3.tar.gz.

File metadata

  • Download URL: django_bootyprint-0.0.3.tar.gz
  • Upload date:
  • Size: 72.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for django_bootyprint-0.0.3.tar.gz
Algorithm Hash digest
SHA256 553800ddf8dd3bbb973d01f2638db0c46c8490bdf77220308dd3b9b026db61f8
MD5 4b9d71bbb090811e6ef24f56f78541fd
BLAKE2b-256 a69da7650fab4ff89b0f146bc7143f020a87633c40a4937131ae9bd8f02d5448

See more details on using hashes here.

File details

Details for the file django_bootyprint-0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for django_bootyprint-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 fda8d00c9eb05ae3f63b2a72eb99ca136c9207bb91b738f9d0d369fd541b5737
MD5 3031cae26f38af785f867e9ea86c4016
BLAKE2b-256 cc5931618b07b46e3d3d0f69c35871c526c81d2b3671714e3f14b925f9cc6e47

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page