Adding a font for use in ReportLab

Posted by Jimmy McCarthy on Stack Overflow See other posts from Stack Overflow or by Jimmy McCarthy
Published on 2010-04-16T21:14:35Z Indexed on 2010/04/16 22:53 UTC
Read the original article Hit count: 386

Filed under:
|
|

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.

© Stack Overflow or respective owner

Related posts about reportlab

Related posts about python