Flowable中的俄语字符串格式化
关于如何使用 reportlab
来创建外文文本,很多人都发过帖子,特别是用 drawString
或 drawCentredString
方法。不过,我在把文本放进 Paragraph
或 Frame
时遇到了困难。
下面的代码在明确调用时可以打印出俄文文本,但当我使用段落和框架时就不行了:
# -*- coding: utf-8 -*-
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
from reportlab.lib.units import inch
from reportlab.platypus import Paragraph, Frame
from reportlab.lib.styles import getSampleStyleSheet
#Set the Arial font
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
pdfmetrics.registerFont(TTFont('Arial', 'Arial.ttf'))
#Set Dimensions of the sheet
width, height = letter
margin = 0.5*inch
mwidth = width - 2*margin
mheight = height - 2*margin
#Set the styles to Centered and Arial
styleSheet = getSampleStyleSheet()
style = styleSheet['Normal']
style.alignment = 1
style.font = "Arial"
style.fontSize = 14
def Fill_Canvas(c):
#Create Border
c.translate(margin,margin)
c.setLineWidth(4)
c.rect(0,0, mwidth, mheight)
#Create Line
c.translate(0,mheight-inch)
c.line(0.5*inch ,0,mwidth - 0.5*inch,0)
#Create Russia Text
c.setFont("Arial",36)
c.translate(0,-inch)
c.drawCentredString(mwidth/2,0,u"ТЕХНИЧЕСКИЙ ПАСПОРТ".encode('utf-8'))
c.translate(0,-.75*inch)
c.drawCentredString(mwidth/2,0,u"HA".encode('utf-8'))
#Create description
c.translate(0, -.75*inch)
f = Frame(0,0, mwidth, 0.5*inch,showBoundary=1)
story = []
story.append(Paragraph(u"Sample russian text: ТЕХНИЧЕСКИЙ ПАСПОРТ".encode('utf-8'),style))
f.addFromList(story,c)
#Create PDF
c = canvas.Canvas("Hello.pdf",pagesize = letter)
Fill_Canvas(c)
c.showPage()
c.save()
有没有什么提示呢?
1 个回答
0
问题不在于Paragraph
或Frame
,而是我需要把style.fontName = 'Arial'
设置成'Arial',而不是' style.font'。