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

Django-wkhtmltopdf returned non-zero exit status 1

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

Command '['wkhtmltopdf', '--disable-javascript', '--encoding', u'utf8', '--quiet', u'False', '/tmp/wkhtmltopdfnUwu3t.html', '-']' returned non-zero exit status 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

这是回溯:

回溯:

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

任何帮助将不胜感激

看到 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),我无法重现您的问题。 但是 我注意到一件事:您从 OrgOwnerMixin 继承了您的 MyPDF class,您在这里忘记了 post。

我怀疑在 OrgOwnerMixinOrgOwnerMixin 继承自(如果有的话)的任何 class 中,你都放了类似的东西:

class OrgOwnerMixin:
    cmd_options = {'quiet': False}

这导致 False 作为参数传递给命令行中的 --quiet 标志,结果 - 触发异常。

如果你想禁用 --quiet 标志,你必须这样做:

cmd_options = {'quiet': None}

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

执行命令 wkhtmltopdf --disable-javascript --encoding utf8 --quiet False /tmp/wkhtmltopdfEG5K8j.html -

并查看您收到的错误。

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

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

参见:https://github.com/wkhtmltopdf/wkhtmltopdf/issues/4460