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,希望它能幫助你 –