Question

I'm using wkhtmltopdf together with the django-wkhtmltopdf wrapper to create a .pdf of a template.

I'm using the example from the django-wkhtmltopdf documentation (though I eventually want more than just a static template):

url(r'^pipeline/snapshot/$', PDFTemplateView.as_view(
    template_name='pdf/pipeline_snapshot.html',
    filename='my_pdf.pdf'), name='pdf'),

And I'm getting the error:

Traceback:
File "/home/pluscitizen/webapps/odin/lib/python2.7/django/core/handlers/base.py" in get_response
  140.                     response = response.render()
File "/home/pluscitizen/webapps/odin/lib/python2.7/django/template/response.py" in render
  105.             self.content = self.rendered_content
File "/home/pluscitizen/webapps/odin/lib/python2.7/django_wkhtmltopdf-1.2.2-py2.7.egg/wkhtmltopdf/views.py" in rendered_content
  144.                                        footer_filename=footer_filename)
File "/home/pluscitizen/webapps/odin/lib/python2.7/django_wkhtmltopdf-1.2.2-py2.7.egg/wkhtmltopdf/views.py" in convert_to_pdf
  103.         return wkhtmltopdf(pages=[filename], **cmd_options)
File "/home/pluscitizen/webapps/odin/lib/python2.7/django_wkhtmltopdf-1.2.2-py2.7.egg/wkhtmltopdf/utils.py" in wkhtmltopdf
  92.     return check_output(ck_args, **ck_kwargs)
File "/usr/local/lib/python2.7/subprocess.py" in check_output
  575.         raise CalledProcessError(retcode, cmd, output=output)

Exception Type: CalledProcessError at /pipeline/snapshot/
Exception Value: Command '['wkhtmltopdf', '--encoding', u'utf8', '--quiet', '/tmp/wkhtmltopdfVaAKrX.html', '-']' returned non-zero exit status 127

However, when I run the same command, with the same file, from the Django shell

>>> subprocess.check_output(['wkhtmltopdf', '--encoding', u'utf8', '--quiet', '/tmp/wkhtmltopdfSGFfYh.html', '-'])

everything runs fine. Ditto for:

>>> wkhtmltopdf(['/tmp/wkhtmltopdfSGFfYh.html'], **{})

So, figuring that the difference must be in the whole shell situation, I've tried adding a shell=True to the call to subprocess within django-wkhtmltopdf (I know it's a security problem), but no luck there. Probably because I have no idea what's really going on.

I saw somewhere saying that the problem might have to do with PATHs not being set up properly, but then I don't understand why Django's shell is not having issues with this.

This entire process has been incredibly taxing, and now that I'm so close, I'm finally turning to SO for an answer.

EDIT: I have tried running the subprocess directly in a view, and it returns the same error, which I guess means that the shell and the server aren't necessarily both running from the same environment?

FIXED: Solved, and the answer by sparks led me on the way. I decided to look at the logs (duh) output by the django app on the server, and noticed that before the traceback, I'm getting the actual output of the command:

wkhtmltopdf: error while loading shared libraries: libwkhtmltox.so.0: cannot open shared object file: No such file or directory

Which seems to imply that it's not finding the libraries I have in /home/user/lib. When I explicitly added these to the WKHTMLTOPDF_ENV variable in settings.py, everything worked swimmingly.

Was it helpful?

Solution

exit status 127

This means that the command you're trying to run can't be found. It is an error in the environment of your process.

I would suggest looking at the contents of sys.path in python and comparing that to the PATH environment variable, or compared to sys.path in the django console.

OTHER TIPS

More specifically, add this to settings.py:

WKHTMLTOPDF_ENV = {'FONTCONFIG_PATH': '/etc/fonts'}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top