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

Thomas Kremmel thomasspin at gmail.com
Mon Apr 19 13:59:15 EDT 2010


We changed back to mod_python and it worked for two days and now I do occur
another error which is:



File "/usr/lib/python2.6/dist-packages/reportlab/lib/utils.py", line 452,
in open_for_read

raise IOError('Cannot open resource "%s"' % name)



IOError: Cannot open resource
"/var/www/mydoamint/libs/reportlab/fonts/DarkGardenMK.afm"

Which is pretty strange since this error is new, the file is available and I
did not changed anything in the meantime. File is world-readable and I have
no clue why it could not be read from one minute to the other.
I ' m thinking of that the calibri font that I'm using is maybe damaged. Is
this a possible source of problems? I do not know anything about fonts but
it seems to me as if this could be the problem..
Still it is strange that it sometimes work and sometimes it does not..er e

2010/4/15 Thomas Kremmel <thomasspin at gmail.com>


> Ok I'm back to strange behavior ;-)

>

> This time just after one hour the old behavior occurs. The behavior works

> like this:

>

> p.setFont('Calibri', 7) is throwing an keyError although I check

> if pdfmetrics._fonts.has_key('Calibri') and if not I register the font.

> Registering the font always works out.

> Interestingly it obviously crashes in these both occasions..

>

> 1. _fonts.has_key('Calibri') is True -> font was already registered before

> 2. _fonts.has_key('Calibri') is False -> font will be registered exactly

> for this request

>

> And more interestingly.. it just crashes each 3-5 call...which confuses me

> much more!

> Furthermore I would expect that after the first registerFont call the

> subsequent requests should not have to re-register the font. But obviously

> it behaves that it sometimes reregister the font and sometimes it does not.

>

>

> ##after restart of the server everything works fine.

>

> [Thu Apr 15 00:25:53 2010] [error] [client 127.0.0.1]

> pdfmetrics._fonts.has_key('Calibri') is True

> [Thu Apr 15 00:25:53 2010] [error] [client 127.0.0.1] view:

> export_objects_as_pdf: info: p.setFont('Calibri', 7) was successfull

> [Thu Apr 15 00:25:54 2010] [error] [client 127.0.0.1]

> pdfmetrics._fonts.has_key('Calibri') is True

> [Thu Apr 15 00:25:54 2010] [error] [client 127.0.0.1] view:

> export_objects_as_pdf: info: p.setFont('Calibri', 7) was successfull

> [Thu Apr 15 00:25:58 2010] [error] [client 127.0.0.1]

> pdfmetrics._fonts.has_key('Calibri') is True

> [Thu Apr 15 00:25:58 2010] [error] [client 127.0.0.1] view:

> export_objects_as_pdf: info: p.setFont('Calibri', 7) was successfull

> [Thu Apr 15 00:25:59 2010] [error] [client 127.0.0.1]

> pdfmetrics._fonts.has_key('Calibri') is True

> [Thu Apr 15 00:25:59 2010] [error] [client 127.0.0.1] view:

> export_objects_as_pdf: info: p.setFont('Calibri', 7) was successfull

> [Thu Apr 15 00:25:59 2010] [error] [client 127.0.0.1]

> pdfmetrics._fonts.has_key('Calibri') is True

> [Thu Apr 15 00:25:59 2010] [error] [client 127.0.0.1] view:

> export_objects_as_pdf: info: p.setFont('Calibri', 7) was successfull

> [Thu Apr 15 00:25:59 2010] [error] [client 127.0.0.1]

> pdfmetrics._fonts.has_key('Calibri') is True

> [Thu Apr 15 00:25:59 2010] [error] [client 127.0.0.1] view:

> export_objects_as_pdf: info: p.setFont('Calibri', 7) was successfull

> [Thu Apr 15 00:26:04 2010] [error] [client 127.0.0.1]

> pdfmetrics._fonts.has_key('Calibri') is True

> [Thu Apr 15 00:26:04 2010] [error] [client 127.0.0.1] view:

> export_objects_as_pdf: info: p.setFont('Calibri', 7) was successfull

> [Thu Apr 15 00:26:05 2010] [error] [client 127.0.0.1]

> pdfmetrics._fonts.has_key('Calibri') is True

> [Thu Apr 15 00:26:05 2010] [error] [client 127.0.0.1] view:

> export_objects_as_pdf: info: p.setFont('Calibri', 7) was successfull

> [Thu Apr 15 00:26:07 2010] [error] [client 127.0.0.1]

> pdfmetrics._fonts.has_key('Calibri') is True

> [Thu Apr 15 00:26:07 2010] [error] [client 127.0.0.1] view:

> export_objects_as_pdf: info: p.setFont('Calibri', 7) was successfull

>

> [Thu Apr 15 00:26:08 2010] [error] [client 127.0.0.1]

> pdfmetrics._fonts.has_key('Calibri') is True,

> [Thu Apr 15 00:26:08 2010] [error] [client 127.0.0.1] view:

