Django WeasyPrint CBV
Project description
A Django class-based view generating PDF responses using WeasyPrint.
Installation
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!
Usage
Use WeasyTemplateView as class based view base class or the mixin WeasyTemplateResponseMixin on a TemplateView (or subclass thereof).
Example
# views.py
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 = 'https://s3.amazonaws.com/django-weasyprint/static/'
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'
# set PDF variant to 'pdf/ua-1' (see weasyprint.DEFAULT_OPTIONS)
pdf_options = {'pdf_variant': 'pdf/ua-1'}
class DynamicNameView(WeasyTemplateResponseMixin, MyDetailView):
# dynamically generate filename
def get_pdf_filename(self):
return 'foo-{at}.pdf'.format(
at=timezone.now().strftime('%Y%m%d-%H%M'),
)
<!-- mymodel.html -->
<!doctype html>
<html>
<head>
<!-- Use "static" template tag and configure STATIC_URL as usual. -->
<link rel="stylesheet" href="{% static 'css/app.css' %}" />
</head>
<body>
Hello PDF-world!
</body>
</html>
Settings
By default WeasyTemplateResponse determines the base_url for weasyprint.HTML and weasyprint.CSS automatically using Django’s request.build_absolute_uri().
To disable that set WEASYPRINT_BASEURL to a fixed value, e.g.:
# Disable prefixing relative URLs with request.build_absolute_uri().
# Instead, handle them as absolute file paths.
WEASYPRINT_BASEURL = '/'
Changelog
See CHANGELOG.md
Links
Project details
Release history Release notifications | RSS feed
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
File details
Details for the file django-weasyprint-2.3.0.tar.gz
.
File metadata
- Download URL: django-weasyprint-2.3.0.tar.gz
- Upload date:
- Size: 13.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2f849e15bfd6c1b2a58512097b9042eddf3533651d37d2e096cd6f7d8be6442b |
|
MD5 | df4e75bf6f103d2e9d3ba18626732675 |
|
BLAKE2b-256 | 2f5c27aad07b07fb85a4273c4a49b64a10571a0e47e3029a3f1c5f67a28d7e66 |
File details
Details for the file django_weasyprint-2.3.0-py3-none-any.whl
.
File metadata
- Download URL: django_weasyprint-2.3.0-py3-none-any.whl
- Upload date:
- Size: 10.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 807cb3b16332123d97c8bbe2ac9c70286103fe353235351803ffd33b67284735 |
|
MD5 | fc0e3e40679c591599afe9a365bbcfac |
|
BLAKE2b-256 | 058ef2f3e038c74dbdd514261268b7a645180cdb23a7a35d6c9938014622dc6d |