Problems with fpdf

394 views
Skip to first unread message

Martin Weissenboeck

unread,
Nov 28, 2015, 12:46:12 PM11/28/15
to web2py-users, Mariano Reingart
Hi, I have tried again to use fpdf.

1st try:

from gluon.contrib.fpdf import FPDF

def test():
    pdf = FPDF('P', 'mm', 'A4')
    pdf.add_page()
    pdf.set_font('Arial', 'B', 16)
    pdf.cell(40,10,'Hello World')    
    response.headers['Content-Type'] = 'application/pdf'    
    return XML(pdf.output('', 'S'))

Ok, that's simple and it works fine.

2nd try:

Now I want to use the unicode version.

Before you can use UTF-8, you have to install at least one Unicode font in the font directory (or system font folder).

But there is no font directory in fpdf!

It would be nice to have a complete example how to use fpdf in web2py!

After some trials I had to copy the font directory containing a lot of "DejaVu*"-files to the gluon/contrib/fpdf directory. 

The structure is: gluon/contrib/fpdf/font


from gluon.contrib.fpdf import FPDF

def test():
    pdf = FPDF('P', 'mm', 'A4')
    pdf.add_page()
    pdf.add_font('DejaVu', '', 'DejaVuSansCondensed.ttf', uni=True)
    pdf.set_font('DejaVu', '', 14)
    pdf.cell(40,10,'Hello World  äöü€')
    
    response.headers['Content-Type'] = 'application/pdf'    
    return XML(pdf.output('', 'S'))

​And now I get an error message:

<type 'exceptions.ValueError'> could not convert string to float

Version

web2py™Version 2.12.3-stable+timestamp.2015.08.19.00.18.03
PythonPython 2.7.6: /usr/local/bin/python (prefix: /usr)

Traceback

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
Traceback (most recent call last):
File "/home/www-data/web2py/gluon/restricted.py", line 227, in restricted
exec ccode in environment
File "/home/www-data/web2py/applications/m_abschluss/controllers/test.py", line 54, in <module>
File "/home/www-data/web2py/gluon/globals.py", line 412, in <lambda>
self._caller = lambda f: f()
File "/home/www-data/web2py/applications/m_abschluss/controllers/test.py", line 26, in test
pdf.add_font('DejaVu', '', 'DejaVuSansCondensed.ttf', uni=True)
File "/home/www-data/web2py/gluon/contrib/fpdf/fpdf.py", line 432, in add_font
font_dict = pickle.load(fh)
ValueError: could not convert string to float

​What is the right way to use fpdf with the whole unicode character set  in web2py?​

​Regards, Martin​

Edwood

unread,
Nov 28, 2015, 10:55:05 PM11/28/15
to web2py-users, rein...@gmail.com, mwei...@gmail.com
I am not having that problem when testing your code with pyfpdf 1.7.2, using the same font.  I noticed web2py 2.12.3 is still using pyfpdf 1.7.1.  I recommend temporarily replacing pyfpdf folder with new version an try again.

Martin Weissenboeck

unread,
Nov 29, 2015, 3:41:01 AM11/29/15
to Edwood, web2py-users, Mariano Reingart
Thank you. I have tried version 1.7.2 - same result.

But it is solved: the .pkl-file is wrong.

I have taken a new set of dejavo*.ttf files from
Then I have unziped and copied these files into contrib/fpdf/font

fpdf creates new .pkl after the first usage.

Don't forget to make www-data the owner of the font directory:
chown -R www-data:www-data font

Maybe a short description of these steps in the web2py book could help others to use fpdf.

Regards, Martin



--
Mit freundlichen Grüßen / With kind regards
Martin Weissenböck
Gregor-Mendel-Str. 37, 1190 Wien
Austria / European Union

Reply all
Reply to author
Forward
0 new messages