FPDF <ul> issue

692 views
Skip to first unread message

step

unread,
Oct 28, 2013, 2:40:09 PM10/28/13
to web...@googlegroups.com
I discovered and reported in http://code.google.com/p/pyfpdf/issues/detail?id=66 an issue with gluon/contrib/FPFD. Basically, rendering HTML <UL> tags as PDF may trigger a utf8 codec error because fpdf/html.py sets the bullet character as '\x95'. As a work-around I have replaced '\x95' with '*'.

Richard Vézina

unread,
Oct 28, 2013, 4:46:10 PM10/28/13
to web2py-users
did you user your_string.decode('utf8') before passing to FPDF??

I think you have mistake in your code, you should provide example code in you issue report!!

Richard


On Mon, Oct 28, 2013 at 2:40 PM, step <step...@gmail.com> wrote:
I discovered and reported in http://code.google.com/p/pyfpdf/issues/detail?id=66 an issue with gluon/contrib/FPFD. Basically, rendering HTML <UL> tags as PDF may trigger a utf8 codec error because fpdf/html.py sets the bullet character as '\x95'. As a work-around I have replaced '\x95' with '*'.

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
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.

step

unread,
Oct 29, 2013, 1:41:20 PM10/29/13
to web...@googlegroups.com
Fair enough, Richard, I'm providing sample code here, and I will link to it in the issue report.
Comments in the sample code explain the 0x95 codec error and also ask some questions.
Thanks for your help.

Download TTF font DejaVuSans.ttf and copy it into gluon/contrib/fpdf/
You can download it from the FPDF site http://code.google.com/p/pyfpdf/downloads/detail?name=fpdf_unicode_font_pack.zip&can=2&q=

Add this function to controllers/default.py

def issue(): return dict()


Replace views/generic.pdf
{{"""
replace the standard views/generic.pdf file with this file
"""

import os
# we comment this as we are using a custom model function
# from gluon.contrib.generics import pdf_from_html
filename
= '%s/%s.html' % (request.controller,request.function)
if os.path.exists(os.path.join(request.folder,'views',filename)):
    html
=response.render(filename)
else:
    html
=BODY(BEAUTIFY(response._vars)).xml()
pass
# note the pyfpdf instead of pdf in the function call
# pyfpdf is defined in views/issue.html
=pyfpdf_from_html(html)
}}

Create views/default/issue.html
{{"""
Put this file in [app]/default/issue.html then browse .../default/issue.pdf.
You should see a PDF file with two bullets, both with garbage characters, one for an accented a, the other one for the euro sign.
Now comment out the line after #1# below and uncomment the similar line after #2#.
The aim is to print out the two characters correctly. Browse again the pdf page to test...
Instead you will get a codec error involving character 0x95.
0x95 is the bullet sign that FPDF adds for UL() below (counter-proof: replace OL() for UL() to get going)
This reproduces issue #66 that I reported on the FPDF google code site http://code.google.com/p/pyfpdf/issues/detail?id=66.

I'm still left scratching my head on how to print the euro character and the accented a character at the same time.
I tried playing with codecs in #3# below to no avail.
"""

def text(a):
   
print repr(a)
   
return a.decode("utf-8").encode("utf-8")
   
#3# setting encode 'latin-1' prints accented characters (but errors out on euro, which needs utf-8 encoding)

def pyfpdf_from_html(html):
   
import os
   
from gluon.contrib.fpdf import FPDF, HTMLMixin
   
   
class MyFPDF(FPDF, HTMLMixin):
       
pass
   
    pdf
= MyFPDF()
    pdf
.add_font('DejaVu', '', 'DejaVuSans.ttf', uni=True)
    pdf
.set_font('DejaVu', '', 12)
    pdf
.add_page()
   
#1#this works but the default font doesn't include the euro character
    pdf
.write_html('%s' % html)
   
#2#this errors out with 'utf8' codec can't decode byte 0x95 in position 5: invalid start byte
   
###note that 0x95 is the bullet sign that FPDF adds for UL() below - replace it with OL() to get going
   
#pdf.write_html('<font face="DejaVu" size="12">%s<font>' % html)
   
return XML(pdf.output(dest='S'))
}}

{{=HTML(BODY(UL([text('à'),text('€')])))}}

Mariano Reingart

unread,
Oct 29, 2013, 2:30:58 PM10/29/13
to web...@googlegroups.com
You can try to download the fpdf source code from the repository (see
the download zip link) and update your web2py distribution:

http://code.google.com/p/pyfpdf/source/browse/

Please let follow this on the fpdf site:

http://code.google.com/p/pyfpdf/issues/detail?id=66

Thanks for reporting it!

Mariano Reingart
http://www.sistemasagiles.com.ar
http://reingart.blogspot.com

Massimo Di Pierro

unread,
Oct 29, 2013, 3:06:23 PM10/29/13
to web...@googlegroups.com
Should I do that too?

step

unread,
Oct 29, 2013, 3:10:56 PM10/29/13
to web...@googlegroups.com
The latest fpdf code commit is dated August 15. I checked its source code against the one bundled with web2py 2.6.4-stable and they appear to be identical.
Reply all
Reply to author
Forward
0 new messages