在开发服务器上,我可以创建一个pdf,然后使用pdfkit将其发送给最终用户
我的返回pdf的视图
@changesBP.route('/pdf', methods=['GET'])
def pdfView():
if not 'dataDict' in session:
flash('Du skal uploade regneark for at kunne fortsætte på siden')
return redirect(url_for('indexBP.indexView'))
balanceChangelist = session['balanceGruppeChanges']
balanceGrupper = balanceData(data = session['dataDict'])
balanceListe = session['balanceListe']
resultatChangelist = session['resultatGruppeChanges']
resultatGrupper = resultatData(data = session['dataDict'])
resultatListe = session['resultatListe']
aendingsLister = []
for bc in balanceChangelist:
for c in bc['changes']:
if c['list'] not in aendingsLister:
aendingsLister.append(c['list'])
for rc in resultatChangelist:
for c in rc['changes']:
if c['list'] not in aendingsLister:
aendingsLister.append(c['list'])
options = {
'page-size': 'Letter',
'margin-top': '0.75in',
'margin-right': '0.75in',
'margin-bottom': '0.75in',
'margin-left': '0.75in',
'encoding': "UTF-8",
'no-outline': None,
'quiet': ''
}
html = render_template('pdfTemplate.html', balanceChangelist = balanceChangelist,
balanceGrupper = balanceGrupper,
balanceListe = balanceListe,
resultatChangelist = resultatChangelist,
resultatGrupper = resultatGrupper,
resultatListe = resultatListe,
aendingsLister = aendingsLister)
out_File = BytesIO()
out_File.write(pdfkit.from_string(html, False, options=options))
out_File.seek(0)
return send_file(out_File,
attachment_filename='out.pdf',
as_attachment=True,
mimetype="application/pdf")
[在this guide之后通过gunicorn和Nginx投放应用程序时,访问pdf视图时收到错误的网关错误。
我可以在错误日志中使用一些帮助,指出以下内容
2017/12/13 10:04:39 [错误] 28047#28047:* 78提前过早从客户端读取响应标头时关闭连接:10.2.20.180,服务器:10.8.0.12,请求:“ GET / pdf HTTP / 1.1”,上游:http://unix:/home/hepo/segesdupont/segesdupont.sock:/pdf”,主机:“ 10.8.0.12”,引荐来源网址:“ http://10.8.0.12/changes”
有什么想法吗?
此问题的内部错误是“ UnicodeEncodeError:'ascii'编解码器无法在位置193编码字符'\ xea':序号不在range(128)中。”
您可以通过从用于生成pdf的html中删除所有特殊字符来解决此问题。使用á代替á。
诊断元素:
可能的问题:服务器上的locale
配置错误。
解决方案:
STEP 1:尝试观看您的locales
。在您的终端中输入:
$ locale //DISPLAYING CURRENT SETTINGS
$ locale -a //DISPLAYING AVAILABLE LOCALES
如果您的语言环境定义正确,我想您会得到的结果是,日耳曼语系的代码行类似于LANG=de_DE.UTF-8
,否则需要to reconfigure your locales。
STEP 2:通过python解释器检查locales
:
>>> import locale
>>> locale.getlocale()
>>> locale.getdefaultlocale()
这些命令应该在python的上下文中通知您locale
的状态。您还必须在应用程序的特定上下文中查看locale
的状态。
检查并最终更正后,您必须调整用于服务应用程序的脚本。
例如,我使用supervisor
来管理Nginx + Gunicorn服务的应用程序。这是我的配置文件,如下所示:
[progam:myapp]
environment=LANG="fr_FR.UTF-8"
// OTHERS COMMANDS...
结论:
将行environment=LANG="de_DE.UTF-8"
添加到配置脚本中(init.d
等)。或支持您的语言的任何配置;可能是您的locale system