【问题标题】:Is it possible to install the wkhtmltopdf Python package on Windows?是否可以在 Windows 上安装 wkhtmltopdf Python 包?
【发布时间】:2020-03-27 21:20:05
【问题描述】:

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

我听说 wkhtmltopdf 是一个更好的选择,我发现了一个可以很好地集成到其中的 Python 包。不幸的是,这个包需要 xvfb,它不能在 Windows 上安装。有没有其他方法可以在 Windows 上为 Python 安装 wkhtmltopdf?

感谢您的帮助!

【问题讨论】:

    标签: python windows wkhtmltopdf


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:
      • 为 Windows 安装 wkhtmltopdf(http://wkhtmltopdf.org/downloads.html)
      • 将 bin 文件夹添加到“Path”环境变量(例如:C:\Program Files (x86)\wkhtmltopdf\bin)
      • 为 python 绑定安装 pdfkit (pip install pdfkit)

      关于 pdfKit 的文档可以在这里找到:https://pypi.python.org/pypi/pdfkit

      【讨论】:

        【解决方案3】:

        这适用于 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
          

        【讨论】:

          猜你喜欢
          • 2021-04-27
          • 2019-06-29
          • 2012-12-03
          • 2013-03-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-02-19
          • 1970-01-01
          相关资源
          最近更新 更多