我正在我的Django网站上开发一个发票PDF生成器。我使用xhtml2pdf。它似乎可以工作,但编码不正确。使用变音符号时出现错误的符号/字符。
这是一个视图:
def render_to_pdf(template_src, context_dict):
template = get_template("pdf/pdf.html")
context = context_dict
html = template.render(context)
result = StringIO.StringIO()
pdf = pisa.pisaDocument(StringIO.StringIO(html.encode('utf-8'), result)
if not pdf.err:
return HttpResponse(result.getvalue(), content_type='application/pdf; encoding="utf-8"')
return HttpResponse('We had some errors<pre>%s</pre>' % escape(html))这是html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>title</title>
</head>
<body>
<p>Č š ž Ž x y ľ ĺ ó</p>
</body>
</html>这是生成的pdf:

你知道如何让它正常工作吗?
发布于 2017-08-15 17:09:18
尝试添加字体urls到您的html,不要忘记替换路径和名称
<!DOCTYPE html>
<html>
<head>
<style>
@font-face {
font-family: FreeSans;
src: url("/usr/share/fonts/truetype/freefont/FreeSans.ttf");
}
body {
font-family: FreeSans;
}
</style>
<meta charset="UTF-8">
<title>title</title>
</head>
<body>
<p>Č š ž Ž x y ľ ĺ ó</p>
</body>
</html>https://stackoverflow.com/questions/45689236
复制相似问题