pyfPDF templates and unicode element values?

237 views
Skip to first unread message

Jurgis Pralgauskis

unread,
Apr 26, 2013, 11:37:13 PM4/26/13
to web...@googlegroups.com
Hi, 

I tried to combine ideas from http://code.google.com/p/pyfpdf/wiki/Templates

but not much luck... encodings were always hard for me...

I can insert unicode via direct "write" ok, 

but if I try to insert it via cell/multicell, I get stuck ...

I changed file encoding of template.py
and also replaced  
        if isinstance(text, unicode):
            #~ text = text.encode("latin1","ignore")
            text = text.encode('utf8')

and explicitly changed all set_font to:
            #~ pdf.set_font(font,style,size)
            pdf.add_font('DejaVu', '', 'DejaVuSansCondensed.ttf', uni=True)
            pdf.set_font('DejaVu','',size)

but then I get:

Traceback (most recent call last):
  File "unicode_template.py", line 321, in <module>
    f.render("./invoice.pdf")
  File "unicode_template.py", line 135, in render
    self.handlers[element['type'].upper()](pdf, **element)
  File "unicode_template.py", line 172, in text
    pdf.cell(w=x2-x1,h=y2-y1,txt=text,border=0,ln=0,align=align)
  File "/home/nijole/Downloads/pyfpdf/fpdf/fpdf.py", line 632, in cell
    txt = self.normalize_text(txt)
  File "/home/nijole/Downloads/pyfpdf/fpdf/fpdf.py", line 1043, in normalize_text
    txt = txt.decode('utf8')
  File "/usr/lib/python2.7/encodings/utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xba in position 1: invalid start byte

Mariano Reingart

unread,
Apr 27, 2013, 2:12:17 PM4/27/13
to web...@googlegroups.com
Please, can you attach a minimal script to reproduce this?

You should pass unicodes when using utf8, do not pass strings with
.encode('utf8')
(using utf8 fonts you can avoid the if isinstance(text, unicode):
block completely)

Yes unicode python support is hard to master, luckily in py3k it is a bit easier

Best regards,

Mariano Reingart
http://www.sistemasagiles.com.ar
http://reingart.blogspot.com
> --
>
> ---
> You received this message because you are subscribed to the Google Groups
> "web2py-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to web2py+un...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Jurgis Pralgauskis

unread,
Apr 28, 2013, 6:38:52 AM4/28/13
to web...@googlegroups.com
I used template.py invoice example, 
but in invoice.csv I tried to change all 'Arial' --> 'DejaVu'

f.parse_csv(infile="_invoice.csv", delimiter=";", decimal_sep=",")
f.pdf.add_font('DejaVu', '', 'DejaVuSansCondensed.ttf', uni=True)
f.pdf.add_font('DejaVu', 'B', 'DejaVuSans-Bold.ttf', uni=True)


and the problem was caused by '\xba'
'document_number';'T';115.30;27.80;125.30;33.30;'DejaVu';14.00;1;0;0;0;0;'I';'N\xba: ';2

***

another issue is in class Template 
    def __setitem__(self, name, value):
        ...
            if isinstance(value,unicode):
                value = value.encode("latin1","ignore")   

so   u"Sample ĄČĘĖŠŲ product"   will show only    "Sample  product ' 
but  "Sample ĄČĘĖŠŲ product"   will show   "Sample ĄČĘĖŠŲ  product '  ... :)


***
ps.: in template.py there was also 
            if font == 'arial black':
                font = 'arial'

which could be generalized (for DejaVu or other) 
        if font.lower().endswith(" black"):
            font=font[:-len(" black")].strip()





You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/iAkD3YFuDWg/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.





--
Jurgis Pralgauskis
tel: 8-616 77613;
Don't worry, be happy and make things better ;)
http://galvosukykla.lt
Reply all
Reply to author
Forward
0 new messages