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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27673870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:06:43  来源:igfitidea点击:

Can't create pdf using python PDFKIT Error : " No wkhtmltopdf executable found:"

pythonapipython-2.7pdfkit

提问by Arun Prakash

I tried installing pdfkit Python API in my windows 8 machine. I'm getting issues related to path.

我尝试在我的 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

Is anybody installed Python PDFKIt in windows machine? How to resolve this error.

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

My sample code :

我的示例代码:

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

回答by feng smith

I am learning python today, and I met the same problem, lately I set the windows enviroment variables and everything is OK.
I add the install path of wkhtml to the path, for example:"D:\developAssistTools\wkhtmltopdf\bin;" is my install path of wkhtml, and I add it to the path, everything is OK.

我今天正在学习python,遇到了同样的问题,最近我设置了windows环境变量,一切正常。
我将wkhtml的安装路径添加到路径中,例如:“D:\developAssistTools\wkhtmltopdf\bin;” 是我的wkhtml的安装路径,我添加到路径中,一切正常。

import pdfkit
pdfkit.from_url("http://google.com", "out.pdf")

finally, I find a out.pdf.

终于,我找到了一个out.pdf。

回答by kadee

The following should work without needing to modify the windows environment variables:

以下应该无需修改 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)

Assuming the path is correct of course (e.g. in my case it is r'C:\Program Files (x86)\wkhtmltopdf\bin\wkhtmltopdf.exe').

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

回答by Mohideen bin Mohammed

Please install wkhtmltopdf using,

请使用安装 wkhtmltopdf,

sudo apt install -y wkhtmltopdf

for windows machine install it from below link, http://wkhtmltopdf.org/downloads.html

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

and you need to add wkhtmltopdfpath into environment variables

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

回答by kariato

Found the decode on a windows platform needed to be a binary string, try:

在 windows 平台上发现解码需要是一个二进制字符串,尝试:

        path_wkthmltopdf = b'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe'
        config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf)
        pdfkit.from_url(url=urlpath, output_path=pdffilepath,configuration=config)

回答by Waqar Detho

No need to write wkhtmltopdf path into code. Just define an environment variable for that, and it works.

无需将 wkhtmltopdf 路径写入代码。只需为此定义一个环境变量,它就可以工作。

import pdfkit
pdfkit.from_url('http://google.com', 'out.pdf')

For me this code is working.

对我来说,这段代码正在工作。

回答by Wang Zoro

You need set

你需要设置

pdfkit.from_url('http://google.com', 'out.pdf',configuration=config)

pdfkit.from_url(' http://google.com', 'out.pdf',configuration=config)

回答by Rutrus

IOError: 'No wkhtmltopdf executable found'

IOError: 'No wkhtmltopdf executable found'

Make sure that you have wkhtmltopdf in your $PATH or set via custom configuration. where wkhtmltopdfin Windows or which wkhtmltopdfon Linux should return actual path to binary.

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

Adding this configuration line worked for me:

添加此配置行对我有用:

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

From github

来自 github

Seems you need to pass configuration=configas argument.

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

回答by user7363725

def urltopdf(url,pdffile):
    import pdfkit
    '''
        input
        - url : target url
        - pdffile : target pdf file name
    '''
    path_wkthmltopdf = 'D:\Program Files (x86)\wkhtmltopdf\bin\wkhtmltopdf.exe'
    config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf)
    #pdfkit.from_url(url=urlpath, output_path=pdffilepath,configuration=config)
    pdfkit.from_url(url,pdffile,configuration=config)


urltopdf('http://www.google.com','pdf/google.pdf')

very good solution! thanks everyone!

很好的解决方案!谢谢大家!

回答by Archie25

When I tried all of the above methods, I was till facing Permission Error as I don't have the admin rights to my workstation. If that's the case for you too, then make sure when you install your wkhtmltopdf.exe. The destination folder for installation is in your python site-packages folder, or add the directory to sys.path. Normally it gets installed in Program files folder. I changed the installation directory and this works for me:

当我尝试上述所有方法时,我一直面临权限错误,因为我没有工作站的管理员权限。如果您也是这种情况,请确保在安装 wkhtmltopdf.exe 时。安装的目标文件夹在你的 python site-packages 文件夹中,或者将目录添加到 sys.path。通常它会安装在 Program files 文件夹中。我更改了安装目录,这对我有用:

import pdfkit
pdfkit.from_url("http://google.com", "out.pdf")