[reportlab-users] Setting TrueType fonts in table cell

Nils Smeds nils.smeds at gmail.com
Wed Sep 25 06:38:36 EDT 2013


I am having problem setting the font to a TrueType font in reportlab.

Here is my snippet:

base_style=[('GRID', (0,0), (-1,-1), 1, (0,0,0)),
* ('FONT', (0,0), (0,-1), 'Courier'),*
('BOX', (0,0), (-1,-1), 3, (0,0,0)),
('ALIGN',(1,0),(1,-1),'RIGHT'),
('ALIGN',(3,0),(3,-1),'RIGHT'),
('VALIGN',(0,0),(-1,-1),'TOP')]

[...]
t=Table(data)
styles=list(base_style)
if(cur_name_span[1]>cur_name_span[0]):
spans.append(('SPAN',(0,cur_name_span[0]),(0,cur_name_span[1])))
if(cur_simsatt_span[1]>cur_simsatt_span[0]):

spans.append(('SPAN',(2,cur_simsatt_span[0]),(2,cur_simsatt_span[1])))
for s in spans:
#print "Adding span: ",s
styles.append(s)
t.setStyle(TableStyle(styles))


This works great with the built-in font Courier. However I do need a more
complete font so I am doing the following in the beginning of my script

from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfbase.pdfmetrics import registerFontFamily


pdfmetrics.registerFont(TTFont('SansR',
'/usr/share/fonts/liberation/LiberationSans-Regular.ttf'))
pdfmetrics.registerFont(TTFont('SansB',
'/usr/share/fonts/liberation/LiberationSans-Bold.ttf'))
pdfmetrics.registerFont(TTFont('SansI',
'/usr/share/fonts/liberation/LiberationSans-Italic.ttf'))
pdfmetrics.registerFont(TTFont('SansBI',
'/usr/share/fonts/liberation/LiberationSans-BoldItalic.ttf'))

registerFontFamily('Sans',normal='SansR',bold='SansB',italic='SansI',boldItalic='SansBI')


This works great for all regular paragraphs and graphical objects. I get
the special characters I need. But, I can not specify my SansR fontname in
the table format.
If I substitute 'Courier' above with any of the Sans names I get:

Traceback (most recent call last):
File "/home/nsmeds/bin/GetDiplomDBI-oop.py", line 650, in <module>
doc.PDFcanvas.save()
File "/usr/lib64/python2.7/site-packages/reportlab/pdfgen/canvas.py",
line 1123, in save
self._doc.SaveToFile(self._filename, self)
File "/usr/lib64/python2.7/site-packages/reportlab/pdfbase/pdfdoc.py",
line 235, in SaveToFile
f.write(self.GetPDFData(canvas))
File "/usr/lib64/python2.7/site-packages/reportlab/pdfbase/pdfdoc.py",
line 247, in GetPDFData
fnt.addObjects(self)
File "/usr/lib64/python2.7/site-packages/reportlab/pdfbase/ttfonts.py",
line 1126, in addObjects
pdfFont.ToUnicode = doc.Reference(cmapStream, 'toUnicodeCMap:' +
baseFontName)
File "/usr/lib64/python2.7/site-packages/reportlab/pdfbase/pdfdoc.py",
line 516, in Reference
raise ValueError, "redefining named object: "+repr(name)
ValueError: redefining named object: 'toUnicodeCMap:AAAAAA+LiberationSans'


Any ideas what might go wrong?

This is python-reportlab-2.5-7.fc19.x86_64
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://two.pairlist.net/pipermail/reportlab-users/attachments/20130925/56198cea/attachment.htm>


More information about the reportlab-users mailing list