Search code examples
pythondjangopdfwkhtmltopdfdjango-wkhtmltopdf

Django - wkhmltopdf - CalledProcessError (only works with sudo from command line)


I'm trying to render PDF from Django template using django-wkhtmltopdf. The problem is that wkhtmltopdf started raising:

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):

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 wkhtmltopdf http://www.google.com g.pdf

I also noticed that some of the temporary html files from /tmp/ folder wasn't deleted.

This is a whole traceback Django returns:

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.

My only idea is that user which runs wkhtmltopdf doesn't have privileges to /tmp/ but I don't know.

EDIT - the view

class PDFAgreementView(PDFTemplateView):
    template_name = 'agreements/pdf_template.html'
    filename = 'rendered.pdf'

Solution

  • The answer was in this case simple but hard to debug:

    I had some settings in 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.

    Thanks to 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.