flask - 由于网络错误而退出代码 1:ProtocolUnknownError
问题描述
from flask import Flask, render_template, make_response
import pdfkit
app = Flask(__name__)
@app.route('/<Customer_Full_Name>/<Customer_adress>/<customerID_number>/<CurrentDate>/<Digital_Signature>')
def pdf_template(Customer_Full_Name, Customer_adress, customerID_number,CurrentDate,Digital_Signature):
rendered = render_template('pdf_template.html', Customer_Full_Name=Customer_Full_Name,Customer_adress=Customer_adress,customerID_number=customerID_number, CurrentDate=CurrentDate, Digital_Signature=Digital_Signature)
pdf = pdfkit.from_string(rendered)
response = make_response(pdf, False)
response.headers['Content-Type'] = 'application/pdf'
response.headers['Content-Disposition'] = 'inline;filename=output'
return response
# return render_template('pdf_template.html')
if __name__ == '__main__':
app.run(debug=True)
127.0.0.1 - - [2021 年 11 月 11 日 16:56:58] “GET /Customer_Fl_Name/Customedress/customD_number/CurntDate/Dil_Signature HTTP/1.1”500 -
flask run
输出:
* Serving Flask app 'app.py' (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
[2021-11-11 16:58:20,679] ERROR in app: Exception on /Customer_Fl_Name/Customedress/customD_number/CurntDate/Dil_Signature [GET]
Traceback (most recent call last):
File "/Users/swapnil/Desktop/Clinsj_Certificate/venv/lib/python3.7/site-packages/flask/app.py", line 2073, in wsgi_app
response = self.full_dispatch_request()
File "/Users/swapnil/Desktop/Clinsj_Certificate/venv/lib/python3.7/site-packages/flask/app.py", line 1518, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/swapnil/Desktop/Clinsj_Certificate/venv/lib/python3.7/site-packages/flask/app.py", line 1516, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/swapnil/Desktop/Clinsj_Certificate/venv/lib/python3.7/site-packages/flask/app.py", line 1502, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File "/Users/swapnil/Desktop/Clinsj_Certificate/app.py", line 11, in pdf_template
pdf = pdfkit.from_string(rendered, False)
File "/Users/swapnil/Desktop/Clinsj_Certificate/venv/lib/python3.7/site-packages/pdfkit/api.py", line 72, in from_string
return r.to_pdf(output_path)
File "/Users/swapnil/Desktop/Clinsj_Certificate/venv/lib/python3.7/site-packages/pdfkit/pdfkit.py", line 156, in to_pdf
raise IOError('wkhtmltopdf reported an error:\n' + stderr)
OSError: wkhtmltopdf reported an error:
Loading pages (1/6)
Warning: Blocked access to file
Warning: Blocked access to file
Error: Failed to load about:blank, with network status code 301 and http status code 0 - Protocol "about" is unknown
Error: Failed to load about:blank, with network status code 301 and http status code 0 - Protocol "about" is unknown
Counting pages (2/6)
Resolving links (4/6)
Loading headers and footers (5/6)
Printing pages (6/6)
Done
Exit with code 1 due to network error: ProtocolUnknownError
解决方案
您应该尝试添加此选项:
"enable-local-file-access": ""
到你的选项字典
斯蒂芬妮
推荐阅读
- c# - 如何将 c# 中的 protobuf 消息反序列化,通过套接字读入字节数组?
- asp.net-core - IdentityServer4如何访问/connect/token请求中的参数
- django - 这个解决PASSWORD_RESET_TIMEOUT_DAYS的黑客不能设置为不到一天的安全吗?
- python - Python中有没有类似Rstudio的帮助部分?
- javascript - 在一个简单的函数中反应 js 错误,该函数接受一个数组并返回一个随机值
- c# - Restsharp补丁请求
- gcc - 如何在文件系统中找到可变参数宏实现
- c# - 如何将数据库内容添加到 ViewModel 中声明的变量中
- asterisk - Asterisk PBX 播放消息“此呼叫可能被录音...”同时分机响铃
- python - 用于计算平均课堂笔记的 Python 脚本