wkhtmltopdf - FileNotFoundError: [Errno 2] No such file or directory: 'which' when using python pdfkit
问题描述
我有一个奇怪的错误。我尝试使用 pdfkit 将 HTML 字符串转换为 PDF,但我得到一个“没有这样的文件或目录:'which'”。我知道 pdfkit 使用 wkhtmltopdf,它试图通过使用 which 来找到这个实用程序。我不知道发生了什么。'which' 工作正常,并且 wkhtmltopdf 安装在 /usr/bin/ 上。我在这一行得到错误:
pdf = pdfkit.from_string(html, False, options=options)
和回溯:
Traceback:
File "/srv/zboss-git/venv/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
39. response = get_response(request)
File "/srv/zboss-git/venv/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/srv/zboss-git/venv/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/srv/zboss-git/venv/lib/python3.5/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
23. return view_func(request, *args, **kwargs)
File "/srv/zboss-git/venv/lib/python3.5/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
23. return view_func(request, *args, **kwargs)
File "/srv/zboss-git/zboss/issues/views.py" in export_comments
453. pdf = pdfkit.from_string(html, False, options=options)
File "/srv/zboss-git/venv/lib/python3.5/site-packages/pdfkit/api.py" in from_string
66. configuration=configuration)
File "/srv/zboss-git/venv/lib/python3.5/site-packages/pdfkit/pdfkit.py" in __init__
38. self.configuration = (Configuration() if configuration is None
File "/srv/zboss-git/venv/lib/python3.5/site-packages/pdfkit/configuration.py" in __init__
18. ['which', 'wkhtmltopdf'], stdout=subprocess.PIPE).communicate()[0].strip()
File "/usr/lib/python3.5/subprocess.py" in __init__
947. restore_signals, start_new_session)
File "/usr/lib/python3.5/subprocess.py" in _execute_child
1551. raise child_exception_type(errno_num, err_msg)
Exception Type: FileNotFoundError at /issues/export_comments/
Exception Value: [Errno 2] No such file or directory: 'which'
解决方案
我找到了解决方案。
根据 python-pdfkit 的文档:
Warning! Version in debian/ubuntu repos have reduced functionality (because it compiled without the wkhtmltopdf QT patches), such as adding outlines, headers, footers, TOC etc. To use this options you should install static binary from wkhtmltopdf site or you can use this script.
脚本:
https://github.com/JazzCore/python-pdfkit/blob/master/travis/before-script.sh
我卸载我的版本并运行这个版本。我还手动设置了我的配置:
config = pdfkit.configuration(wkhtmltopdf=bytes('/usr/bin/wkhtmltopdf', 'utf-8'))
pdf = pdfkit.from_string(html, False, options=options, configuration=config)
瞧!
推荐阅读
- google-search-console - 如何修复解析错误:Osclass 免费分类上缺少“}”或对象成员名称
- sql-server - 计算列值相对于最大行值占一行的所有列的百分比
- flutter - 颤振:没有为类“标记”定义吸气剂“选项”
- swift - WatchOS 6.0、Swift 5.0:无背景触觉
- c# - Azure Function v3 中的 System.Text.Json:JsonResult.SerializerSettings 必须是 JsonSerializerSettings 类型的实例
- c# - 将 DataTable 插入 MySQL 数据库表时缺少行
- php - PHP每天重命名文件
- python-3.x - 拥有 .dbf、.prj、.shp 和 .shx 文件时,如何在 geopandas 中读取 shapefile?
- angular - webapi核心无法上传被cors策略阻止的大文件
- azure - 从 Azure Web App Bot 获取 Qna Maker 错误