Last active
October 10, 2016 09:13
-
-
Save mattmcc/fe050fcf3a6ee4d35182f9ced02b4acb to your computer and use it in GitHub Desktop.
Django PDFResponse using weasyprint
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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