我使用pip (pip install pdfkit)安装pdfkit。
然后从这里安装wkhtmltopdf。
但是,当我尝试运行以下代码时:
import pdfkit
config = pdfkit.configuration(wkhtmltopdf='C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe')
pdfkit.from_url('https://www.google.com', 'C:\\Users\\Χρήστος\\Desktop\\out-test.pdf', configuration=config)错误或错误:
Traceback (most recent call last):
File "create_pdf.py", line 4, in <module>
pdfkit.from_url('https://www.google.com', 'C:\\Users\\Χρήστος\\Desktop\\out-test.pdf', configuration=config)
File "C:\Users\Χρήστος\AppData\Local\Programs\Python\Python38\lib\site-packages\pdfkit\api.py", line 26, in from_url
return r.to_pdf(output_path)
File "C:\Users\Χρήστος\AppData\Local\Programs\Python\Python38\lib\site-packages\pdfkit\pdfkit.py", line 156, in to_pdf
raise IOError('wkhtmltopdf reported an error:\n' + stderr)
OSError: wkhtmltopdf reported an error:
Loading pages (1/6)
QPainter::begin(): Returned false============================] 100%
Error: Unable to write to destination
Exit with code 1, due to unknown error.任何建议都是有用的。
我还使用管理权限运行代码。
编辑:来自wkhtmltopdf站点的:
我该怎么用呢?下载预编译的二进制文件或从源代码生成要转换为PDF (或图像)的HTML文档,通过该工具运行HTML文档。例如,如果我真的喜欢Google今天对他们的徽标所做的处理,并希望永远将其作为PDF来捕获: wkhtmltopdf http://google.com google.pdf
所以我试了一下:
cd C:\Program Files\wkhtmltopdf\bin
wkhtmltopdf http://google.com google.pdf不起作用,但当我使用管理员权限运行它时,pdf是生成的。因此pdfkit模块必须使用管理权限打开wkhtmltopdf。
发布于 2020-11-20 14:53:05
有一个问题 on GitHub,似乎您必须指定完整的路径(例如。'C://foo/bar')
可能是因为脚本没有在其所在文件夹中写入的权限:
C:\Users\Χρήστος\AppData\Local\Programs\Python\Python38\lib\site-packages\pdfkit\pdfkit.py
您给了它一个相对路径,所以在运行时,它将尝试创建一个文件:
C:\Users\Χρήστος\AppData\Local\Programs\Python\Python38\lib\site-packages\pdfkit\out.pdf
https://stackoverflow.com/questions/64931535
复制相似问题