是否可以在Windows上安装wkhtmltopdf Python包?

时间:2012-02-04 21:50:43

标签: python windows wkhtmltopdf

我正在尝试通过Python脚本将我的计算机上本地存储的一些HTML文件转换为PDF格式,我尝试过xhtml2pdf,但我遇到了无数错误并决定停止使用它。

我听说wkhtmltopdf是一个更好的选择,我找到了一个很好的集成的Python包。不幸的是,这个软件包需要xvfb,不能为Windows安装。有没有其他方法在Windows上安装wkhtmltopdf for Python?

感谢您的帮助!

3 个答案:

答案 0 :(得分:1)

这是wkhtmltopdf download list,包括Windows安装程序

答案 1 :(得分:0)

  • 为Windows安装wkhtmltopdf(http://wkhtmltopdf.org/downloads.html
  • 将bin文件夹添加到“Path”env变量(例如:C:\ Program Files(x86)\ wkhtmltopdf \ bin)
  • 安装pdfkit for python bindings(pip install pdfkit)

有关pdfKit的文档可在此处找到:https://pypi.python.org/pypi/pdfkit

答案 2 :(得分:0)

这是针对Windows和Linux的

  • wkhtmltopdf [下载列表] [1],用于Windows Installer
  • 将二进制“路径”添加到env变量

    def html_to_pdf(<html>):
       with tempfile.NamedTemporaryFile(suffix='.html', delete=False) as temp:
           temp.write(html.encode('windows-1252'))
           temp.seek(0)
           pdfkit.from_file(temp.name, 'out.pdf') 
           temp.close()
           pdf = open('out.pdf', 'r+b')
           response = HttpResponse(pdf.read(), content_type='application/pdf')
           response['Content-Disposition'] = 'attachment; filename=out.pdf'
           pdf.close()
           # remove the locally created pdf file.
           os.remove(temp.name)
           return response