1

I'm getting errors while trying to create a .pdf file with my Flask application's pdfkit. The application does work on a local machine. The problems start to appear when I try to run the code on Digitalocean's Ubuntu 18.04 droplet (Nginx is used as a web server, Gunicorn (assisted by a supervisor) runs wsgi). That's the error (500) I'm getting inside of a browser:

The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

That's the error from the Gunicorn's error log:

[2019-07-27 14:44:56,969] ERROR in app: Exception on /store/resins/daylight/3 [POST]
Traceback (most recent call last):
  File "/home/slava/env/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/slava/env/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/slava/env/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/slava/env/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/home/slava/env/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/slava/env/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/slava/ag3d/ag3d/routes.py", line 184, in resin_store
    pdfkit.from_string(rendered, pdf_name, options = options)
  File "/home/slava/env/lib/python3.6/site-packages/pdfkit/api.py", line 72, in from_string
    return r.to_pdf(output_path)
  File "/home/slava/env/lib/python3.6/site-packages/pdfkit/pdfkit.py", line 159, in to_pdf
    raise IOError("wkhtmltopdf exited with non-zero code {0}. error:\n{1}".format(exit_code, stderr))
OSError: wkhtmltopdf exited with non-zero code 1. error:
qt.qpa.screen: QXcbConnection: Could not connect to display 
Could not connect to any X display.

This is how the application uses pdfkit:

options = {
  'page-size': 'A4',
  'margin-top': '0.75in',
  'margin-right': '0.75in',
  'margin-bottom': '0.75in',
  'margin-left': '0.75in',
}
rendered = render_template('order_doc.html', pdf_resin = pdf_resin)
pdfkit.from_string(rendered, pdf_name, options = options)
davidism
  • 121,510
  • 29
  • 395
  • 339

1 Answers1

0

The PDFKit package makes use of wkhtmltopdf, which in turn needs an X Server to run. In Debian land, this generally requires xvfb-run wrapper.

The headless_pdfkit package tries to make the hotfix proposed by jakewins a bit easier to work with.

You can install headless_pdfkit by running:

pip install headless-pdfkit

Save a simple PDF from string.:

from headless_pdfkit import generate_pdf

ret = generate_pdf('<html></html>')
with open('output.pdf', 'wb') as w:
    w.write(ret)

options = {
    'auto_servernum': ''
}

ret = generate_pdf('<html></html>', options=options)
with open('output.pdf', 'wb') as w:
    w.write(ret)
Talha Junaid
  • 2,351
  • 20
  • 29