无法使用python PDFKIT创建pdf错误:"找不到wkhtmltopdf可执行文件:"

Aru*_*ash 20 python api pdfkit python-2.7

我尝试在我的Windows 8机器上安装pdfkit Python API.我遇到了与路径有关的问题.

Traceback (most recent call last):
  File "C:\Python27\pdfcre", line 13, in <module>
    pdfkit.from_url('http://google.com', 'out.pdf')
  File "C:\Python27\lib\site-packages\pdfkit\api.py", line 22, in from_url
    configuration=configuration)
  File "C:\Python27\lib\site-packages\pdfkit\pdfkit.py", line 38, in __init__
    self.configuration = (Configuration() if configuration is None
  File "C:\Python27\lib\site-packages\pdfkit\configuration.py", line 27, in __init__
    'https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf' % self.wkhtmltopdf)
IOError: No wkhtmltopdf executable found: ""
If this file exists please check that this process can read it. Otherwise please install wkhtmltopdf - https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf
Run Code Online (Sandbox Code Playgroud)

是否有人在Windows机器上安装了Python PDFKIt?如何解决此错误.

我的示例代码:

import pdfkit
import os
config = pdfkit.configuration(wkhtmltopdf='C:\\Python27\\wkhtmltopdf\bin\\wkhtmltopdf.exe')
pdfkit.from_url('http://google.com', 'out.pdf')
Run Code Online (Sandbox Code Playgroud)

kad*_*dee 24

以下应该可以在不修改Windows环境变量的情况下工作:

import pdfkit
path_wkhtmltopdf = r'C:\Program Files (x86)\wkhtmltopdf\bin\wkhtmltopdf.exe'
config = pdfkit.configuration(wkhtmltopdf=path_wkhtmltopdf)
pdfkit.from_url("http://google.com", "out.pdf", configuration=config)
Run Code Online (Sandbox Code Playgroud)

假设路径当然是正确的(例如在我的情况下它是r'C:\ Program Files(x86)\ wkhtmltopdf\bin\wkhtmltopdf.exe').

  • 只是为了让可能没有管理员权限的读者明白,下载后不需要实际运行 wkhtmltopdf 安装程序,只需解压缩“.exe”,然后指向“\bin\wkhtmltopdf.exe”文件在解压后的“.exe”中。对于我来说(在没有权限的 Windows 机器上),这是我测试过的所有 Python html-to-pdf 解决方案中最合适的(我还尝试了 weasyprint 和 xhtml2pdf)。 (3认同)

Moh*_*med 10

请使用安装wkhtmltopdf,

sudo apt install -y wkhtmltopdf
Run Code Online (Sandbox Code Playgroud)

对于Windows机器从下面链接安装它,http://wkhtmltopdf.org/downloads.html

并且您需要将wkhtmltopdf路径添加到环境变量中


Rut*_*rus 6

IOError: 'No wkhtmltopdf executable found'

确保$ PATH中包含wkhtmltopdf或通过自定义配置进行设置。where wkhtmltopdf在Windows或which wkhtmltopdfLinux上,应返回二进制的实际路径。

添加此配置行对我有用:

config = pdfkit.configuration(wkhtmltopdf="C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe")
pdfkit.from_string(html, 'MyPDF.pdf', configuration=config)
Run Code Online (Sandbox Code Playgroud)

来自github

似乎您需要configuration=config作为参数传递。

  • 这是天赐之物……我一天以来一直在努力让 Weasyprint 或 PyQT5 做这样的事情。这就是这么简单。谢谢你! (2认同)

fen*_*ith 5

我今天正在学习python,我遇到了同样的问题,最近我设置了windows环境变量,一切都还可以.
我将wkhtml的安装路径添加到路径中,例如:"D:\ developAssistTools\wkhtmltopdf\bin;" 是我的wkhtml的安装路径,我把它添加到路径,一切都OK.

import pdfkit
pdfkit.from_url("http://google.com", "out.pdf")
Run Code Online (Sandbox Code Playgroud)

最后,我发现了一个out.pdf.