使用django包装器的wkhtmltopdf子进程错误,但不在shell中

时间:2013-10-17 19:47:27

标签: django wkhtmltopdf

我正在使用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中调用subprocess进入shell = True(我知道这是一个安全问题),但是没有运气。可能是因为我不知道究竟发生了什么。

我看到某个地方说问题可能与PATH没有正确设置有关,但后来我不明白为什么Django的shell没有问题。

整个过程非常费力,现在我已经非常接近,我终于转向SO寻求答案。

编辑:我已经尝试在视图中直接运行子进程,并且它返回相同的错误,我想这意味着shell和服务器不一定都在同一环境中运行?

已修复:已解决,而sparks的回答让我顺便前行。我决定查看服务器上的django app输出的日志(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变量时,一切都在顺利进行。

2 个答案:

答案 0 :(得分:1)

退出状态127

这意味着无法找到您尝试运行的命令。这是您流程环境中的错误。

我建议在python中查看sys.path的内容,并将其与PATH环境变量进行比较,或者与django控制台中的sys.path进行比较。

答案 1 :(得分:0)

更具体地说,将其添加到settings.py:

WKHTMLTOPDF_ENV = {' FONTCONFIG_PATH':' / etc / fonts'}