> export_objects_as_pdf: info: p.setFont('Calibri', 7) was successfull,

>

> ##after one hour break I tried it again and started directly with an error.

> following with two successfull requests and a final crashing request

>

> [Thu Apr 15 01:47:39 2010] [error] [client 127.0.0.1] view:

> export_objects_as_pdf: info: pdfmetrics._fonts.has_key('Calibri') is False -

> Successfully registered Calibri and Calibri-Bold Font,

> [Thu Apr 15 01:47:39 2010] [error] [client 127.0.0.1] view:

> export_objects_as_pdf: info: error in line p.setFont('Calibri', 7),

> [Thu Apr 15 01:47:39 2010] [error] [client 127.0.0.1] 'Calibri',

> [Thu Apr 15 01:47:46 2010] [error] [client 127.0.0.1]

> pdfmetrics._fonts.has_key('Calibri') is True,

> [Thu Apr 15 01:47:46 2010] [error] [client 127.0.0.1] view:

> export_objects_as_pdf: info: p.setFont('Calibri', 7) was successfull,

> [Thu Apr 15 01:47:47 2010] [error] [client 127.0.0.1]

> pdfmetrics._fonts.has_key('Calibri') is True,

> [Thu Apr 15 01:47:47 2010] [error] [client 127.0.0.1] view:

> export_objects_as_pdf: info: p.setFont('Calibri', 7) was successfull,

> [Thu Apr 15 01:47:48 2010] [error] [client 127.0.0.1]

> pdfmetrics._fonts.has_key('Calibri') is True

> [Thu Apr 15 01:47:48 2010] [error] [client 127.0.0.1] view:

> export_objects_as_pdf: info: error in line p.setFont('Calibri', 7)

> [Thu Apr 15 01:47:48 2010] [error] [client 127.0.0.1] 'Calibri'

>

> The 'Calibri' output is a KeyError: 'Calibri' , I don't know why just

> 'Calibri' is printed out.

>

> 2010/4/15 Thomas Kremmel <thomasspin at gmail.com>

>

> Sorry was wrong with the fonts. Fonts are read from the file system..

>> But it would work using a http:// instead of a file system path. I used

>> that once and it worked, but in this list a user told me that I should not

>> do it that way ;-) Thus fonts are read from the file system.

>>

>> Yes I would agree with you that things should fail either all the time or

>> never.

>> I advised my admin to complete re-install rl with all libs in

>> the rl_addons package and the result is that everything worked as before.

>> PDF generation is running smoothly and 100% of the calls are successfully

>> conducted. This was how my source behaved the day before yesterday. Then

>> after one day the strange behavior started. Thus I will try tomorrow whether

>> this problems reoccur. For your information, _rl_accel was already installed

>> before. Thus that has nothing to do with my problems.

>>

>> Here is a list of additional information one could need to understand the

>> problem:

>>

>> environment:

>> apache with mod_wsgi

>> django 1.1.1

>>

>>

>> Reportlab version 2.4

>> _rl_accel.pyd installed

>>

>> 1. ReportLab Toolkit - open source PDF library... version 2.4

>> (in C:\Python25\lib\site-packages\reportlab\__init__.pyc)

>>

>> 2. _rl_accel extension module... found version 0.61

>> (in C:\Python25\lib\site-packages\_rl_accel.pyd)

>>

>> 3. Python Imaging Library... found

>> (in C:\Python25\lib\site-packages\PIL)

>>

>> 4. _renderPM extension module... found version 1.06

>> (in C:\Python25\lib\site-packages\_renderPM.pyd)

>>

>> 5. pyRXP xml parser... not found

>>

>>

>> !simpliefied! django view:

>>

>> def export_objects_as_pdf(request, model, object_list, user_object,

>> project_name, filename, month, year, billing_company, work_day_print_switch,

>> work_place_print_switch, LIST_HEADERS=None):

>> rl_config.warnOnMissingFontGlyphs = 0

>>

>> 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'))

>> if settings.WSGI_LOGGING is True:

>> print >> request.environ['wsgi.errors'], "view:

>> export_objects_as_pdf: info: pdfmetrics._fonts.has_key('Calibri') is False -

>> Successfully registered Calibri and Calibri-Bold Font"

>> else:

>> if settings.WSGI_LOGGING is True:

>> print >> request.environ['wsgi.errors'],

>> "pdfmetrics._fonts.has_key('Calibri') is True"

>> except Exception, e:

>> if settings.WSGI_LOGGING is True:

>> print >> request.environ['wsgi.errors'], "view:

>> export_objects_as_pdf: info: error while registering font"

>> print >> request.environ['wsgi.errors'], str(e)

