[reportlab-users] Re: Using UTF-8 strings with ReportLab.

Jorge Godoy godoy at ieee.org
Tue Dec 20 09:45:11 EST 2005


Jorge Godoy <godoy at ieee.org> writes:

> Hi!
> 
> 
> What is the best way to use UTF-8 strings with ReportLab?  I was replicating
> the first example on the user guide and just for fun translated it to pt_BR
> (after all, this is the language all my data is in...) and the output isn't
> right.
> 
> ================================================================================
> #!/usr/bin/python
> # -*- coding: utf-8 -*-
> 
> from reportlab.pdfgen import canvas
> 
> def hello(c):
>     c.drawString(100,100,'Olá mundo!')
> 
> c = canvas.Canvas('ola.pdf')
> hello(c)
> c.showPage()
> c.save()
> ================================================================================

Traceback for

        *  " 'Olá mundo!'.decode('utf-8')":


Traceback (most recent call last):
  File "relatorio.platypus", line 10, in ?
    hello(c)
  File "relatorio.platypus", line 7, in hello
    c.drawString(100,100, 'Olá mundo!'.decode('utf-8'))
  File "/usr/lib/python2.4/site-packages/reportlab/pdfgen/canvas.py", line 1169, in drawString
    t.textLine(text)
  File "/usr/lib/python2.4/site-packages/reportlab/pdfgen/textobject.py", line 334, in textLine
    self._code.append('%s T*' % self._formatText(text))
  File "/usr/lib/python2.4/site-packages/reportlab/pdfgen/textobject.py", line 306, in _formatText
    text = self._canvas._escape(text)
  File "/usr/lib/python2.4/site-packages/reportlab/pdfgen/canvas.py", line 231, in _escape
    return _escapePDF(s)
  File "/usr/lib/python2.4/site-packages/reportlab/pdfbase/pdfutils.py", line 139, in _escape
    return join(map(lambda c, d=_ESCAPEDICT: d[c],s),'')
  File "/usr/lib/python2.4/site-packages/reportlab/pdfbase/pdfutils.py", line 139, in <lambda>
    return join(map(lambda c, d=_ESCAPEDICT: d[c],s),'')
KeyError: u'\xe1'


        * " 'Olá mundo!'.encode('utf-8')"

Traceback (most recent call last):
  File "relatorio.platypus", line 10, in ?
    hello(c)
  File "relatorio.platypus", line 7, in hello
    c.drawString(100,100, 'Olá mundo!'.encode('utf-8'))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 2: ordinal not in range(128)


        * " u'Olá mundo!'"

Traceback (most recent call last):
  File "relatorio.platypus", line 10, in ?
    hello(c)
  File "relatorio.platypus", line 7, in hello
    c.drawString(100,100, u'Olá mundo!')
  File "/usr/lib/python2.4/site-packages/reportlab/pdfgen/canvas.py", line 1169, in drawString
    t.textLine(text)
  File "/usr/lib/python2.4/site-packages/reportlab/pdfgen/textobject.py", line 334, in textLine
    self._code.append('%s T*' % self._formatText(text))
  File "/usr/lib/python2.4/site-packages/reportlab/pdfgen/textobject.py", line 306, in _formatText
    text = self._canvas._escape(text)
  File "/usr/lib/python2.4/site-packages/reportlab/pdfgen/canvas.py", line 231, in _escape
    return _escapePDF(s)
  File "/usr/lib/python2.4/site-packages/reportlab/pdfbase/pdfutils.py", line 139, in _escape
    return join(map(lambda c, d=_ESCAPEDICT: d[c],s),'')
  File "/usr/lib/python2.4/site-packages/reportlab/pdfbase/pdfutils.py", line 139, in <lambda>
    return join(map(lambda c, d=_ESCAPEDICT: d[c],s),'')
KeyError: u'\xe1'


Again, ISO-8859-1 string work.


Be seeing you,
-- 
Jorge Godoy      <godoy at ieee.org>



More information about the reportlab-users mailing list