可以在Windows上安装wkhtmltopdf Python包吗?
我正在尝试通过一个Python脚本把存储在我电脑上的一些HTML文件转换成PDF格式。我试过使用xhtml2pdf,但遇到了很多错误,所以我决定不再使用它。
我听说wkhtmltopdf是一个更好的选择,而且我找到一个和它很好集成的Python包。不幸的是,这个包需要xvfb,而xvfb在Windows上无法安装。有没有其他方法可以在Windows上为Python安装wkhtmltopdf?
谢谢你的帮助!
3 个回答
0
这段内容是关于Windows和Linux系统的。
- wkhtmltopdf [下载链接][1],这是Windows的安装程序。
把这个程序的“路径”添加到环境变量中。
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
2
- 在Windows上安装wkhtmltopdf(可以去这个链接下载:http://wkhtmltopdf.org/downloads.html)
- 把bin文件夹添加到“Path”环境变量中(比如:C:\Program Files (x86)\wkhtmltopdf\bin)
- 安装pdfkit这个Python库(可以用命令:pip install pdfkit)
关于pdfKit的使用说明可以在这里找到:https://pypi.python.org/pypi/pdfkit
2
这是 wkhtmltopdf 的 下载列表,里面有 Windows 安装程序可以下载。