有没有办法用django模板中的pisa生成包含非ascii符号的pdf?

mih*_*ilt 19 python pdf django pisa

我正在尝试使用此代码段从模板生成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')
Run Code Online (Sandbox Code Playgroud)

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

我已经尝试将视图保存为ANSI,然后保存到用户unicode(html,"UTF-8"),但它会抛出TypeError.

另外我想也许是因为默认字体不支持utf-8所以根据比萨文档我试图在样式部分的模板体中设置字体.

那仍然没有结果.

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

yed*_*tko 29

这对我有用:

pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("UTF-8")), result, encoding='UTF-8')
Run Code Online (Sandbox Code Playgroud)