[英]Django - wkhmltopdf - CalledProcessError (only works with sudo from command line)
I'm trying to render PDF from Django template using django-wkhtmltopdf
.我正在尝试使用
django-wkhtmltopdf
从 Django 模板渲染 PDF。 The problem is that wkhtmltopdf
started raising:问题是
wkhtmltopdf
开始提出:
Command '['/usr/bin/wkhtmltopdf', '--encoding', u'utf8', '--quiet', u'False', '/tmp/wkhtmltopdfRDyi61.html', '-']' returned non-zero exit status 1
Can't figure out where the problem is but I noticed that I can't even do in command line (I'm in virtualenv):无法弄清楚问题出在哪里,但我注意到我什至无法在命令行中执行(我在 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.
It works only with sudo
privileges (but I'm in PyCharmProject my users directory):它仅适用于
sudo
权限(但我在 PyCharmProject 我的用户目录中):
sudo wkhtmltopdf http://www.google.com g.pdf
I also noticed that some of the temporary html
files from /tmp/
folder wasn't deleted.我还注意到
/tmp/
文件夹中的一些临时html
文件没有被删除。
This is a whole traceback Django returns:这是 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
Do you have any ideas?你有什么想法? I tried to install newest
wkhtmltopdf
version but it didn't help.我尝试安装最新的
wkhtmltopdf
版本,但没有帮助。
My only idea is that user
which runs wkhtmltopdf
doesn't have privileges to /tmp/
but I don't know.我唯一的想法是运行
wkhtmltopdf
user
没有/tmp/
权限,但我不知道。
EDIT - the view编辑 - 视图
class PDFAgreementView(PDFTemplateView):
template_name = 'agreements/pdf_template.html'
filename = 'rendered.pdf'
The answer was in this case simple but hard to debug:在这种情况下,答案很简单,但很难调试:
I had some settings in settings.py
:我在 settings.py 中有一些
settings.py
:
WKHTMLTOPDF_CMD_OPTIONS = {
'quiet': True,
}
Then, I changed it to:然后,我将其更改为:
WKHTMLTOPDF_CMD_OPTIONS = {
'quiet': False,
}
Which is False
by default and it causes error when you specify it explicitely.默认情况下为
False
,当您明确指定时会导致错误。
Thanks to chidg :感谢chidg :
Hi, try removing 'quiet': False from your options. 您好,请尝试从您的选项中删除 'quiet': False。 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.
quiet 选项默认为 false,因此如果您将其设为 true,则只需要指定该选项,并且将此选项字典映射到命令行选项会导致命令中传递 --quiet False,这是不正确的句法。
If your version of macOs is Big Sur.如果您的 macOS 版本是 Big Sur。 Add the following to your settings.py file
将以下内容添加到 settings.py 文件中
WKHTMLTOPDF_CMD_OPTIONS = {
'enable-local-file-access': True,
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.