[reportlab-users] Character Encoding

Tim Roberts timr at probo.com
Fri Apr 22 12:53:51 EDT 2005

On Fri, 22 Apr 2005 13:09:51 +0200, Uli Schrempp <schrempp at syslab.com> 

>I'm trying to write an ISO 8859-7 (greek letters) string into a PDF like 
>	text = unicode(greek_letters, 'utf-8').encode('iso8859-7')
>	myCanvas.setFont('helvetica', 16)
>	myCanvas.drawCentred String( x, y, text)
>But I get no greek letters written ...
>What have I to improove?

Two problems.  First, I suspect you have an encoding problem here.  What 
that code says is that "greek_letters" is a string in UTF-8, which you 
then convert to ISO8859-7 and send to ReportLab.  However, ReportLab 
doesn't understand ISO8859-7 -- it understands UTF-8.  What IS the 
variable "greek letters"?  Is it actually an ISO8859-7 string?  If so, 
you want this:

    text = greek_letters.decode('iso8859-7').encode('utf-8')

Convert the ISO8859-7 to Unicode, then convert the Unicode to UTF-8.

Second, I'm not sure the Poscscript fonts (like Helvetica) understand 
UTF-8 at all, and I don't think they have any Greek characters.  UTF-8 
certainly works with TrueType fonts.  I just tried this, and it worked:

    pdfmetric.registerFont( TTFont( 'Arial1', 'arial.ttf') )
    u = u"\N{greek capital letter delta}\N{greek capital letter gamma}"
    u1 = u.encode('utf-8')
    c.setFont( 'Arial1', 36 )
    c.drawString( 80, 600, u1 )

Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.

More information about the reportlab-users mailing list