[reportlab-users] registerFont stays how long in memory? what s the best place to register a font?

Thomas Kremmel thomasspin at gmail.com
Wed Apr 14 18:41:22 EDT 2010


Hi,

I changed my code to loading the font based on if the _fonts variable has
the key Calibri. If not I load the font.

try:
if not pdfmetrics._fonts.has_key('Calibri'):
pdfmetrics.registerFont(TTFont('Calibri', settings.FONT_DIR +
'/fonts/Calibri.ttf'))
pdfmetrics.registerFont(TTFont('Calibri-Bold', settings.FONT_DIR
+ '/fonts/Calibri-bold.ttf'))
except:
request.user.message_set.create(message='Font could not be read from
file system. Please try again or contact your system administrator.')
return 0

try:
p.setFont("Calibri", 7)
except:
request.user.message_set.create(message='Font could not be loaded.
Please try again or contact your system administrator.')
return 0

This worked fine in my dev. settings and it worked fine in production now
for a day. But now I'm back to my old problem that the call
p.setFont("Calibri", 7) throws a keyError at about each 5th pdf generation
call, which is more than strange. The production server is an apache server
with modpython running.

I really have no clue what is wrong and I'm wondering whether this font
loading is maybe buggy?

Would appreciate any help!
Tom


2010/4/13 Robin Becker <robin at reportlab.com>


> On 13/04/2010 11:13, Thomas Kremmel wrote:

>

>> Hi,

>>

>> I'm wondering what is the best time to register a font for use in

>> reportlab.

>> I added the following line into my settings.py in django:

>>

>> pdfmetrics.registerFont(TTFont('Calibri', FONT_DIR +

>> '/fonts/Calibri.ttf'))

>>

>> the settings.py is called the first time when the server starts.

>>

>> After this call the font is available for pdf generation. But it occurs

>> that

>> after a few days the font is not available anymore and I have to restart

>> the

>> django app, which leads to a re-registering of the font.

>>

>> One approach I already tried was registering the font before each pdf

>> generation call, which did not worked since it crashed each 3-5 call.

>> Thats

>> the reason why I put it in a place which is just called once at the

>> beginning.

>>

>> I just want to know if the settings.py is the correct place to register a

>> font for reportlab and if yes, what could be the reason that the font is

>> not

>> available after a few days anymore!?

>>

>>

>> would be great to find some help here!

>>

>> thomas

>>

> ........

>

>

> well I wouldn't use the django settings.py file to do this; it's supposed

> to be executed just once, but your observation shows that there can be

> problems.

>

> How about checking at the startup to see if the font is already present

>

> if not pdfmetrics._fonts.has_key('Calibri'):

>

> pdfmetrics.registerFont(TTFont('Calibri', FONT_DIR +

> '/fonts/Calibri.ttf'))

>

>

> --

> Robin Becker

>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://two.pairlist.net/pipermail/reportlab-users/attachments/20100415/1f7a6298/attachment.htm>


More information about the reportlab-users mailing list