I'm using reportlab in python to make a pdf report. I was able to use the bold tag within my paragraph as needed as shown below but as soon I used a specific font, the bold tags no longer appear as bold.
import reportlab.rl_config
reportlab.rl_config.TTFSearchPath.append('/usr/share/fonts/FansyFont/')
from reportlab.pdfbase.pdfmetrics import registerFontFamily
registerFontFamily('FansyFont', normal='FansyFont-Regular', bold='FansyFont-Bold', italic='FansyFont-Italic', boldItalic='FansyFont-BoldItalic')
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
pdfmetrics.registerFont(TTFont('FansyFont-Bold', 'FansyFont-Bold.ttf'))
pdfmetrics.registerFont(TTFont('FansyFont-Regular', 'FansyFont-Regular.ttf'))
pdfmetrics.registerFont(TTFont('FansyFont-Italic', 'FansyFont-Italic.ttf'))
pdfmetrics.registerFont(TTFont('FansyFont-BoldItalic', 'FansyFont-BoldItalic.ttf'))
styles = getSampleStyleSheet()
ps = ParagraphStyle(styles['Normal'], fontName='FansyFont-Regular', alignment=TA_LEFT, fontSize=8)
Paragraph("<b>Table1:</b> some nice table", ps)
Is there a was to make reportlab understand that it should switch the font to "FansyFont-Bold" or is there a way to make it pick the correct one?
Also, in other programming languages or even other python packages such as matplotlib, you would just specify the font family not the individual font files for different weights so how come I need to specify it in reportlab?
After registering all the fonts, you want to tie them together with registerFontFamily()
pdfmetrics.registerFont(TTFont('FansyFont-Bold', 'FansyFont-Bold.ttf'))
pdfmetrics.registerFont(TTFont('FansyFont-Regular', 'FansyFont-Regular.ttf'))
pdfmetrics.registerFont(TTFont('FansyFont-Italic', 'FansyFont-Italic.ttf'))
pdfmetrics.registerFont(TTFont('FansyFont-BoldItalic', 'FansyFont-BoldItalic.ttf'))
pdfmetrics.registerFontFamily(
'FansyFont',
normal='FansyFont-Regular',
bold='FansyFont-Bold',
italic='FansyFont-Italic',
boldItalic='FansyFont-BoldItalic')
Then, just use FansyFont
as your fontName
:
ps = ParagraphStyle(styles['Normal'], fontName='FansyFont', alignment=TA_LEFT, fontSize=8)