1
我正在开发Django网站上的发票PDF生成器。我使用xhtml2pdf
。它似乎有效,但编码不正确。当我使用变音符时,有错误的符号/字符。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>
你知道如何使它正常工作?
我试着和它的作品。唯一的问题是它不适用于{%static“...”%}。它想要绝对路径。有没有办法如何使它与相对路径一起工作或使用类似于静态的快捷方式放置绝对路径?我还没有处于开发模式,所以我的静态文件夹被分成应用程序,所以我不能做类似{{STATIC_ROOT}} {%static“...”%} –
你可以尝试'link_callback'来添加url http ://xhtml2pdf.readthedocs.io/en/stable/usage.html#using-xhtml2pdf-in-django,希望它能帮助你 –