有很多关于如何使用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()
任何提示?
答案 0 :(得分:0)
问题不在于Paragraph
或Frame
,而在于我需要设置style.fontName = 'Arial'
而不是' style.font`。