python pdfkit多个html文件生成pdf错误调试


开发环境
wkhtmltopdf: 0.12.3.2
Python: 3.5.2+

问题描述

在将多个html文件转换成pdf的时候, 提示: This version of wkhtmltopdf is build against an unpatched version of QT

options = {
    'encoding': "UTF-8",
    'custom-header': [
        ('Accept-Encoding', 'gzip')
    ]
}
pdfkit.from_file(['0.html', '1.html'], 'demo.pdf', options=options)
Traceback (most recent call last):
  File "/path/to/jekyll_spider.py", line 148, in <module>
    main()
  File "/path/to/jekyll_spider.py", line 144, in main
    save_pdf(html_paths, file_name)
  File "/path/to/jekyll_spider.py", line 118, in save_pdf
    pdfkit.from_file(html_path, pdf_path, options=options)
  File "/usr/local/lib/python3.5/dist-packages/pdfkit/api.py", line 49, in from_file
    return r.to_pdf(output_path)
  File "/usr/local/lib/python3.5/dist-packages/pdfkit/pdfkit.py", line 156, in to_pdf
    raise IOError('wkhtmltopdf reported an error:\n' + stderr)
OSError: wkhtmltopdf reported an error:
Error: This version of wkhtmltopdf is build against an unpatched version of QT, and does not support more then one input document.
Exit with code 1, due to unknown error.

解决方法

下载并使用 0.12.4 版本的 wkhtmltopdf^1

$ wget http://download.gna.org/wkhtmltopdf/0.12/0.12.4/wkhtmltox-0.12.4_linux-generic-amd64.tar.xz
$ tar xvJf wkhtmltox-0.12.4_linux-generic-amd64.tar.xz
$ sudo cp wkhtmltox/bin/wkhtmlto* /usr/bin/

如果使用PDFkit没有安装wkhtmltopdf, 则会报如下错误:

Traceback (most recent call last):
  File "/path/to/jekyll_spider.py", line 148, in <module>
    main()
  File "/path/to/jekyll_spider.py", line 144, in main
    save_pdf(html_paths, file_name)
  File "/path/to/jekyll_spider.py", line 118, in save_pdf
    pdfkit.from_file(html_path, pdf_path, options=options)
  File "/usr/local/lib/python3.5/dist-packages/pdfkit/api.py", line 47, in from_file
    configuration=configuration, cover_first=cover_first)
  File "/usr/local/lib/python3.5/dist-packages/pdfkit/pdfkit.py", line 42, in __init__
    self.configuration = (Configuration() if configuration is None
  File "/usr/local/lib/python3.5/dist-packages/pdfkit/configuration.py", line 27, in __init__
    'https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf' % self.wkhtmltopdf)
OSError: No wkhtmltopdf executable found: "b''"
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

Ubuntu或者debain系统, 可以直接命令行安装wkhtmltopdf:

$ sudo apt-get install wkhtmltopdf

参考文档


Author: Itaken
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint polocy. If reproduced, please indicate source Itaken !
  TOC目录