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

4
我在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

以下是Traceback:

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

任何帮助都将不胜感激。
4个回答

5

我遇到了类似的问题,但是它是由于我的模板链接到外部文件所导致的。我发现解决办法是在我的选项中添加'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重新安装wkhtmltopdf, 而不是使用apt get,然后在选项中包含 --allow /tmp,然后更改我的 style.css 文件。我还没有找到原始文件的问题,但更改了格式以使其工作。 - HenryM

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类继承自OrgOwnerMixin,但您忘记在这里发布它。
我怀疑您在OrgOwnerMixin中或者任何OrgOwnerMixin继承的类中(如果有的话)放置了类似以下内容的代码:
class OrgOwnerMixin:
    cmd_options = {'quiet': False}

这会导致在命令行中将False作为参数传递给--quiet标志,进而引发异常。
如果您想禁用--quiet标志,您需要执行以下操作:
cmd_options = {'quiet': None}

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


0

运行命令向我展示了错误是什么。在我的情况下,错误是因为我没有一个 static/ 文件夹。运行 python3 manage.py collectstatic 命令解决了这个问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接