Django-pdf响应编码错误-xhtml2pdf [英] Django - pdf response has wrong encoding - xhtml2pdf
本文介绍了Django-pdf响应编码错误-xhtml2pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在Django网站上开发发票PDF生成器。我使用 xhtml2pdf
。似乎可行,但编码不正确。使用变音符号时出现错误的符号/字符。
I'm working on an invoice PDF generator on my Django website. I use xhtml2pdf
. It seems to be working but encodings is not correct. There are wrong signs/characters when I use diacritics.
这是一个视图:
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:
This is the generated pdf:
您知道如何使其正常工作吗?
Do you know how to make it work correctly?
推荐答案
尝试将字体网址添加到您的html中,不要忘记替换路径和名称
Try add font urls to your html, don't forget to replace path and name
<!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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文