[reportlab-users] crash when using TTFont
Dries
dries at urga.be
Thu Jul 27 12:47:15 EDT 2006
hi,
I'm trying to dynamically generate a pdf file that has a sample text
printed on every page, by feeding the program a .ttf file at the command
line.
It seems to work until I tried to feed it the same .ttf file twice. This
matters because apparently some .ttf files internally use the same name
(I have this with Bank_md.ttf and BankGothic-Medium.ttf for example.
Here is my python script:
=== Start Code font2pdf.py====
#!/usr/bin/env python
import os
import sys
sys.path=['/Users/dries/lib/python']+sys.path
import reportlab
import reportlab.rl_config
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import *
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter, A4
from reportlab.lib.units import *
from reportlab.lib.colors import pink, black, red, blue, green
# Some script wide variables
text = "The quick brown fox jumps over the lazy dog."
pdf_file="fonts.pdf"
ttf_files = sys.argv[1:]
width, height = A4
# we know some glyphs are missing, suppress warnings
reportlab.rl_config.warnOnMissingFontGlyphs = 0
# get the canvas
pdf = canvas.Canvas(pdf_file, pagesize=A4, verbosity=1)
print pdf.getAvailableFonts()
# Main loop
for ttf_file in ttf_files:
print ttf_file
try:
fontname = os.path.basename(ttf_file)
font = TTFont(fontname,ttf_file)
print font
print fontname
pdfmetrics.registerFont(font)
print pdfmetrics.getRegisteredFontNames()
pdf.init_graphics_state()
pdf.translate(1*cm,height-1*cm)
pdf.setFont(fontname, 14)
pdf.drawString(0,-14,text)
#pdf.setFont(fontname, 22)
#pdf.drawString(0,-28,text)
#print ttf_file, " converted..."
#pdf.showPage()
except:
print 'Raised an EXCECPTION!! while processing ', ttf_file, "
Skipping ..."
print sys.exc_type, sys.exc_value
pdf.save()
print reportlab.Version
=== End of code
This is how I launch:
~/workspace/font2pdf $ font2pdf.py
/Users/dries/lib/python/reportlab/fonts/rina.ttf
/Users/dries/lib/python/reportlab/fonts/rina.ttf
This is what I get:
['Courier', 'Courier-Bold', 'Courier-BoldOblique', 'Courier-Oblique',
'Helvetica', 'Helvetica-Bold', 'Helvetica-BoldOblique',
'Helvetica-Oblique', 'Symbol', 'Times-Bold', 'Times-BoldItalic',
'Times-Italic', 'Times-Roman', 'ZapfDingbats']
/Users/dries/lib/python/reportlab/fonts/rina.ttf
<reportlab.pdfbase.ttfonts.TTFont instance at 0x2aaaac745bd8>
rina.ttf
['Helvetica', 'Symbol', 'ZapfDingbats', 'rina.ttf']
/Users/dries/lib/python/reportlab/fonts/rina.ttf
<reportlab.pdfbase.ttfonts.TTFont instance at 0x2aaaab403bd8>
rina.ttf
['Helvetica', 'Symbol', 'ZapfDingbats', 'rina.ttf']
Traceback (most recent call last):
File "./font2pdf.py", line 53, in ?
pdf.save()
File "/Users/dries/lib/python/reportlab/pdfgen/canvas.py", line 843,
in save
self._doc.SaveToFile(self._filename, self)
File "/Users/dries/lib/python/reportlab/pdfbase/pdfdoc.py", line 218,
in SaveToFile
f.write(self.GetPDFData(canvas))
File "/Users/dries/lib/python/reportlab/pdfbase/pdfdoc.py", line 230,
in GetPDFData
fnt.addObjects(self)
File "/Users/dries/lib/python/reportlab/pdfbase/ttfonts.py", line
1099, in addObjects
pdfFont.ToUnicode = doc.Reference(cmapStream, 'toUnicodeCMap:' +
baseFontName)
File "/Users/dries/lib/python/reportlab/pdfbase/pdfdoc.py", line 459,
in Reference
raise ValueError, "redefining named object: "+repr(name)
ValueError: redefining named object: 'toUnicodeCMap:AAAAAA+Rina'
Can someone help to pin this down? Thanks.
More information about the reportlab-users
mailing list