我将使用wkhtmltopdf和django-wkhtmltopdf包装器来创建模板的.pdf。
我使用的是django-wkhtmltopdf文档中的示例(尽管我最终想要的不仅仅是一个静态模板):
url(r'^pipeline/snapshot/$', PDFTemplateView.as_view(
template_name='pdf/pipeline_snapshot.html',
filename='my_pdf.pdf'), name='pdf'),
我得到了一个错误:
Traceback:
File "/home/pluscitizen/webapps/odin/lib/python2.7/django/core/handlers/base.py" in get_response
140. response = response.render()
File "/home/pluscitizen/webapps/odin/lib/python2.7/django/template/response.py" in render
105. self.content = self.rendered_content
File "/home/pluscitizen/webapps/odin/lib/python2.7/django_wkhtmltopdf-1.2.2-py2.7.egg/wkhtmltopdf/views.py" in rendered_content
144. footer_filename=footer_filename)
File "/home/pluscitizen/webapps/odin/lib/python2.7/django_wkhtmltopdf-1.2.2-py2.7.egg/wkhtmltopdf/views.py" in convert_to_pdf
103. return wkhtmltopdf(pages=[filename], **cmd_options)
File "/home/pluscitizen/webapps/odin/lib/python2.7/django_wkhtmltopdf-1.2.2-py2.7.egg/wkhtmltopdf/utils.py" in wkhtmltopdf
92. return check_output(ck_args, **ck_kwargs)
File "/usr/local/lib/python2.7/subprocess.py" in check_output
575. raise CalledProcessError(retcode, cmd, output=output)
Exception Type: CalledProcessError at /pipeline/snapshot/
Exception Value: Command '['wkhtmltopdf', '--encoding', u'utf8', '--quiet', '/tmp/wkhtmltopdfVaAKrX.html', '-']' returned non-zero exit status 127
然而,当我使用相同的文件从Django shell运行相同的命令时
>>> subprocess.check_output(['wkhtmltopdf', '--encoding', u'utf8', '--quiet', '/tmp/wkhtmltopdfSGFfYh.html', '-'])
一切都很顺利。同上:
>>> wkhtmltopdf(['/tmp/wkhtmltopdfSGFfYh.html'], **{})
因此,考虑到差异一定是在整个shell情况下,我尝试在django-wkhtmltopdf中的子进程调用中添加一个shell=True(我知道这是一个安全问题),但没有成功。可能是因为我不知道到底发生了什么。
我在某个地方看到有人说这个问题可能与没有正确设置PATH有关,但我不明白为什么Django的shell没有这个问题。
整个过程非常耗费精力,现在我已经很接近了,我终于向so寻求答案。
编辑:我曾尝试直接在视图中运行子流程,但它返回相同的错误,我想这意味着shell和服务器不一定都在同一个环境中运行?
固定的
:已解决,答案为
sparks
带我走在路上。我决定查看服务器上django应用程序输出的日志(duh),并注意到在回溯之前,我得到了命令的实际输出:
wkhtmltopdf: error while loading shared libraries: libwkhtmltox.so.0: cannot open shared object file: No such file or directory
这似乎意味着它没有在/home/user/lib中找到我拥有的库。当我明确地将这些添加到settings.py中的WKHTMLTOPDF_ENV变量中时,一切都很顺利。