我试图用这个片段来生成模板PDF:有没有办法使用django模板生成包含非ASCII字符的pdf格式的比萨?
def write_pdf(template_src, context_dict):
template = get_template(template_src)
context = 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 http.HttpResponse(result.getvalue(), mimetype='application/pdf')
except Exception('PDF error')
所有非拉丁字符的显示不正确,模板和视图使用UTF-8编码保存的。
我试着将视图保存为ANSI,然后保存为用户unicode(html,“UTF-8”),但它会引发TypeError。
另外我想也许这是因为默认字体莫名其妙地不支持utf-8 ,所以根据比萨文档,我尝试在样式部分设置模板体中的fontface。
这仍然没有结果。
有没有人有一些想法如何解决这个问题?
替换后得到pdf完整的不可读数据, 检查了解决方案,该方法与我使用的没有多大差别,但我仍然使用html.encode(“ISO-8859-1”)对其进行测试抛出: “拉丁-1”编解码器无法编码字符u'\ ufeff'在位置0:序号不在范围(256)“ 与编码(”UTF-8“)它返回pdf,但与我一样之前有过 - 非ascii符号仍然没有正确渲染。 – mihailt 2009-10-28 13:51:09
@mihailt你找到了解决方案吗? – 2014-05-09 20:05:46
正好加上这个:'encoding ='UTF-8''解决了我编码波兰字符的问题 – andi 2014-08-22 20:58:00