我刚刚找到wkhtmltopdf ,使用 webkit 的令人惊叹的 html 转换器。我已经在我的开发机器上尝试过它,它简单且运行良好。
如何最好地将其与基于 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 绑定(bind)后,如何使用它们?我可以调用哪些电话?
生成的 pdf 是否必须保存到高清,或者我可以使用某些东西将其从 View 中流式传输吗?
例如:
response['Content-Disposition'] = 'attachment; filename='+letter_name
response['Content-Type'] = 'Content-type: application/octet-stream'
response['Content-Length'] = bytes
return response
最佳答案
我会推荐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'),
# ...
)
关于python - 使用 python 和 wkhtmltopdf 将 HTML 转为 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15188245/