I'm working on a Django project where we generate PDF files from HTML template. I use django-pdfkit
to render PDF.
On my Ubuntu 16.04
everything works correctly but when I deploy the project on DigitalOcean Ubuntu 16.04 server, it raises error:
IOError at /render/doklad/ wkhtmltopdf exited with non-zero code -6. error:
QXcbConnection: Could not connect to display
I tried to install
sudo apt-get install libxrender1 fontconfig xvfb
Set WKHTMLTOPDF_BIN
variable to correct path.
Changed gunicorn
user to root.
pip install wkhtmltopdf
Nothing helped, do you know what I can do to solve this?
EDIT:
I've found out that if I do this command as a root, it works correctly:
wkhtmltopdf http://www.google.com google.pdf
But the problem is if I run the command using django
user. It returns the same thing:
QXcbConnection: Could not connect to display
Aborted (core dumped)
So I tried to change gunicorn.service
so gunicorn is running under root
instead of django
but it didn't help.
http://mypage/render/doklad/?id=1
returns the same error.
The error seems to be raised in this file:
https://github.com/JazzCore/python-pdfkit/blob/master/pdfkit/pdfkit.py
And this is a traceback:
Aug 25 04:42:46 homeit-generator-faktur-beta gunicorn[29032]: - - [25/Aug/2017:04:42:46 +0000] "GET / HTTP/1.0" 302 - "-" "Mozilla/5.0"
Aug 25 06:05:55 homeit-generator-faktur-beta gunicorn[29032]: Internal Server Error: /render/doklad/
Aug 25 06:05:55 homeit-generator-faktur-beta gunicorn[29032]: Traceback (most recent call last):
Aug 25 06:05:55 homeit-generator-faktur-beta gunicorn[29032]: File "/home/django/homeit/homeitvenv/local/lib/python2.7/site-packages/django/cor
Aug 25 06:05:55 homeit-generator-faktur-beta gunicorn[29032]: response = get_response(request)
Aug 25 06:05:55 homeit-generator-faktur-beta gunicorn[29032]: File "/home/django/homeit/homeitvenv/local/lib/python2.7/site-packages/django/cor
Aug 25 06:05:55 homeit-generator-faktur-beta gunicorn[29032]: response = self.process_exception_by_middleware(e, request)
Aug 25 06:05:55 homeit-generator-faktur-beta gunicorn[29032]: File "/home/django/homeit/homeitvenv/local/lib/python2.7/site-packages/django/cor
Aug 25 06:05:55 homeit-generator-faktur-beta gunicorn[29032]: response = wrapped_callback(request, *callback_args, **callback_kwargs)
Aug 25 06:05:55 homeit-generator-faktur-beta gunicorn[29032]: File "/home/django/homeit/homeitvenv/local/lib/python2.7/site-packages/django/vie
Aug 25 06:05:55 homeit-generator-faktur-beta gunicorn[29032]: return self.dispatch(request, *args, **kwargs)
Aug 25 06:05:55 homeit-generator-faktur-beta gunicorn[29032]: File "/home/django/homeit/homeitvenv/local/lib/python2.7/site-packages/django/vie
Aug 25 06:05:55 homeit-generator-faktur-beta gunicorn[29032]: return handler(request, *args, **kwargs)
Aug 25 06:05:55 homeit-generator-faktur-beta gunicorn[29032]: File "/home/django/homeit/dashboard/views.py", line 186, in get
Aug 25 06:05:55 homeit-generator-faktur-beta gunicorn[29032]: return super(DokladToPdf, self).get(*args, **kwargs)
Aug 25 06:05:55 homeit-generator-faktur-beta gunicorn[29032]: File "/home/django/homeit/homeitvenv/local/lib/python2.7/site-packages/django_pd
Aug 25 06:05:55 homeit-generator-faktur-beta gunicorn[29032]: content = self.render_pdf(*args, **kwargs)
Aug 25 06:05:55 homeit-generator-faktur-beta gunicorn[29032]: File "/home/django/homeit/homeitvenv/local/lib/python2.7/site-packages/django_pd
Aug 25 06:05:55 homeit-generator-faktur-beta gunicorn[29032]: pdf = pdfkit.from_string(html, False, options, **kwargs)
Aug 25 06:05:55 homeit-generator-faktur-beta gunicorn[29032]: File "/home/django/homeit/homeitvenv/local/lib/python2.7/site-packages/pdfkit/ap
Aug 25 06:05:55 homeit-generator-faktur-beta gunicorn[29032]: return r.to_pdf(output_path)
Aug 25 06:05:55 homeit-generator-faktur-beta gunicorn[29032]: File "/home/django/homeit/homeitvenv/local/lib/python2.7/site-packages/pdfkit/pd
Aug 25 06:05:55 homeit-generator-faktur-beta gunicorn[29032]: raise IOError("wkhtmltopdf exited with non-zero code {0}. error:\n{1}".format(
Aug 25 06:05:55 homeit-generator-faktur-beta gunicorn[29032]: IOError: wkhtmltopdf exited with non-zero code -6. error:
Aug 25 06:05:55 homeit-generator-faktur-beta gunicorn[29032]: QXcbConnection: Could not connect to display
Aug 25 06:05:55 homeit-generator-faktur-beta gunicorn[29032]: - - [25/Aug/2017:06:05:55 +0000] "GET /render/doklad/?id=1 HTTP/1.0" 500 108058 "
Are you running your commmand through xvfb-run
?
In your calling code you may need to replace straight-up calls to wkhtmltopdf
with something more like this:
xvfb-run -a -s "-screen 0 1024x768x16" /path/to/bin/wkhtmltopdf