Package: django-weasyprint
ADjangoclass-based view generating PDF responses usingWeasyPrint.InstallationInstall and update usingpip:pip install -U django-weasyprintWeasyPrintis automatically installed as a dependency of this package.
If you run into any problems be sure to check theirinstall instructionsfor help!TipIn version 53 WeasyPrint switched topydyfas PDF generator instead of Cairo.
With that change PNG output was dropped and you might encounter other
changes in the generated PDF.You can continue using WeasyPrint/Cairo by installing django-weasyprint 1.x!UsageUseWeasyTemplateViewas class based view base class or the
mixinWeasyTemplateResponseMixinon aTemplateView(or subclass
thereof).Example# views.pyimportfunctoolsfromdjango.confimportsettingsfromdjango.views.genericimportDetailViewfromdjango_weasyprintimportWeasyTemplateResponseMixinfromdjango_weasyprint.viewsimportWeasyTemplateResponsefromdjango_weasyprint.utilsimportdjango_url_fetcherclassMyDetailView(DetailView):# vanilla Django DetailViewtemplate_name='mymodel.html'defcustom_url_fetcher(url,*args,**kwargs):# rewrite requests for CDN URLs to file path in STATIC_ROOT to use local filecloud_storage_url='https://s3.amazonaws.com/django-weasyprint/static/'ifurl.startswith(cloud_storage_url):url='file://'+url.replace(cloud_storage_url,settings.STATIC_URL)returndjango_url_fetcher(url,*args,**kwargs)classCustomWeasyTemplateResponse(WeasyTemplateResponse):# customized response class to pass a kwarg to URL fetcherdefget_url_fetcher(self):# disable host and certificate checkcontext=ssl.create_default_context()context.check_hostname=Falsecontext.verify_mode=ssl.CERT_NONEreturnfunctools.partial(custom_url_fetcher,ssl_context=context)classPrintView(WeasyTemplateResponseMixin,MyDetailView):# output of MyDetailView rendered as PDF with hardcoded CSSpdf_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-fetcherresponse_class=CustomWeasyTemplateResponseclassDownloadView(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'}classDynamicNameView(WeasyTemplateResponseMixin,MyDetailView):# dynamically generate filenamedefget_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. --><linkrel="stylesheet"href="{% static 'css/app.css' %}"/></head><body>Hello PDF-world!</body></html>SettingsBy defaultWeasyTemplateResponsedetermines thebase_urlforweasyprint.HTMLandweasyprint.CSSautomatically using Django’srequest.build_absolute_uri().To disable that setWEASYPRINT_BASEURLto a fixed value, e.g.:# Disable prefixing relative URLs with request.build_absolute_uri().# Instead, handle them as absolute file paths.WEASYPRINT_BASEURL='/'ChangelogSeeCHANGELOG.mdLinksReleases:https://pypi.python.org/pypi/django-weasyprintIssue tracker:https://github.com/fdemmer/django-weasyprint/issuesCode:https://github.com/fdemmer/django-weasyprint