【问题标题】:Django-wkhtmltopdf returned non-zero exit status 1Django-wkhtmltopdf 返回非零退出状态 1
【发布时间】:2018-05-09 06:23:26
【问题描述】:

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

命令 '['wkhtmltopdf', '--disable-javascript', '--encoding', u'utf8', '--quiet', u'False', '/tmp/wkhtmltopdfnUwu3t.html', '-']' 返回非零退出状态 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

任何帮助将不胜感激

【问题讨论】:

    标签: python django wkhtmltopdf


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

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

      看看你得到的错误。

      【讨论】:

      • 这是我走的路线。我发现我需要使用wgetpathced QT 而不是apt get 重新安装wkhtmltopdf,然后在选项中包含--allow /tmp,然后更改我的style.css 文件。我没有发现原始文件的问题,但更改了格式以使其正常工作
      【解决方案3】:

      看到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 类,而您忘记在此处发布。

      我怀疑在OrgOwnerMixinOrgOwnerMixin 继承自(如果有的话)的任何类中,您都输入了类似的内容:

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

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

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

      cmd_options = {'quiet': None}
      

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-01-13
        • 2019-10-02
        • 1970-01-01
        • 2017-05-20
        • 2014-07-30
        • 1970-01-01
        • 2021-02-15
        • 1970-01-01
        相关资源
        最近更新 更多