Search code examples
pythondjangowkhtmltopdfpython-bindings

HTML to PDF with python and wkhtmltopdf


I have just found wkhtmltopdf, amazing html converter using webkit. I have tried it on my dev machine and its simple and works well.

How can this best be integrated with a django based site?

I found the python bindings, but they presume a certain level of understanding of how to install things I just don't have. e.g.

you need libwkhtmltox.* somewhere in your LD path (/usr/local/lib)
you need the directory src/include/wkhtmltox from wkhtmltopdf
    somewhere on your include path (/usr/local/include)
  • After installing those python bindings, how do I use them? What calls can I do?

  • Does the resulting pdf have to be saved to the hd or can I stream it out of a view with something?

For example:

response['Content-Disposition'] = 'attachment; filename='+letter_name
response['Content-Type'] = 'Content-type: application/octet-stream'
response['Content-Length'] = bytes
return response

Solution

  • I would recommend django-wkhtmltopdf for this purpose. Their usage documentation gives a few examples on how to integrate:

    from django.conf.urls.defaults import *
    from wkhtmltopdf.views import PDFTemplateView
    
    
    urlpatterns = patterns('',
        # ...
        url(r'^pdf/$', PDFTemplateView.as_view(template_name='my_template.html',
                                               filename='my_pdf.pdf'), name='pdf'),
        # ...
    )