django-advanced-pdf
django-advanced-pdf is a Django app for building PDFs from XML templates (with Django template syntax), powered by ReportLab.
This repository contains:
- the reusable package:
django_advanced_pdf/ - a runnable Django demo project:
django_examples/
What this project does
You can generate PDFs by:
- Rendering XML stored in the database (
PrintingTemplate) - Rendering XML files from templates
- Rendering in background tasks (Celery helper included)
The XML supports rich layout features such as:
- tables, row/column spans, nested tables
- custom style blocks and inline style attributes
- page borders and page-level pager/header/footer blocks
- conditional/hidden rows/columns
- overflow handling for long cells
- embedded PNG/SVG and object injection
Quick start (Docker, easiest)
From repository root:
docker compose up --build
Then open:
http://localhost:8012/(example app)
The compose stack includes:
- Django app
- Postgres (
db_pdf) - Redis (
redis) - Celery worker
Local setup (without Docker)
1) Install dependencies
python -m venv .venv
source .venv/bin/activate
pip install --upgrade pip
pip install -r requirements.txt
2) Configure database/cache for the demo app
The example project settings are in:
django_examples/django_examples/settings.py
By default, the demo uses Postgres + Redis hostnames intended for Docker:
- DB host:
db_pdf - Redis host:
redison port6380
If running fully local, update these to your local services.
3) Run migrations and start server
cd django_examples
python manage.py migrate
python manage.py runserver 0.0.0.0:8012
4) (Optional) load sample data
python manage.py import_advanced_pdf
Integrate into your own Django project
1) Install package
pip install django-advanced-pdf
2) Add app
INSTALLED_APPS = [
# ...
"django_advanced_pdf",
]
3) Include URLs (for task PDF download/modal helpers)
from django.urls import include, path
urlpatterns = [
# ...
path("pdf/", include("django_advanced_pdf.urls", namespace="django_advanced_pdf")),
]
Common usage patterns
A) Render from a database template (PrintingTemplate)
from django_advanced_pdf.models import PrintingTemplate
template = PrintingTemplate.objects.get(name="invoice")
result = template.make_pdf(context={"invoice": invoice})
pdf_bytes = result["pdf_data"].getvalue()
has_xml_warnings = result["has_potential_xml_errors"]
B) Render XML directly with ReportXML
from django_advanced_pdf.engine.report_xml import ReportXML
xml = """
<document title="Example" page_size="A4">
<table>
<tr><td>Hello world</td></tr>
</table>
</document>
"""
report = ReportXML()
buffer = report.load_xml_and_make_pdf(xml)
pdf_bytes = buffer.getvalue()
C) Class-based view for database templates
Inherit from django_advanced_pdf.views.standard.DatabasePDFView and set the model instance.
XML template quick reference
Root element
<document
title="My report"
page_size="A4"
page_orientation="portrait"
page_style="borders"
border_top_first="10"
border_bottom_first="10"
border_left_first="10"
border_right_first="10">
...
</document>
Frequently used tags
<style>...</style>: define reusable classes<table>,<tr>,<td>: table layout<header>,<footer>inside table<keep>wrapper for “keep-with-next” row groups<p>: paragraph block<spacer style="height:10"><page_break/><obj ...>: inject Python objects fromobject_lookup<pagers><pager ...>...</pager></pagers>: page-level fixed blocks
Styling
Styles are CSS-like but mapped to ReportLab table/paragraph options. Examples seen in this repository:
inner_grid:0.25,#000000box:0.5,#000000text_color:#0000FFbackground:#F0F0F0line_below:0.5,#000000left_padding:1
Reference examples:
django_examples/advanced_pdf_examples/templates/file_examples/basic.xml.../border.xml.../pager.xml.../hidden.xml
Background task flow (Celery)
The helper base class is:
django_advanced_pdf.tasks.TaskProcessPDFHelper
Typical flow:
- build PDF in task (
build_pdf) - save bytes + filename to Django cache
- redirect user to
django_advanced_pdf:view_task_pdffor download - or open modal via
django_advanced_pdf:view_task_pdf_modal
Example task implementation:
django_examples/advanced_pdf_examples/tasks.py
Demo routes worth checking
From the demo project root URL:
/database templates demo/files/file-based XML demos/view/companies/context-driven companies PDF/report/example/merged report sample/report/headed-notepaper/background image example/tasks/<slug>/task/modal examples
Running tests
Package tests live in:
django_advanced_pdf/tests.py
Run:
python -m unittest django_advanced_pdf.tests
Troubleshooting
- Blank/failed output: check malformed XML and inspect
has_potential_xml_errors. - Task PDF not downloadable: verify cache backend + Redis connectivity.
- Demo app DB errors: ensure Postgres connection values match your environment.
- Missing static/demo assets: run from repo layout expected by
django_examples.
Demo video
An automated demo recording script is included at:
scripts/create_demo_video.py
It starts the Django example project with demo-friendly settings
(django_examples.settings_demo), walks through a few routes, and saves a
video to demo/django-advanced-pdf-demo.mp4.
Generate locally
pip3 install -r requirements.txt
pip3 install playwright
python3 -m playwright install chromium
python3 scripts/create_demo_video.py
License
MIT (see LICENSE).
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_advanced_pdf-0.2.23.tar.gz.
File metadata
- Download URL: django_advanced_pdf-0.2.23.tar.gz
- Upload date:
- Size: 4.2 MB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2cd28c64717c548b3ec508219d26e6d4c04856ba03e55d9c42abf21b24ed4519
|
|
| MD5 |
cd554b4e8fdb895dd8e8ece88ac6459e
|
|
| BLAKE2b-256 |
9b49e496de00d63d4b3c53c6ba19b6d14f67a2f618e74c106f2f8e968b41c3bd
|
Provenance
The following attestation bundles were made for django_advanced_pdf-0.2.23.tar.gz:
Publisher:
publish.yml on django-advance-utils/django-advanced-pdf
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_advanced_pdf-0.2.23.tar.gz -
Subject digest:
2cd28c64717c548b3ec508219d26e6d4c04856ba03e55d9c42abf21b24ed4519 - Sigstore transparency entry: 2655307134
- Sigstore integration time:
-
Permalink:
django-advance-utils/django-advanced-pdf@43a03c4b2072dffe6bddbedb02c94eb8bbdb8cd5 -
Branch / Tag:
refs/tags/v0.2.23 - Owner: https://github.com/django-advance-utils
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@43a03c4b2072dffe6bddbedb02c94eb8bbdb8cd5 -
Trigger Event:
release
-
Statement type:
File details
Details for the file django_advanced_pdf-0.2.23-py3-none-any.whl.
File metadata
- Download URL: django_advanced_pdf-0.2.23-py3-none-any.whl
- Upload date:
- Size: 4.3 MB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d3101b5b40646e81286b808747db2b31b441f006e34b2b90294527bb02246745
|
|
| MD5 |
fda84d7b847ccdd3a930cfde7895db4c
|
|
| BLAKE2b-256 |
a26abf28fd525468d69d9b3e1eb278803a56c173173469e03984f801eb491954
|
Provenance
The following attestation bundles were made for django_advanced_pdf-0.2.23-py3-none-any.whl:
Publisher:
publish.yml on django-advance-utils/django-advanced-pdf
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_advanced_pdf-0.2.23-py3-none-any.whl -
Subject digest:
d3101b5b40646e81286b808747db2b31b441f006e34b2b90294527bb02246745 - Sigstore transparency entry: 2655307167
- Sigstore integration time:
-
Permalink:
django-advance-utils/django-advanced-pdf@43a03c4b2072dffe6bddbedb02c94eb8bbdb8cd5 -
Branch / Tag:
refs/tags/v0.2.23 - Owner: https://github.com/django-advance-utils
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@43a03c4b2072dffe6bddbedb02c94eb8bbdb8cd5 -
Trigger Event:
release
-
Statement type: