Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mattmcc
Last active October 10, 2016 09:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattmcc/fe050fcf3a6ee4d35182f9ced02b4acb to your computer and use it in GitHub Desktop.
Save mattmcc/fe050fcf3a6ee4d35182f9ced02b4acb to your computer and use it in GitHub Desktop.
Django PDFResponse using weasyprint
from django.template.response import TemplateResponse
from django.views.generic import DetailView
from weasyprint import HTML
class PDFResponse(TemplateResponse):
@property
def rendered_content(self):
template = self.resolve_template(self.template_name)
context = self.resolve_context(self.context_data)
content = template.render(context)
content = HTML(string=content, base_url=self._request.build_absolute_uri('/')).write_pdf()
return content
class DetailPDFView(DetailView):
content_type = 'application/pdf'
response_class = PDFResponse
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment