Could someone please provide me with a comprehensive example of how to get a view
in django
to return a PDF
using wkhtmltopdf
. There are limited number of examples that come with django-wkhtmltopdf
and they presume a level of knowledge I just don't have. I have looked through the source code but I can't make heads or tails of how to use it (for example whats the difference between PDFTemplateView
and PDFTemplateResponse
?!?)
I would be very grateful for any help.
BTW(I'm using templates for the header and footer as well)
EDIT
def some_view(request,sID):
something = get_object_or_404(Something,id=sID)
return render_to_response('something.html', {'something':something}, context_instance=RequestContext(request))
How would I get the following simple view to provide me with a pdf
instead of an html
file?
EDIT 2
I am currently playing around with:
def pdf_view(request,sID):
template = 'pdf.html'
something = get_object_or_404(Something,id=sID)
context = {
'something' : Something,
'object_for_header_and_footer': something.object_for_header_and_footer,
}
cmd_options = settings.WKHTMLTOPDF_CMD_OPTIONS
return PDFTemplateResponse(request=request,
context=context,
template=template,
filename='something',
header_template='header.html',
footer_template='footer.html',
cmd_options=cmd_options)
but I am getting 'str' object has no attribute 'update'
in /usr/local/lib/python2.7/dist-packages/wkhtmltopdf/utils.py in wkhtmltopdf, line 74
. I don't know whether to starting hacking wkhtmltopdf?!?!
Hmm the error indicates that you're passing string somewhere that you shouldn't.
After checking its source code, I guess in
settings.py
you haveWKHTMLTOPDF_CMD_OPTIONS
as a string, something likeBut you should assign a dict there:
Otherwise your code should work fine.
The difference between PDFTemplateView and PDFTemplateResponse is that the view is a class-based view. And PDFTemplateResponse renders the pdf data and sets the right response headers. To add header and footer:
Opening pdf/ in your browser will start a download of my_pdf.pdf based on the my_template.html, my_header_template.html and my_footer_template.html.
The advanced example shows how to subclass PDFTemplateView extending and changing the logic of PDFTemplateView. To understand what happens read Using class based views.
Like
header_template
andfooter_template
you can define aresponse_class
. Because PDFTemplateResponse is the default, you don't have to define it.EDIT
The following simple view provides you with a pdf instead of an html. This is not using django-wkhtmltopdf. You could use wkhtmltopdf in your html2pdf function.
EDIT 2
A simple view with context:
template.html
urls.py
views.py
EDIT 3
If you use a DetailView, you can add the object to context: