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 WeasyTemplateResponse class MyDetailView(DetailView): # vanilla Django DetailView 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 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' 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'), )
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-2.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ba3822b93849b3ea9d70b618e1ebfee8dd7bd5f0fa170f5b71dc6ccceb48d772 |
|
MD5 | ccbbef242db80b2e81e12a7b3bd07f05 |
|
BLAKE2-256 | bb5171ea5ba67b5cb0f447b83905464a629bfc60bf7fa38d624d5936d243e46d |