我正在尝试从以下python编程生成pdf,但生成的输出不能正确显示希伯来字母
# -*- coding: utf-8 -*-
from reportlab.pdfgen import canvas
def hello(c):
c.drawString(100,100, "מה שלומך")
c = canvas.Canvas("hello.pdf")
hello(c)
c.showPage()
c.save()
答案 0 :(得分:5)
此代码(见下文)有效!您需要做的就是将ArialHB.ttf
(或任何其他支持希伯来字符的字体)放入site-packages/reportlab/fonts
...
所需的输出将位于pdf页面的底部。
# -*- coding: utf-8 -*-
from reportlab.pdfgen import canvas
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
pdfmetrics.registerFont(TTFont('Hebrew', 'ArialHB.ttf'))
def hello(c):
c.setFont("Hebrew", 14)
c.drawString(10,10, u"מה שלומך".encode('utf-8'))
c = canvas.Canvas("hello.pdf")
hello(c)
c.showPage()
c.save()
答案 1 :(得分:2)
如果使用正确的解码调用,例如"מה שלומך".decode("utf-8")
,则可以正常工作。
# -*- coding: utf-8 -*-
from reportlab.pdfgen import canvas
def hello(c):
c.drawString(100,100, "מה שלומך".decode("utf-8"))
c = canvas.Canvas("hello.pdf")
hello(c)
c.showPage()
c.save()