Usage¶
The PDFTemplateView is a Django class-based view.
By default, it uses PDFTemplateResponse to render an HTML
template to PDF.
It accepts the following class attributes:
template_name- The full name of a template to use as the body of the PDF.
header_template- Optional. The full name of a template to use as the header on each page.
footer_template- Optional. The full name of a template to use as the footer on each page.
filenameThe filename to use when responding with an attachment containing the PDF. Default is
'rendered_pdf.pdf'.If
None, the view returns the PDF output inline, not as an attachment.response_class- The response class to be returned by
render_to_response()method. Default isPDFTemplateResponse. html_response_class- The response class to be returned by
render_to_response()method, when rendering as HTML. See note below. Default isTemplateResponse. cmd_optionsThe dictionary of command-line arguments passed to the underlying
puppeteer-pdfbinary. Default is{}.puppeteer-pdf options can be found by running
puppeteer-pdf --help. Unfortunately they don’t provide hosted documentation.
Note
For convenience in development you can add the GET arg ?as=html to the
end of your URL to render the PDF as a web page.
Simple Example¶
Point a URL at PDFTemplateView:
from django.conf.urls.defaults import *
from puppeteer_pdf.views import PDFTemplateView
urlpatterns = patterns('',
# ...
url(r'^pdf/$', PDFTemplateView.as_view(template_name='my_template.html',
filename='my_pdf.pdf'), name='pdf'),
# ...
)
Advanced Example¶
Point a URL (as above) at your own view that subclasses
PDFTemplateView
and override the sections you need to.
from puppeteer_pdf.views import PDFTemplateView
class MyPDF(PDFTemplateView):
filename = 'my_pdf.pdf'
template_name = 'my_template.html'
cmd_options = {
'margin-top': 3,
}
Unicode characters¶
Templates containing utf-8 characters should be supported. You will need to ensure that you set the content type in your template file for puppeteer-pdf to interpret it properly.
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">