【问题标题】:Wkhtmltopdf (pdfkit) - QXcbConnection: Could not connect to displayWkhtmltopdf (pdfkit) - QXcbConnection: 无法连接到显示器
【发布时间】:2017-08-24 21:46:21
【问题描述】:

我正在开发一个 Django 项目,我们从 HTML 模板生成 PDF 文件。我使用django-pdfkit 来渲染PDF。

在我的Ubuntu 16.04 上一切正常,但是当我在 DigitalOcean Ubuntu 16.04 服务器上部署项目时,它会引发错误:

/render/doklad/wkhtmltopdf 处的 IOError 以非零代码 -6 退出。 错误:

QXcbConnection:无法连接到显示器

我尝试安装

sudo apt-get install libxrender1 fontconfig xvfb

WKHTMLTOPDF_BIN 变量设置为正确的路径。

gunicorn 用户更改为 root。

pip install wkhtmltopdf

没有任何帮助,你知道我能做些什么来解决这个问题吗?

编辑:

我发现如果我以 root 身份执行此命令,它可以正常工作:

wkhtmltopdf http://www.google.com google.pdf

但问题是如果我使用django 用户运行命令。它返回相同的东西:

QXcbConnection: Could not connect to display 
Aborted (core dumped)

所以我尝试更改 gunicorn.service 以便 gunicorn 在 root 而不是 django 下运行,但它没有帮助。

http://mypage/render/doklad/?id=1 

返回相同的错误。

这个文件中似乎出现了错误:

https://github.com/JazzCore/python-pdfkit/blob/master/pdfkit/pdfkit.py

这是一个回溯

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 "

【问题讨论】:

  • 请注意,在数字海洋中,如果您通过 SSH 连接,则无法访问屏幕,您必须使用 -X 选项,即:ssh -X user@ip-address

标签: python django ubuntu wkhtmltopdf python-pdfkit


【解决方案1】:

您是否通过xvfb-run 运行您的命令?

在您的调用代码中,您可能需要将直接调用 wkhtmltopdf 替换为类似以下内容:

xvfb-run -a -s "-screen 0 1024x768x16" /path/to/bin/wkhtmltopdf

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 2017-11-26
    • 2013-08-01
    • 1970-01-01
    • 2017-04-10
    • 1970-01-01
    • 2012-03-25
    相关资源
    最近更新 更多