Django WeasyPrint CBV
Project description
A Django class-based view generating PDF responses using WeasyPrint.
Installing
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 just the mixin WeasyTemplateResponseMixin on a TemplateView (or subclass thereof).
Example
import functools
from django.conf import settings
from django.views.generic import DetailView
from django_weasyprint import WeasyTemplateResponseMixin
from django_weasyprint.views import CONTENT_TYPE_PNG, WeasyTemplateResponse
class MyModelView(DetailView):
# vanilla Django DetailView
model = MyModel
template_name = 'mymodel.html'
class CustomWeasyTemplateResponse(WeasyTemplateResponse):
# customized response class to change the default 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(django_url_fetcher, ssl_context=context)
class MyModelPrintView(WeasyTemplateResponseMixin, MyModelView):
# output of MyModelView 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 MyModelDownloadView(WeasyTemplateResponseMixin, MyModelView):
# suggested filename (is required for attachment/download!)
pdf_filename = 'foo.pdf'
class MyModelImageView(WeasyTemplateResponseMixin, MyModelView):
# generate a PNG image instead
content_type = CONTENT_TYPE_PNG
# dynamically generate filename
def get_pdf_filename(self):
return 'foo-{at}.pdf'.format(
at=timezone.now().strftime('%Y%m%d-%H%M'),
)
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
Close
Hashes for django-weasyprint-1.1.0.post1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | e73b51aaea4322dcbcbfb9b06d5c5da49bdd349c25e608e9037a9f2cef71ca31 |
|
MD5 | b74d4e027ac1d5878ece909619e5a8b4 |
|
BLAKE2b-256 | 62a244b22b816c69408b5e5d063772da55b74ccff4c79971503135d605d241a6 |
Close
Hashes for django_weasyprint-1.1.0.post1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 68fa9658e81d0019a304836accb2b993cc746ad86d7a90bc2dd2be023a6804a5 |
|
MD5 | 87e42b3bc6cf90093141311ddf3949ab |
|
BLAKE2b-256 | a157b9db73932fad07259eacf5cc4910b638fd4fe5d1df5db6f9995b9adbb7a7 |