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

2024-06-11 22:36:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着在我的windows 8机器上安装pdfkit Python API。我遇到了和path有关的问题。

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

有人在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')

Tags: infrompycomurlwindowslibpackages
3条回答

请使用安装wkhtmltoppdf

sudo apt install -y wkhtmltopdf

对于windows计算机,请从下面的链接http://wkhtmltopdf.org/downloads.html安装它

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

在不需要修改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)

当然,假设路径是正确的(例如,在我的例子中,它是r'C:\程序文件(x86)\ wkhtmltopf\bin\wkhtmltopf.exe')。

IOError: 'No wkhtmltopdf executable found'

确保您的$PATH中有wkhtmltopfd或通过自定义配置进行设置。where wkhtmltopdf在Windows或which wkhtmltopdf在Linux上应该返回二进制文件的实际路径。

添加此配置行对我有效:

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

From github

似乎你需要把configuration=config作为参数。

相关问题 更多 >