Michael Michael - 1 year ago 137
Python Question

reportlab add font with registerFont

I've to use the registerFontFamily method from reportlab.pdfbase.pdfmetrics.
I tried to add two fonts to a family but I can't use the bold font with "<b>sometext</b>". I need this feature for my report. I can only use the regular one and I don't know why.

Here's the code.

registerFont(TTFont('Own_Font', os.path.dirname(os.path.abspath(__file__)) + '\\OwnSans-Regular.ttf'))
registerFont(TTFont('OwnBold_Font', os.path.dirname(os.path.abspath(__file__)) + '\\OwnSans-Bold.ttf'))


# define parameter for the page and paragraph font
STYLES = getSampleStyleSheet()
STYLES.add( ParagraphStyle(name='Text', fontName = 'Own_Font', fontSize = 10 ))

STYLES.add( ParagraphStyle(name='Centered', fontName = 'Own_Font', fontSize = 10, alignment=TA_CENTER ))
STYLES.add( ParagraphStyle(name='CenteredBig', parent=STYLES['Centered'], fontSize=18, spaceAfter=10) )
STYLES.add( ParagraphStyle(name='CenteredMedium', parent=STYLES['Centered'], fontSize=15, spaceAfter=10) )

I've got the following error message:

File "X:\tools\Python2\lib\site-packages\reportlab-2.7-py2.7-win32.egg\reportlab\platypus\paragraph.py", line 916, in __init__
self._setup(text, style, bulletText or getattr(style,'bulletText',None), frags, cleanBlockQuotedText)
File "X:\tools\Python2\lib\site-packages\reportlab-2.7-py2.7-win32.egg\reportlab\platypus\paragraph.py", line 938, in _setup
style, frags, bulletTextFrags = _parser.parse(text,style)
File "X:\tools\Python2\lib\site-packages\reportlab-2.7-py2.7-win32.egg\reportlab\platypus\paraparser.py", line 1083, in parse
File "X:\tools\Python2\lib\site-packages\reportlab-2.7-py2.7-win32.egg\reportlab\lib\xmllib.py", line 562, in finish_starttag
self.handle_starttag(tag, method, attrs)
File "X:\tools\Python2\lib\site-packages\reportlab-2.7-py2.7-win32.egg\reportlab\lib\xmllib.py", line 596, in handle_starttag
File "X:\tools\Python2\lib\site-packages\reportlab-2.7-py2.7-win32.egg\reportlab\platypus\paraparser.py", line 823, in start_para
self._stack = [self._initial_frag(attr,_paraAttrMap)]
File "X:\tools\Python2\lib\site-packages\reportlab-2.7-py2.7-win32.egg\reportlab\platypus\paraparser.py", line 817, in _initial_frag
frag.fontName, frag.bold, frag.italic = ps2tt(style.fontName)
File "X:\tools\Python2\lib\site-packages\reportlab-2.7-py2.7-win32.egg\reportlab\lib\fonts.py", line 75, in ps2tt
raise ValueError("Can't map determine family/bold/italic for %s" % psfn)
ValueError: Can't map determine family/bold/italic for Own_Font

Thank you in advance.

Answer Source

The first problem is that you made a typo in the registerFontFamily. To be more specific you register the normal font as being the bold by doing bold='Own_Font'.

The second problem is that your font family doesn't have fonts for all types (in this case italic and boldItalic). According to the manual, if you read Vera as Own_font:

If we only have a Vera regular font, no bold or italic then we must map all to the same internal fontname. <b> and <i> tags may now be used safely, but have no effect. After registering and mapping the Vera font as

Thus to fix it you should replace the affected line like this:


This should make <b></b> function as expected.