gpt4 book ai didi

Django - pdf 响应编码错误 - xhtml2pdf

转载 作者:行者123 更新时间:2023-12-03 16:25:44 24 4
gpt4 key购买 nike

我正在我的 Django 网站上开发发票 PDF 生成器。我用 xhtml2pdf .它似乎有效,但编码不正确。当我使用变音符号时有错误的符号/字符。

这是一个 View :

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:
enter image description here

你知道如何让它正常工作吗?

最佳答案

尝试将字体 url 添加到您的 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,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45689236/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com