Django-wkhtmltopdf返回非零退出状态1

时间:2018-05-09 06:23:27

标签: python django wkhtmltopdf

我在开发中的Django项目中使用wkhtmltopdf收到以下错误。如果我尝试使用Apache服务器运行它,则返回的状态代码是6而不是1.

  

命令' [' wkhtmltopdf',' - 禁用-javascript',' - 编码',   u' utf8',' - 安静',你'错误',' /tmp/wkhtmltopdfnUwu3t.html',' - &# 39;]'   返回非零退出状态1

这是我的观点。

class MyPDF(OrgOwnerMixin, PDFTemplateView):
    filename = 'my_pdf.pdf'
    template_name = 'pdf/test.html'

    def get_object(self, *args, **kwargs):
        return Organisation.objects.get(slug=self.kwargs['slug'])

    def get_context_data(self, *args, **kwargs):
        ctx = super(MyPDF, self).get_context_data(*args, **kwargs)
        ctx['object'] = self.get_object()
        return ctx

这是Traceback:

回溯:

File "/home/henry/Documents/Sites/Development/fargus/env/local/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner
  41.             response = get_response(request)

File "/home/henry/Documents/Sites/Development/fargus/env/local/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  217.                 response = self.process_exception_by_middleware(e, request)

File "/home/henry/Documents/Sites/Development/fargus/env/local/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  215.                 response = response.render()

File "/home/henry/Documents/Sites/Development/fargus/env/local/lib/python2.7/site-packages/django/template/response.py" in render
  107.             self.content = self.rendered_content

File "/home/henry/Documents/Sites/Development/fargus/env/local/lib/python2.7/site-packages/wkhtmltopdf/views.py" in rendered_content
  78.             cmd_options=cmd_options

File "/home/henry/Documents/Sites/Development/fargus/env/local/lib/python2.7/site-packages/wkhtmltopdf/utils.py" in render_pdf_from_template
  186.                           cmd_options=cmd_options)

File "/home/henry/Documents/Sites/Development/fargus/env/local/lib/python2.7/site-packages/wkhtmltopdf/utils.py" in convert_to_pdf
  124.     return wkhtmltopdf(pages=filename, **cmd_options)

File "/home/henry/Documents/Sites/Development/fargus/env/local/lib/python2.7/site-packages/wkhtmltopdf/utils.py" in wkhtmltopdf
  110.     return check_output(ck_args, **ck_kwargs)

File "/usr/lib/python2.7/subprocess.py" in check_output
  574.         raise CalledProcessError(retcode, cmd, output=output)

Exception Type: CalledProcessError at /pdf/org-1/
Exception Value: Command '['wkhtmltopdf', '--disable-javascript', '--encoding', u'utf8', '--quiet', u'False', '/tmp/wkhtmltopdfEG5K8j.html', '-']' returned non-zero exit status 1

非常感谢任何帮助

2 个答案:

答案 0 :(得分:1)

看到PDFTemplateView我怀疑你正在使用django-wkhtmltopdf

查看追溯的Exception Value

Exception Value: Command '['wkhtmltopdf', '--disable-javascript', '--encoding', u'utf8', '--quiet', u'False', '/tmp/wkhtmltopdfEG5K8j.html', '-']' returned non-zero exit status 1

我在此处看到了可疑参数False--quiet False /tmp/wkhtmltopdfEG5K8j.html

但是,我刚刚进行了全新安装(最新的Django 2.x和django-wkhtmltopdf==3.1.0),我无法重现您的问题。 但是我注意到一件事:你继承了MyPDF的{​​{1}}课程,你忘记在这里发帖了。

我怀疑在OrgOwnerMixin OR中OrgOwnerMixin继承的任何类(如果有的话)中都有类似的内容:

OrgOwnerMixin

这会导致class OrgOwnerMixin: cmd_options = {'quiet': False} 作为参数传递给命令行中的False标志,从而触发您的异常。

如果要禁用--quiet标志,则必须执行以下操作:

--quiet

虽然我没有在文档中看到这一点,但我可以在代码中清楚地看到,只有当您将cmd_options = {'quiet': None} 作为选项的值传递时才会从命令行中删除它。您可以通过查看wkhtmltopdf.utils._options_to_args函数来查看此内容。

答案 1 :(得分:1)

我有一个类似的问题,但这是由于我的模板链接到外部文件。我发现解决方案是将'enable-local-file-access': True添加到我的选项中:

cmd_options = {'quiet': None, 'enable-local-file-access': True}

请参阅:https://github.com/wkhtmltopdf/wkhtmltopdf/issues/4460