ReportLab Django不渲染汉字 [英] ReportLab Django Not Rendering Chinese Characters

查看:43
本文介绍了ReportLab Django不渲染汉字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难使ReportLab呈现汉字.从我查看过的所有内容中,人们都说这可能是字体问题,但是我使用了许多不同的字体,甚至似乎根本没有使用它们.汉字总是以黑色方块的形式出现.以下是我所拥有的一些示例代码.

I'm having difficulty making ReportLab render Chinese Characters. From everything I've looked up people are saying that it is probably a font problem but I've used a lot of different fonts and it doesn't even seem to be using them at all. The Chinese characters always just come out as black squares. Below is some sample code of what I have.

# -*- coding: utf8 -*-
from reportlab.lib.pagesizes import letter
from reportlab.pdfbase.ttfonts import TTFont
from io import BytesIO

pdfmetrics.registerFont(TTFont('Arial', 'arial.ttf', 'UTF-8'))

buffer = BytesIO()
doc = SimpleDocTemplate(buffer,
                        rightMargin=inch*0.5,  # 1/2 Inch
                        leftMargin=inch*0.5,  # 1/2 Inch
                        bottomMargin=0,
                        topMargin=inch*0.375,  # 3/8 Inch
                        pagesize=letter)
# Get Styles
styles = getSampleStyleSheet()
# Custom Style
styles.add(ParagraphStyle(name='Address', font='Arial', fontSize=8))

elements = []
elements.append(Paragraph(u'6905\u897f\u963f\u79d1\u8857\uff0c\u5927\u53a6\uff03\u5927', styles['Address']))

doc.build(elements)

# Get the value of the BytesIO buffer and write it to the response.
pdf = buffer.getvalue()
buffer.close()
return pdf

我正在使用在字体文件夹中的Ubuntu 12.04安装上找到的arial.ttf字体.我还尝试了安装在这台计算机上的其他字体,即使在数字上,它们的外观也完全相同,而且所有汉字都不是黑色方块.

I'm using an arial.ttf font found on my Ubuntu 12.04 installation in the fonts folder. I have also tried other fonts installed on this machine and all have exactly the same look even on the numbers and none of the Chinese characters are anything other than black squares.

我是否注册了字体错误,即使开头的数字也无法正确打印?是什么原因导致黑色方块出现?

Am I registering fonts wrong if even the numbers at the beginning aren't printing correctly? What could be causing the black squares?

推荐答案

已解决.事实证明,在您的ParagraphStyle中,它必须是fontName ="Arial"而不是font ="Arial",但我确实学到了一些其他技巧,可以使其在下面的其他方式下工作.

Solved it. Turns out in your ParagraphStyle it needs to be fontName="Arial" not font="Arial" but I did learn some other tricks of getting it to work in other ways below.

styles.add(ParagraphStyle(name='Address', fontName='Arial')

进行一些挖掘后,我学到了一些东西,希望对这种情况的其他人有所帮助.当您在Unicode文本周围的段落中添加标签并将其显式设置为字体时,它将起作用.

After doing some digging I've learned a few things that I hope helps someone else in this situation. When you add the tags inside of your Paragraph around the Unicode text and set it explicitly to a font it will work.

elements.append(Paragraph(u'<font name="Arial">6905\u897f\u963f\u79d1\u8857\uff0c\u5927\u53a6\uff03\u5927</font>', styles['Address']))

这至少解决了具有各种字体的段落的问题.再次,此代码将起作用.

This fixes the problem at least for Paragraphs with various fonts. Again this code will work.

这篇关于ReportLab Django不渲染汉字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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