The issue was not the Paragraph
or the Frame
, but rather that I needed to set style.fontName = 'Arial'
instead of 'style.font`.
Russian String Formatting in Flowable
سؤال
There have been many posts about how to use reportlab
to create foreign text using drawString
or drawCentredString
, but I am struggling to wrap the text in a Paragraph
or Frame
.
The code below works to print the Russian text when called explicitly, but when using paragraph and frame it fails:
# -*- 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()
Any hints?
المحلول
لا تنتمي إلى StackOverflow