Pragmatism in the real world

Notes on embedding fonts in rst2pdf

I wanted to use Helvetica Light in a PDF that I’m creating using rst2pdf and this proved a little tricker than I expected.

Note that to use fonts with rst2pdf, you need to install fontconfig using brew:

brew install fontconfig

Now, on to the problem!

In this case, I discovered that setting stdFont: Helvetica-Light or stdFont: HelveticaLight in the style file resulted in Verdana being used in the PDF file!

Fortunately, the -v switch to rst2pdf is your friend when you want to know what’s happening:

[INFO] findfonts.py:270 Trying to embed Helvetica-Light
[INFO] findfonts.py:307 fname for findTTFont: Helvetica-Light
[INFO] findfonts.py:308 Variants via findTTFont: ['/Library/Fonts/Microsoft/Verdana.ttf', '/Library/Fonts/Microsoft/Verdana Bold.ttf', '/Library/Fonts/Microsoft/Verdana Italic.ttf', '/Library/Fonts/Microsoft/Verdana Bold Italic.ttf']
[INFO] findfonts.py:317 Registering font: Verdana from /Library/Fonts/Microsoft/Verdana.ttf
[INFO] findfonts.py:317 Registering font: Verdana Bold from /Library/Fonts/Microsoft/Verdana Bold.ttf
[INFO] findfonts.py:317 Registering font: Verdana Italic from /Library/Fonts/Microsoft/Verdana Italic.ttf
[INFO] findfonts.py:317 Registering font: Verdana Bold Italic from /Library/Fonts/Microsoft/Verdana Bold Italic.ttf
[INFO] findfonts.py:329 Embedding via findTTFont as ['Verdana', 'Verdana Bold', 'Verdana Italic', 'Verdana Bold Italic']

As can be seen by the output, rst2pdf can’t find Helvetica-Light and so has substituted Verdana instead. This would seem to be because it couldn’t find a TTF file for the font on my Mac as Helvetica Light is stored in a dfont file.

To solve this, I used DFontSplitter to extract Helvetica Light as a separate TTF file and added this as an embedded font to the style file:

embeddedFonts: []
    [fonts/HelveticaLight.ttf, fonts/HelveticaLight.ttf, fonts/HelveticaLight.ttf, fonts/HelveticaLight.ttf]

and re-ran with -v which gave me this information:

[INFO] styles.py:276 Registering font: fonts/HelveticaLight from ./fonts/HelveticaLight.ttf

I now know that the name to use when referencing this font is “fonts/HelveticaLight” and so I can use it within the style file like this:

stdFont: fonts/HelveticaLight

and the font is embedded as I expected.