本人环境配置
wkhtmltopdf: 0.12.3.2Python: 3.5.2+
问题描述
options = {'encoding': "UTF-8",'custom-header': [('Accept-Encoding', 'gzip')]}pdfkit.from_file(['0.html', '1.html'], 'demo.pdf', options=options)
在将多个html文件转换成pdf的时候, 提示:
This version of wkhtmltopdf is build against an unpatched version of QT
发现下面错误有时候pdf已经转换出来 try下 延时下就可以了
except:print'-------------xxxxx-----------' time.sleep(2)
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 mainsave_pdf(html_paths, file_name)File "/path/to/jekyll_spider.py", line 118, in save_pdfpdfkit.from_file(html_path, pdf_path, options=options)File "/usr/local/lib/python3.5/dist-packages/pdfkit/api.py", line 49, in from_filereturn r.to_pdf(output_path)File "/usr/local/lib/python3.5/dist-packages/pdfkit/pdfkit.py", line 156, in to_pdfraise 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 /wkhtmltopdf/wkhtmltopdf/releases/download/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 mainsave_pdf(html_paths, file_name)File "/path/to/jekyll_spider.py", line 118, in save_pdfpdfkit.from_file(html_path, pdf_path, options=options)File "/usr/local/lib/python3.5/dist-packages/pdfkit/api.py", line 47, in from_fileconfiguration=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 NoneFile "/usr/local/lib/python3.5/dist-packages/pdfkit/configuration.py", line 27, in __init__'/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 - /JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf
Ubuntu可以直接命令行安装
wkhtmltopdf
:
$ sudo apt-get install wkhtmltopdf