[reportlab-users] Problem using FAQ example for embedding font
Gerry Reno
greno at verizon.net
Tue Sep 16 18:06:18 EDT 2008
Ok, I made a lot of progress but still not quite there with embedding a
'FontSpecific' font.
I took everything out of the container and am running it standalone from
command line.
Here is the problem:
==================================================================
# python /tmp/test.py
_encodings = {}
_encodings = {'ZapfDingbatsEncoding':
<reportlab.pdfbase.pdfmetrics.Encoding instance at 0xb7c810ac>}
_encodings = {'SymbolEncoding': <reportlab.pdfbase.pdfmetrics.Encoding
instance at 0xb7c87aec>, 'ZapfDingbatsEncoding':
<reportlab.pdfbase.pdfmetrics.Encoding instance at 0xb7c810ac>}
_encodings = {'SymbolEncoding': <reportlab.pdfbase.pdfmetrics.Encoding
instance at 0xb7c87aec>, 'ZapfDingbatsEncoding':
<reportlab.pdfbase.pdfmetrics.Encoding instance at 0xb7c810ac>,
'WinAnsiEncoding': <reportlab.pdfbase.pdfmetrics.Encoding instance at
0x9f3afec>}
testing for existence of MICR font files...
afmFile = /usr/share/fonts/default/Type1/GnuMICR.afm
pfbFile = /usr/share/fonts/default/Type1/GnuMICR.pfb
justFace = pdfmetrics.EmbeddedType1Face(afmFile, pfbFile)
pdfmetrics.registerTypeFace(justFace)
justFont = pdfmetrics.Font('GnuMICR', faceName, 'GnuMICREncoding')
_encodings = {'GnuMICREncoding': <reportlab.pdfbase.pdfmetrics.Encoding
instance at 0x9f412cc>, 'SymbolEncoding':
<reportlab.pdfbase.pdfmetrics.Encoding instance at 0xb7c87aec>,
'ZapfDingbatsEncoding': <reportlab.pdfbase.pdfmetrics.Encoding instance
at 0xb7c810ac>, 'WinAnsiEncoding':
<reportlab.pdfbase.pdfmetrics.Encoding instance at 0x9f3afec>}
pdfmetrics.registerFont(justFont)
_encodings = {'GnuMICREncoding': <reportlab.pdfbase.pdfmetrics.Encoding
instance at 0x9f412cc>, 'SymbolEncoding':
<reportlab.pdfbase.pdfmetrics.Encoding instance at 0xb7c87aec>,
'ZapfDingbatsEncoding': <reportlab.pdfbase.pdfmetrics.Encoding instance
at 0xb7c810ac>, 'WinAnsiEncoding':
<reportlab.pdfbase.pdfmetrics.Encoding instance at 0x9f3afec>}
_encodings = {'GnuMICREncoding': <reportlab.pdfbase.pdfmetrics.Encoding
instance at 0x9f412cc>, 'SymbolEncoding':
<reportlab.pdfbase.pdfmetrics.Encoding instance at 0xb7c87aec>,
'ZapfDingbatsEncoding': <reportlab.pdfbase.pdfmetrics.Encoding instance
at 0xb7c810ac>, 'WinAnsiEncoding':
<reportlab.pdfbase.pdfmetrics.Encoding instance at 0x9f3afec>}
Traceback (most recent call last):
File "/tmp/test.py", line 72, in <module>
form.draw_micr()
File "/tmp/test.py", line 64, in draw_micr
t.textOut("A1234567890A ABCD")
File "/usr/lib/python2.5/site-packages/reportlab/pdfgen/textobject.py",
line 353, in textOut
self._x = self._x + self._canvas.stringWidth(text, self._fontname,
self._fontsize)
File "/usr/lib/python2.5/site-packages/reportlab/pdfgen/canvas.py", line
1283, in stringWidth
return pdfmetrics.stringWidth(text, fontName, fontSize)
File "/usr/lib/python2.5/site-packages/reportlab/pdfbase/pdfmetrics.py",
line 721, in _py_stringWidth
return getFont(fontName).stringWidth(text, fontSize, encoding=encoding)
File "/usr/lib/python2.5/site-packages/reportlab/pdfbase/pdfmetrics.py",
line 402, in _py_stringWidth
return sum([sum(map(f.widths.__getitem__,map(ord,t))) for f, t in
unicode2T1(text,[self]+self.substitutionFonts)])*0.001*size
File "/usr/lib/python2.5/site-packages/reportlab/pdfbase/pdfmetrics.py",
line 49, in _py_unicode2T1
R.append((font,utext.encode(enc)))
LookupError: unknown encoding: GnuMICREncoding
==================================================================
Here is the code:
==================================================================
# cat /tmp/test.py
#!/usr/env/python
import os
import reportlab
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfgen.canvas import Canvas
from reportlab.lib.pagesizes import letter, A4
from reportlab.lib.units import cm, inch
Courier = 'Courier'
Helvetica = 'Helvetica'
Helvetica_Bold = 'Helvetica-Bold'
Helvetica_Bold_Oblique = 'Helvetica-BoldOblique'
GnuMICR = 'GnuMICR'
class Form:
def __init__(self, **kw):
self.pdfpath = kw.get('pdfpath', 'test.pdf')
self.verbose = kw.get('verbose', 0)
self.text_font = kw.get('typeface', Helvetica)
self.text_size = kw.get('fontsize', 10)
self.pagewidth, self.pageheight = letter # keep for later
self.canvas = Canvas(self.pdfpath, pagesize=letter)
def draw_micr(self):
# folder = os.path.dirname(reportlab.__file__) + os.sep + 'fonts'
folder = os.path.dirname('/usr/share/fonts/default/Type1/')
# folder = os.path.join('addons','l10n_micr','fonts')
afmFile = os.path.join(folder, 'GnuMICR.afm')
pfbFile = os.path.join(folder, 'GnuMICR.pfb')
print 'testing for existence of MICR font files...'
if os.path.exists(afmFile):
print 'afmFile = '+str(afmFile)
if os.path.exists(pfbFile):
print 'pfbFile = '+str(pfbFile)
print "justFace = pdfmetrics.EmbeddedType1Face(afmFile, pfbFile)"
justFace = pdfmetrics.EmbeddedType1Face(afmFile, pfbFile)
faceName = 'GnuMICR' # pulled from AFM file
print "pdfmetrics.registerTypeFace(justFace)"
pdfmetrics.registerTypeFace(justFace)
# justFont = pdfmetrics.Font('GnuMICR', faceName, 'WinAnsiEncoding')
# justFont = pdfmetrics.Font('GnuMICR', faceName, 'FontSpecific')
print "justFont = pdfmetrics.Font('GnuMICR', faceName, 'GnuMICREncoding')"
justFont = pdfmetrics.Font('GnuMICR', faceName, 'GnuMICREncoding')
print "pdfmetrics.registerFont(justFont)"
try:
pdfmetrics.registerFont(justFont)
except AttributeError, e:
print "Check variable spelling for enc.name", e
except FontError, e:
print "Warning: ", e
#
# Change this to UTF-8 if you plan tu use Reportlab's UTF-8 support
#
# reportlab use "code page 1252" encoding by default. cfr reportlab user
guide p.46
### encoding = 'utf-8'
c = self.canvas
t = c.beginText(72, 58)
t.setFont("GnuMICR", 12)
# t.setFont("Helvetica", 12)
t.textOut("A1234567890A ABCD")
c.drawText(t)
c.save()
if __name__ == '__main__':
import test
form = Form()
form.draw_micr()
==================================================================
I can see that it has the GnuMICREncoding registered but for some reason
it still fails.
Regards,
Gerry
More information about the reportlab-users
mailing list