Django - pdf 响应编码错误 - xhtml2pdf
Django - pdf response has wrong encoding - xhtml2pdf
我正在我的 Django 网站上开发发票 PDF 生成器。我使用 xhtml2pdf
。它似乎有效,但编码不正确。当我使用变音符号时,有错误的 signs/characters。
这是一个视图:
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:
你知道如何让它正常工作吗?
尝试将字体网址添加到您的 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>
我正在我的 Django 网站上开发发票 PDF 生成器。我使用 xhtml2pdf
。它似乎有效,但编码不正确。当我使用变音符号时,有错误的 signs/characters。
这是一个视图:
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:
你知道如何让它正常工作吗?
尝试将字体网址添加到您的 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>