Robert Johnstone
Robert Johnstone

Reputation: 5371

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)

For example:

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

Upvotes: 2

Views: 5089

Answers (1)

cwgem
cwgem

Reputation: 2809

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'),
    # ...
)

Upvotes: 4

Related Questions