>> request.user.message_set.create(message='Font could not be read

>> from file system. Please try again or contact your system administrator.')

>> return 0

>>

>> response = HttpResponse(mimetype='application/pdf')

>> response['Content-Disposition'] = 'attachment; filename=%s' % filename

>> buffer = StringIO()

>> PAGESIZE = pagesizes.portrait(pagesizes.A4)

>>

>> #COLOR definition

>> my_gray = colors.Color(.82,.82,.82)

>>

>> try:

>> company_cd_color1_rr = float(billing_company.cd_colour1_rr) /

>> float(255)

>> company_cd_color1_gg = float(billing_company.cd_colour1_gg) /

>> float(255)

>> company_cd_color1_bb = float(billing_company.cd_colour1_bb) /

>> float(255)

>> company_cd_color1 =

>> colors.Color(company_cd_color1_rr,company_cd_color1_gg,company_cd_color1_bb)

>> except Exception:

>> #print "value error in creating colors"

>> request.user.message_set.create(message='Company Color could not

>> be loaded. Please contact your system administrator.')

>> company_cd_color1 = colors.Color(0,0,0)

>>

>> # Create the PDF object, using the StringIO object as its "file."

>> p = canvas.Canvas(buffer,pagesize=PAGESIZE)

>> try:

>> p.setFont("Calibri", 7)

>> print >> request.environ['wsgi.errors'], "view:

>> export_objects_as_pdf: info: p.setFont('Calibri', 7) was successfull"

>> except KeyError, e:

>> if settings.WSGI_LOGGING is True:

>> print >> request.environ['wsgi.errors'], "view:

>> export_objects_as_pdf: info: error in line p.setFont('Calibri', 7)"

>> print >> request.environ['wsgi.errors'], str(e)

>> request.user.message_set.create(message='Font could not be loaded.

>> Please try again or contact your system administrator.')

>> return 0

>>

>> try:

>> path = fetch_resources(billing_company.logo_img.path)

>> company_img = Image(path,40,7)

>> company_img.drawOn(p, 500, 790)

>> except:

>> #could not find file

>> request.user.message_set.create(message='Company Logo could not be

>> loaded. Please contact your system administrator.')

>> pass

>> #here we draw the pdf .. removed it since i just want to show the

>> relevant parts

>>

>> # Close the PDF object cleanly.

>> p.showPage()

>> p.save()

>>

>> # Get the value of the StringIO buffer and write it to the response.

>> pdf = buffer.getvalue()

>> buffer.close()

>> response.write(pdf)

>> return response

>>

>>

>> !WSGI Logging!

>>

>> [Thu Apr 15 00:18:44 2010] [error] [client 127.0.0.1] view:

>> export_objects_as_pdf: info: pdfmetrics._fonts.has_key('Calibri') is False -

>> Successfully registered Calibri and Calibri-Bold Font

>> [Thu Apr 15 00:18:44 2010] [error] [client 127.0.0.1] view:

>> export_objects_as_pdf: info: p.setFont('Calibri', 7) was successfull

>>

>> [Thu Apr 15 00:26:07 2010] [error] [client 127.0.0.1]

>> pdfmetrics._fonts.has_key('Calibri') is True

>> [Thu Apr 15 00:26:07 2010] [error] [client 127.0.0.1] view:

>> export_objects_as_pdf: info: p.setFont('Calibri', 7) was successfull

>> [Thu Apr 15 00:26:08 2010] [error] [client 127.0.0.1]

>> pdfmetrics._fonts.has_key('Calibri') is True

>> [Thu Apr 15 00:26:08 2010] [error] [client 127.0.0.1] view:

>> export_objects_as_pdf: info: p.setFont('Calibri', 7) was successfull

>>

>> This logging is how it should behave. I will check the logs tomorrow when

>> the error occurs again how the login changes and will post it here.

>> best regards

>> Thomas

>>

>>

>>

>>

>>

>> 2010/4/15 Andy Robinson <andy at reportlab.com>

>>

>>> On 15 April 2010 14:17, Thomas Kremmel <thomasspin at gmail.com> wrote:

>>>

>>> >>> Is there just one machine, or some kind of a cluster?

>>> > one machine, but static content (like fonts, pictures) are served via a

>>> > separate server.

>>>

>>> Images I understand, but fonts? We have always loaded our fonts off

>>> the server file system. Is your settings.FONTDIR some kind of

>>> 'http://' URL instead of a file system path? This MIGHT work - I

>>> don't know - but it's your server side that needs the font, not their

>>> browser.

>>>

>>>

>>> > _rl_accel

>>> I'm still very surprised at the inconsistency. Please let us know in

>>> a day or two if it's stable. If so we still have an issue to look

>>> into. IMHO if _rl_accel is missing then either things should fail

>>> obviously 100% of the time (with a clue in the traceback), or work

>>> robustly but just a bit slower.

>>>

>>> - Andy

>>> _______________________________________________

>>> reportlab-users mailing list

>>> reportlab-users at lists2.reportlab.com

>>> http://two.pairlist.net/mailman/listinfo/reportlab-users

>>>

>>

>>

>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://two.pairlist.net/pipermail/reportlab-users/attachments/20100419/a44190a2/attachment-0001.htm>


More information about the reportlab-users mailing list