为什么通过python pdfkit调用时会出现wkhtmltopdf IO错误?
我正试图将我的为什么通过python pdfkit调用时会出现wkhtmltopdf IO错误?,python,wkhtmltopdf,pdfkit,Python,Wkhtmltopdf,Pdfkit,我正试图将我的wkhtmltopdf软件包升级到0.12.2.1,但运气不好 它在更新之前确实起作用了。我尝试更新的原因是 修复PDF页面之间内容的拆分 我正在运行-Ubuntu 64x-Trusty 我正在通过python pdfkit使用 我从获取最新版本的wkhtmltopdf 以下是我迄今为止所做/尝试的内容。有人更新过他们的设置吗?或者任何指针都会非常有用!谢谢 开箱即用,我得到了配置错误我硬编码就是为了克服这个问题 # -*- coding: utf-8 -*- import s
wkhtmltopdf
软件包升级到0.12.2.1
,但运气不好
- 它在更新之前确实起作用了。我尝试更新的原因是 修复PDF页面之间内容的拆分
- 我正在运行-Ubuntu 64x-Trusty
- 我正在通过python pdfkit使用
- 我从获取最新版本的
wkhtmltopdf
配置错误
我硬编码就是为了克服这个问题
# -*- coding: utf-8 -*-
import subprocess
import sys
class Configuration(object):
def __init__(self, wkhtmltopdf='', meta_tag_prefix='pdfkit-'):
self.meta_tag_prefix = meta_tag_prefix
self.wkhtmltopdf = wkhtmltopdf
if not self.wkhtmltopdf:
if sys.platform == 'win32':
self.wkhtmltopdf = subprocess.Popen( ['where', 'wkhtmltopdf'], stdout=subprocess.PIPE).communicate()[0].strip()
else:
self.wkhtmltopdf = subprocess.Popen( ['which', 'wkhtmltopdf'], stdout=subprocess.PIPE).communicate()[0].strip()
try:
#with open(self.wkhtmltopdf) as f:
with open("/usr/local/bin/wkhtmltopdf") as f:
pass
except IOError:
raise IOError('No wkhtmltopdf executable found: "%s"\n'
'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' % self.wkhtmltopdf)
现在我得到这个错误,不知道如何继续。我尝试向上面的Popen添加shell=True
,但也没有成功
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/rq/worker.py", line 543, in perform_job
rv = job.perform()
File "/usr/local/lib/python2.7/dist-packages/rq/job.py", line 490, in perform
self._result = self.func(*self.args, **self.kwargs)
File "/home/worker-1/Desktop/Dropbox/changeaddress/facts/jobs.py", line 864, in job_sharepdfs
mymovepdf_link = build_mymovepdf(account_uuid, addresschange_uuid)
File "/home/worker-1/Desktop/Dropbox/changeaddress/facts/jobs.py", line 608, in build_mymovepdf
s3file = pdfkit.from_string( output.getvalue() , False )
File "/usr/local/lib/python2.7/dist-packages/pdfkit/api.py", line 68, in from_string
return r.to_pdf(output_path)
File "/usr/local/lib/python2.7/dist-packages/pdfkit/pdfkit.py", line 93, in to_pdf
stderr=subprocess.PIPE)
File "/usr/lib/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1327, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
您必须设置
wkhtmltopdf
库的配置
(路径)
我创建了一个基本的助手函数,如下所示
import os
import pdfkit
WKHTMLTOPDF_PATH = '/usr/local/bin/wkhtmltopdf'
def generate_pdf(html, static_path, _path):
config = pdfkit.configuration(wkhtmltopdf=WKHTMLTOPDF_PATH)
_status = pdfkit.from_string(
html,
os.path.join(static_path, _path),
configuration=config,
options={
'page-size': 'A4',
'margin-top': '0',
'margin-right': '0',
'margin-left': '0',
'margin-bottom': '0',
'zoom': '1.2',
'encoding': "UTF-8",
})
return _path if _status else ''
使用:
html = "<h1>Hello World !!!</h1>"
static_path = "/static/"
file_path = "pdfs/out.pdf"
generate_pdf(html, static_path, file_path)
html=“你好,世界!!!”
static_path=“/static/”
file_path=“pdfs/out.pdf”
生成pdf(html、静态路径、文件路径)
另一个解决方案(安装wkhtmltopdf
):
请注意,现在关门了。因此,如果您想下载最新的二进制文件,请访问
wget http://download.gna.org/wkhtmltopdf/0.12/0.12.3/wkhtmltox-0.12.3_linux-generic-amd64.tar.xz
tar xvf wkhtmltox-0.12.3_linux-generic-amd64.tar.xz
sudo mv wkhtmltox/bin/wkhtmltopdf /usr/local/bin/wkhtmltopdf
sudo chmod +x /usr/local/bin/wkhtmltopdf