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