【发布时间】:2020-01-20 06:48:38
【问题描述】:
我正在尝试通过 pdfkit 库从 html 模板生成 pdf 文件。一切正常,期待使用本地字母表中的某些特殊字符的情况(例如,在这种情况下为“Ž”)。然后出现如下错误:
UnicodeEncodeError: 'latin-1' codec can't encode character '\u017d' in 位置 57:序数不在范围内(256)
代码:
if request.form['action'] == 'Print':
config = pdfkit.configuration(wkhtmltopdf=path_wkhtmltopdf)
rendered = render_template('pdf.html')
pdf = pdfkit.from_string(rendered, False, configuration=config)
response = make_response(pdf)
response.headers['Content-Type'] = 'application/pdf'
response.headers['Content-Disposition'] = 'attachment; filename='filename.pdf'
return response
html模板中也定义了charset:<meta charset="utf-8">
您知道什么是根本原因以及如何解决它吗?
【问题讨论】:
标签: python flask character-encoding pdf-generation