[reportlab-users] accentuated chars w/ TTF and textLabels

Tim Roberts timr at probo.com
Fri Mar 18 12:41:02 EST 2005


On Fri, 18 Mar 2005 15:49:05 +0100, Webmaster <webmaster at expertancy.com> 
wrote:

>I just compiled _renderPM  :-)  see previous messages
>now my objective is to use TT fonts in text labels, that would support 
>Stroke outline, and é à û ù ... (latin-1) char in them
>
>
>error E1 :
>  File 
>"/home/zope/zope/py24/lib/python2.4/site-packages/reportlab/graphics/renderPM.py", 
>line 170, in drawString
>    self._canvas.drawString(x,y,text)
>UnicodeDecodeError: 'utf8' codec can't decode bytes in position 6-8: 
>invalid data
>
>===================================
>CODE
>
># -*- coding: latin-1 -*-
>import os, sys
>  
>

That line tells the Python interpreter that any non-Unicode string 
constants in your code are to be interpreted as Latin-1.

>def MakeLabel():
>    D = Drawing(400, 200)
>    lab = Label()
>    lab.setOrigin(50,150)
>    lab.boxAnchor = 'nw'
>    lab.angle = 5
>    lab.dx = 0
>    lab.dy = -20
>    lab.fontName = 'Rina'
>    lab.fontSize = 46
>    lab.fillColor = colors.black
>    #lab.strokeColor = colors.yellow
>    #lab.strokeWidth = 1
>    lab.setText('Hello é ï î ö ô\n ç à ù û ü World')
>  
>

So, in this case, here, you have a Latin-1 string that you are sending 
to setText.  However, your Python library is apparently configured to 
assume utf8 as the default for non-Unicode strings, so it tries to 
interpret these Latin-1 characters as UTF8, which is illegal:

>ERROR TRACE
>
>  File 
>"/home/zope/zope/py24/lib/python2.4/site-packages/reportlab/graphics/renderPM.py", 
>line 170, in drawString
>    self._canvas.drawString(x,y,text)
>UnicodeDecodeError: 'utf8' codec can't decode bytes in position 6-8: 
>invalid data
>  
>

There is probably a way for you to reset the default codec to Latin-1, 
and perhaps someone else can chime in with it.  In the meantime, I 
believe you can solve this problem by converting the string to utf8:

    lab.setText('Hello é ï î ö ô\n ç à ù û ü World'.decode('latin-1').encode('utf8'))

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



More information about the reportlab-users mailing list