Flask:如何消除造成500e的pdfkit

2024-09-29 21:34:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用Flask应用程序的pdfkit创建一个.pdf文件时出错。应用程序在本地计算机上运行。当我试图在Digitalocean的Ubuntu18.04Droplet上运行代码时,问题就开始出现了(Nginx被用作一个web服务器,Gunicorn(在一个主管的协助下)运行wsgi)。这是我进入浏览器的错误(500):

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.

这是Gunicorn错误日志中的错误:

[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.

应用程序使用pdfkit的方式如下:

^{pr2}$

Tags: toinpyenvappflaskhomerequest
1条回答
网友
1楼 · 发布于 2024-09-29 21:34:10

PDFKit包使用wkhtmltopdf,它反过来需要一个X服务器来运行。在Debian land中,这通常需要xvfb-run包装器。在

headless_pdfkit包试图使jakewins提出的hotfix更易于使用。在

您可以通过运行以下命令来安装headless_pdfkit

^{1}$

从字符串保存一个简单的PDF文件:

^{pr2}$

相关问题 更多 >

    热门问题