2013-03-03 105 views
2

我刚刚发现wkhtmltopdf,使用webkit的惊人的html转换器。我已经在我的开发机器上尝试过它,它的简单并且运行良好。HTML to PDF with python and wkhtmltopdf

如何才能最好地与基于Django的网站集成?

我发现python bindings,但他们认为对如何安装我没有的东西有一定程度的了解。例如

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) 
  • 安装的Python绑定后,我该如何使用它们?我可以做什么?

  • 由此产生的PDF必须保存到HD或我可以流出一个视图与东西?

例如:

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

回答

4

我会建议django-wkhtmltopdf用于这一目的。他们的usage documentation给出了几个关于如何整合的例子:

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