views:

181

answers:

1

I'm trying to add a font to the python ReportLab so that I can use it for a function. The function is using canvas.Canvas to draw a bunch of text in a PDF, nothing complicated, but I need to add a fixed width font for layout issues.

When I tried to register a font using what little info I could find, that seemed to work. But when I tried to call .addFont('fontname') from my Canvas object I keep getting

"PDFDocument instance has no attribute 'addFont'"

Is the function just not implemented? How do I get access to fonts other than the 10 or so default ones that are listed in .getAvailableFonts? Thanks.

Some example code of what I'm trying to make happen:

from reportlab.pdfgen import canvas
c = canvas.Canvas('label.pdf')
c.addFont('TestFont') #This throws the error listed above, regardless of what argument I use (whether it refers to a font or not).
c.drawString(1,1,'test data here')
c.showPage()
c.save()

To register the font, I tried

from reportlab.lib.fonts import addMapping
from reportlab.pdfbase import pdfmetrics

pdfmetrics.registerFont(TTFont('TestFont', 'ghettomarquee.ttf'))
addMapping('TestFont', 0, 0, 'TestFont')

where 'ghettomarquee.ttf' was just a random font I had lying around.

+2  A: 
c.addFont('TestFont')
c.drawString(1,1,'test data here')

setFont to set the font name you're going to use, and drawString.

ReportLab will automatically embed the font if you use it in the document, you don't have to manually add it after you've registered the font globally under a name.

bobince