[reportlab-users] TrueType and German Umlaut

Dirk Holtwick reportlab-users@reportlab.com
Wed, 12 Feb 2003 11:08:08 +0100


Dinu Gherman wrote:
> In any case, I used to have some success in the past with a
> function suggested by Marius:
> 
>   def latin1toUTF8(s):
>     return unicode(s, "iso-8859-1").encode("utf-8")
> 
> to convert text to Unicode before passing it to Platypus, but
> I can't quickly reproduce this using the Rina font and umlauts...
> Maybe you can...

yes, works fine! thanks Dinu for your help.

here follows the patch for everyone having similar problems:

from reportlab.pdfgen.canvas import Canvas
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib.units import inch
from reportlab.platypus import Paragraph, Frame
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.lib.fonts import addMapping

def latin1toUTF8(s):
     return unicode(s, "iso-8859-1").encode("utf-8")

ttf = "test.ttf"
name = "milano"
pdfmetrics.registerFont(TTFont(name, ttf))
addMapping(name, 0, 0, name) #normal
addMapping(name, 0, 1, name) #italic
addMapping(name, 1, 0, name) #bold
addMapping(name, 1, 1, name) #italic and bol

styles = getSampleStyleSheet()
styleN = styles['Normal']
story = []
story.append(Paragraph(latin1toUTF8("""
     <font face="milano">äöü Test FONT Milano</font>
     """), styleN))
c = Canvas('mydoc.pdf')
f = Frame(inch, inch, 6*inch, 9*inch, showBoundary=1)
f.addFromList(story,c)
c.save()