有没有办法从django模板生成包含比萨的非ASCII符号的pdf? [英] Is there a way to generate pdf containing non-ascii symbols with pisa from django template?

查看:110
本文介绍了有没有办法从django模板生成包含比萨的非ASCII符号的pdf?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用此代码段从模板生成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)),结果)
如果不是pdf.err:
返回http.HttpResponse(result.getvalue(),mimetype ='application / pdf')
除了异常('PDF错误')

所有非拉丁符号不正确显示,使用utf-8编码保存模板和视图。



我已尝试将视图保存为ANSI,然后再将用户unicode(html,UTF- 8),但它会引发TypeError。



我也认为这可能是因为默认字体不支持utf-8
,所以根据比萨文件我试图在样式部分的模板体中设置fontface n。



仍然没有结果。



有没有人有一些想法如何解决这个问题?

解决方案

这对我有用:

 code> pdf = pisa.pisaDocument(StringIO.StringIO(html.encode(UTF-8)),result,encoding ='UTF-8')
pre>

I'm trying to generate a pdf from template using this snippet:

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')

All non-latin symbols are not showing correctly, the template and view are saved using utf-8 encoding.

I've tried saving view as ANSI and then to user unicode(html,"UTF-8"), but it throws TypeError.

Also I thought that maybe it's because the default fonts somehow do not support utf-8 so according to pisa documentation I tried to set fontface in template body in style section.

That still gave no results.

Does any one have some ideas how to solve this issue?

解决方案

This does work for me:

pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("UTF-8")), result, encoding='UTF-8')

这篇关于有没有办法从django模板生成包含比萨的非ASCII符号的pdf?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