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.

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):
BOOTYPRINT = {
    'DEFAULT_TEMPLATE': 'myapp/my_template.html',  # Override default template
    'PDF_OPTIONS': {
        'page_size': 'Letter',  # Change from default A4
        'margin_top': '1in',    # Custom margins
        'margin_right': '1in',
        'margin_bottom': '1in',
        'margin_left': '1in',
    },
    '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
    )

    # 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 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'
    )

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.1.tar.gz (6.4 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.1-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_bootyprint-0.0.1.tar.gz
  • Upload date:
  • Size: 6.4 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.1.tar.gz
Algorithm Hash digest
SHA256 7b88071e6d0f296e1c6d0785f9ffbb8800f61d99e153936202ba95e0ee044d28
MD5 5c1f1b6e7e8fac90604d35020492d6ad
BLAKE2b-256 b2c725975d32cd4d3da193d92aff75ecb8269931131d59db5348760f3ff4c465

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_bootyprint-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2948eb5c81938c56db8866cd487bd65a570c3cb3b81018eedbea859b336f7c2a
MD5 93aede39f5ac4604d453e25643b83298
BLAKE2b-256 844d72315f65297d28a28a3fa2439c69b12371123da56859a49290e5699b9b95

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