[reportlab-users] TrueType font Embedding : Learn from my mistake

Ian Sparks reportlab-users@reportlab.com
Thu, 29 May 2003 14:25:19 -0400


I unknowingly passed TTFont a unicode string for the font name (it came =
from an XML document)

Don't do this!

When the font gets registered it carries along the font-name as a =
comment in the PDF output, turning the PDF source for that element to =
unicode as a consequence.=20

Then PDFFile.format chokes on string.join(self.strings,"") with the =
oh-so-helpful ASCII Decoding error: Ordinal not in range(128).

I don't know if the cure is "Don't be so stupid" or if it would be wise =
to put some sanity checking into TTFont.__init__ ?

Here's my "test case" :

from reportlab.pdfgen import canvas
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont

canv =3D canvas.Canvas('test.pdf')

fontname =3D u'MS Sans Serif'   #Unicode ARGHHH!

pdfmetrics.registerFont(TTFont(fontname,'C:\WINNT\FONTS\MICROSS.TTF'))

canv.setFont(fontname,11)
canv.drawString(10,150,'hello')

canv.save()  #Boom! =20