ReportLab Django Not Rendering Chinese Characters

家住魔仙堡 提交于 2021-02-08 10:25:03

问题


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

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?


回答1:


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

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.




回答2:


Choose the fonts that supports Chinese characters.

In Ubuntu, I choose "AR PL UMing CN" for example.

My code snippets:

# -*- coding: utf-8 -*-
...
pdfmetrics.registerFont(TTFont('AR PL UMing CN', 'uming.ttc'))
styles = getSampleStyleSheet()

...
styles.add(ParagraphStyle(name='Chinese', fontName='AR PL UMing CN',    fontSize=20))

elements=[]
elements.append(Paragraph("成”, styles['Chinese']))
doc.build(elements)
...

I can even change to Chinese editor and type in the character straight off. Hope this helps.



来源:https://stackoverflow.com/questions/21465540/reportlab-django-not-rendering-chinese-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!