html转pdf错误wkhtmltopdf

本人环境配置

wkhtmltopdf: 0.12.3.2
Python: 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 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 https://github.com/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 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可以直接命令行安装wkhtmltopdf:

$ sudo apt-get install wkhtmltopdf
    原文作者:xp5xp6
    原文地址: https://blog.csdn.net/xp5xp6/article/details/78438156
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