Problem with function [pyfpdf] add_font() on GAE.

88 views
Skip to first unread message

Brez Yl

unread,
Aug 2, 2013, 6:19:43 AM8/2/13
to web...@googlegroups.com
Hello!

I'm writing an app, which results with pdf file with some text with unicode characters. On local GAE it works good, but after deploy it can't import crash after add_font() (pyfpdf).

The code is:


# -*- coding: utf-8 -*-
def fun1():

   
from gluon.contrib.pyfpdf import FPDF, HTMLMixin
   
class MyFPDF(FPDF, HTMLMixin):
       
pass

    pdf
=MyFPDF()
    pdf
.add_font('DejaVu', '', 'DejaVuSansCondensed.ttf', uni=True)
    pdf
.add_page()
    pdf
.set_font('DejaVu','',16)
    pdf
.write(10,'testąśł')
   
    response
.headers['Content-Type']='application/pdf'
   
return pdf.output(dest='S')

The font files (with
a file DejaVuSansCondensed.pkl generated after first run on web2py server...) is in /gluon/contrib/fpdf/font. I didn't add anything to routers.py (I'm using Pattern-based system) also app.yaml is not changed.

As I said on local (both web2py and gae) it works well. After deploy only something like this works:
    pdf =MyFPDF()
    pdf
.add_page()
    pdf
.set_font('Arial','',16)
    pdf
.write(10,'testąśł')

But without "unusual" characters...

The best solution would be to add my font files (like DejaVu), but basically I need unicode characters in any font... maybe some "half-solution" to use "generic GAE unicode" fonts... if it exist something like this...

Christian Foster Howes

unread,
Aug 3, 2013, 4:24:28 AM8/3/13
to web...@googlegroups.com
can you post the stack trace to help us understand what went wrong?

Brez Yl

unread,
Aug 3, 2013, 7:40:10 AM8/3/13
to web...@googlegroups.com
In FILE: /base/data/home/apps/s~myapp/web2py-04.369240954601780983/applications/app3/controllers/default.py

Traceback (most recent call last):
 
File "/base/data/home/apps/s~myapp/web2py-04.369240954601780983/gluon/restricted.py", line 212, in restricted
   
exec ccode in environment
 
File "/base/data/home/apps/s~myapp/web2py-04.369240954601780983/applications/app3/controllers/default.py", line 674, in <module>
 
File "/base/data/home/apps/s~myapp/web2py-04.369240954601780983/gluon/globals.py", line 194, in <lambda>
   
self._caller = lambda f: f()
 
File "/base/data/home/apps/s~myapp/web2py-04.369240954601780983/applications/app3/controllers/default.py", line 493, in up_plik
    pdf
.add_font('DejaVu', '', 'DejaVuSansCondensed.ttf', uni=True)
 
File "/base/data/home/apps/s~myapp/web2py-04.369240954601780983/gluon/contrib/fpdf/fpdf.py", line 432, in add_font
    font_dict
= pickle.load(fh)
 
File "/base/data/home/runtimes/python27p/python27_dist/lib/python2.7/pickle.py", line 1378, in load
   
return Unpickler(file).load()
 
File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/pickle.py", line 858, in load
    dispatch
[key](self)
 
File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/pickle.py", line 966, in load_string
   
raise ValueError, "insecure string pickle"
ValueError: insecure string pickle

Christian Foster Howes

unread,
Aug 4, 2013, 12:42:28 PM8/4/13
to web...@googlegroups.com
after a quick moment of asking google i saw the suggestions that your error indicates something wrong with the pickled item....which i think in this case is the font file.  is your font file in the right format to be loaded by fpdf?  (i have no familiarity with this error or fpdf so i'm just throwing out suggestions).

good luck!

cfh

Brez Yl

unread,
Aug 4, 2013, 12:48:28 PM8/4/13
to
I think, the file is ok, for pdf/pyfpdf (as I said, it works well on devservers). But maybe it is, in some way wrong for GAE... I'm confused. But thanks for suggestion.

Christian Foster Howes

unread,
Aug 4, 2013, 12:55:19 PM8/4/13
to web...@googlegroups.com, Brez Yl
perhaps try printing the string fh at line 42 of
gluon/contrib/fpdf/fpdf.py ? maybe that will shed some light....

do you know if the path to the font you are adding in your controller is
correct for GAE? i don't remember off the top of my head the default
file path on GAE.

cfh

On 8/4/13 9:47 , Brez Yl wrote:
> I think, the file is ok, for pdf/pyfpdf (as I said, it works good on
> devservers). But maybe it is, in some way wrong for GAE... I'm confused.
> But thanks for suggestion.
>
>>>>> The font files (with a file *DejaVuSansCondensed.pkl* generated after

Brez Yl

unread,
Aug 4, 2013, 3:02:03 PM8/4/13
to web...@googlegroups.com, Brez Yl
I found some solution... it isn't the best one, but it works...

The problem is with using pickle on GAE. The best solution would be to overload/rewrite the add_font() function where for GAE, in such a way, that it would write to a datastore instead of a filesystem. Additionaly ValueError: insecure string pickle error can still occur, I tried b64 encoding according to this. But still I get errors. So my solution is to overload add_font() function with commented out/deleted parts:

if os.path.exists(unifilename):
    fh
= open(unifilename)
   
try:
        font_dict
= pickle.load(fh)
   
finally:
    fh
.close()
else:

and
try:
    fh
= open(unifilename, "w")
    pickle
.dump(font_dict, fh)
    fh
.close()
except IOError, e:
   
if not e.errno == errno.EACCES:
       
raise  # Not a permission error.

Because of this the function every time calculates little bit more instead of just reading data from the pickle... but it works on GAE.

Once again thanks for help Christian!


Reply all
Reply to author
Forward
0 new messages