remcoboerma / pyfpdf

Automatically exported from code.google.com/p/pyfpdf
GNU Lesser General Public License v3.0
0 stars 0 forks source link

write_html( unicode ) does not use TTF #54

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?

1. try http://code.google.com/p/pyfpdf/wiki/Unicode , 
> pdf.write(8, u"Ąžuolas")   # works fine 

2. pdf.write_html( u'Ąžuolas' ) #  gives error

 File "/home/jurgis/web2py/applications/apskaitele/controllers/default.py", line 59, in pdftest
    pdf.write_html( u'Ąžuolas' )
  File "/home/jurgis/web2py/gluon/contrib/fpdf/html.py", line 397, in write_html
    h2p.feed(text)
  File "/usr/local/lib/python2.7/HTMLParser.py", line 114, in feed
    self.goahead(0)
  File "/usr/local/lib/python2.7/HTMLParser.py", line 152, in goahead
    if i < j: self.handle_data(rawdata[i:j])
  File "/home/jurgis/web2py/gluon/contrib/fpdf/html.py", line 122, in handle_data
    self.pdf.write(self.h,txt)
  File "/home/jurgis/web2py/gluon/contrib/fpdf/fpdf.py", line 822, in write
    txt = self.normalize_text(txt)
  File "/home/jurgis/web2py/gluon/contrib/fpdf/fpdf.py", line 1012, in normalize_text
    txt = txt.encode('latin1')
UnicodeEncodeError: 'latin-1' codec can't encode characters in position 0-1: 
ordinal not in range(256)

3.

What is the expected output? What do you see instead?

--> Ąžuolas

What version of the product are you using? On what operating system?

__version__ = "1.7"

Please provide any additional information below.

pdf.write_html( u'Ąžuolas'.encode('utf8') )  # produces  "Ąžuolas"  

https://groups.google.com/d/msg/web2py/KJDeQoLKw-M/PY7cmfd8XkYJ

Original issue reported on code.google.com by jurgis.pralgauskis on 26 Apr 2013 at 8:23

GoogleCodeExporter commented 9 years ago
I've made a change to allow them, please update html.py:

https://pyfpdf.googlecode.com/hg/fpdf/html.py

Then, you need to load a ttf unicode font, and then pass it in <font> face 
attribute:

pdf=MyFPDF()
# add utf8 font
pdf.add_font('DejaVu', '', 'DejaVuSansCondensed.ttf', uni=True)
# first page:
pdf.add_page()
pdf.write_html(u"<font face='DejaVu'>Ąžuolas</font>")

For more info and complete code, see:

https://code.google.com/p/pyfpdf/wiki/Web2Py

Let me know if that works so I can update the docs and web2py contrib version

Original comment by reingart@gmail.com on 27 Apr 2013 at 3:24

GoogleCodeExporter commented 9 years ago
one more weird thing - combining html_write(..) and after it write(..) - gives 
error

    pdf.add_font('DejaVu', '', 'DejaVuSansCondensed.ttf', uni=True)
    pdf.set_font('DejaVu', '', 14)

    pdf.write_html(u"<font face='DejaVu'>Ąžuolas</font>")    
    pdf.write(8, u'Ąžuolas')  # causes error

***

    pdf.write(8, u'Ąžuolas')
  File "/home/nijole/Downloads/pyfpdf/fpdf/fpdf.py", line 837, in write
    txt = self.normalize_text(txt)
  File "/home/nijole/Downloads/pyfpdf/fpdf/fpdf.py", line 1045, in normalize_text
    txt = txt.encode('latin1')
UnicodeEncodeError: 'latin-1' codec can't encode characters in position 0-1: 
ordinal not in range(256)

***
but write(..) before write_html(..) doesn't give it :)

    pdf.write(8, u'Ąžuolas')  
    pdf.write_html(u"<font face='DejaVu'>Ąžuolas</font>")   # no problem :)

Original comment by jurgis.pralgauskis on 28 Apr 2013 at 7:56

GoogleCodeExporter commented 9 years ago
I could workaround by repeatidly setting font for write

    pdf.set_font('DejaVu', '', 14)
    pdf.write_html(u"<font face='DejaVu'>Ąžuolas</font>")    

    pdf.set_font('DejaVu', '', 14)
    pdf.write(8, u'Ąžuolas')

Original comment by jurgis.pralgauskis on 28 Apr 2013 at 7:58

GoogleCodeExporter commented 9 years ago
a, I get -- I don't need to pdf.set_font(..) for write_html

as I indicate it in <font ...> tag :)

Original comment by jurgis.pralgauskis on 28 Apr 2013 at 8:09

GoogleCodeExporter commented 9 years ago
by the way, I can give string without u prepended, and works ok (encodings are 
mystic :) 

pdf.write_html("<font face='DejaVu' size='40'>.. Ąžuolas</font>")  

Original comment by jurgis.pralgauskis on 28 Apr 2013 at 8:17

GoogleCodeExporter commented 9 years ago
Thanks for reporting and testing the solution!

Original comment by reingart@gmail.com on 5 Feb 2014 at 1:47