ReportLab Django不渲染汉字(ReportLab Django Not Rendering Chinese Characters)

系统教程 行业动态 更新时间:2024-06-14 16:57:18
ReportLab Django不渲染汉字(ReportLab Django Not Rendering Chinese Characters) python

我很难使ReportLab渲染中文字符。 从我查找过的所有内容来看,人们都说它可能是一个字体问题,但我使用了很多不同的字体,它们似乎根本就没有使用它们。 汉字总是以黑色方块出现。 下面是我的一些示例代码。

# -*- 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

我正在使用在fonts文件夹中安装的Ubuntu 12.04上找到的arial.ttf字体。 我也试过在这台机器上安装了其他字体,即使在数字上也都有完全相同的外观,除了黑色方块外,没有任何汉字。

即使开头的数字打印不正确,我是否错误地注册了字体? 什么可能导致黑色方块?

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?

最满意答案

解决了它。 在你的ParagraphStyle中,它需要是fontName =“Arial”而不是font =“Arial”,但我确实学到了一些其他技巧,让它在下面以其他方式工作。

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

在做了一些挖掘后,我学会了一些我希望在这种情况下帮助别人的事情。 当您在段落内添加标记围绕Unicode文本并将其明确设置为字体时,它将起作用。

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

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

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.

更多推荐

本文发布于:2023-04-12 20:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/7c79af5d13d8ba5434096ae8c94c2ae7.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:汉字   Django   ReportLab   Characters   Chinese

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!