Django - wkhmltopdf - CalledProcessError(仅适用于命令行中的sudo)

时间:2017-11-29 11:02:59

标签: python django pdf wkhtmltopdf django-wkhtmltopdf

我正在尝试使用django-wkhtmltopdf从Django模板渲染PDF。问题是wkhtmltopdf开始提升:

Command '['/usr/bin/wkhtmltopdf', '--encoding', u'utf8', '--quiet', u'False', '/tmp/wkhtmltopdfRDyi61.html', '-']' returned non-zero exit status 1

无法弄清楚问题出在哪里,但我注意到我甚至无法在命令行中做(我在virtualenv中):

wkhtmltopdf http://www.google.com g.pdf

Loading pages (1/6)
QPainter::begin(): Returned false============================] 100%
Error: Unable to write to destination                              
Exit with code 1, due to unknown error.

它仅适用于sudo权限(但我在PyCharmProject我的用户目录中):

sudo wkhtmltopdf http://www.google.com g.pdf

我还注意到html文件夹中的一些临时/tmp/文件未被删除。

这是Django返回的整个追溯:

TRACEBACK:

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/render/

Django Version: 1.11.7
Python Version: 2.7.12
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'agreements',
 'weasyprint']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

    File "/home/milano/.virtualenvs/maklerienv/local/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner
      41.             response = get_response(request)

    File "/home/milano/.virtualenvs/maklerienv/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/milano/.virtualenvs/maklerienv/local/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
      215.                 response = response.render()

    File "/home/milano/.virtualenvs/maklerienv/local/lib/python2.7/site-packages/django/template/response.py" in render
      107.             self.content = self.rendered_content

    File "/home/milano/.virtualenvs/maklerienv/local/lib/python2.7/site-packages/wkhtmltopdf/views.py" in rendered_content
      78.             cmd_options=cmd_options

    File "/home/milano/.virtualenvs/maklerienv/local/lib/python2.7/site-packages/wkhtmltopdf/utils.py" in render_pdf_from_template
      186.                           cmd_options=cmd_options)

    File "/home/milano/.virtualenvs/maklerienv/local/lib/python2.7/site-packages/wkhtmltopdf/utils.py" in convert_to_pdf
      124.     return wkhtmltopdf(pages=filename, **cmd_options)

    File "/home/milano/.virtualenvs/maklerienv/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 /render/
    Exception Value: Command '['/usr/bin/wkhtmltopdf', '--encoding', u'utf8', '--quiet', u'False', '/tmp/wkhtmltopdfRDyi61.html', '-']' returned non-zero exit status 1

你有什么想法吗?我试图安装最新的wkhtmltopdf版本,但它没有帮助。

我唯一的想法是,运行user的{​​{1}}没有wkhtmltopdf的权限,但我不知道。

编辑 - 视图

/tmp/

2 个答案:

答案 0 :(得分:0)

答案在这种情况下很简单,但很难调试:

我在WKHTMLTOPDF_CMD_OPTIONS = { 'quiet': True, } 中有一些设置:

WKHTMLTOPDF_CMD_OPTIONS = {
     'quiet': False,
}

然后,我将其改为:

False

默认情况下为false,当您明确指定时会导致错误。

感谢 chidg

Hi, try removing 'quiet': False from your options. The quiet option is false by default, so you only need to specify the option if you are making it true, and the mapping of this options dictionary to the command line options results in --quiet False being passed in the command, which is incorrect syntax.

答案 1 :(得分:0)

如果您的 macOS 版本是 Big Sur。将以下内容添加到 settings.py 文件中

WKHTMLTOPDF_CMD_OPTIONS = {
    'enable-local-file-access': True,
}