Reportlab – All About Fonts

Have you ever wondered how to embed custom fonts in Reportlab? Or maybe you just want to switch fonts or change the font’s color. Well in this tutorial, we’ll take a look at all of these questions. You’ll need to go out and download a copy of Reportlab as it isn’t a part of the standard Python installation. You can go to PyPI or the official Reportlab website to get the package. Reportlab only works with Python 2.5-2.7, so keep that in mind. Once you’re ready, we can continue.

How to Embed Fonts in PDFs

We’ll be using a barcode font for this example called free3of9. You can find it on numerous websites, including this one. I’ve used this font in production before. It should be noted that Reportlab can actually do barcodes itself, so you really don’t need this font at all. However, we’re going to use it for this example for demonstration purposes. After you have downloaded the font and put it somewhere, we can continue. Are you ready? Then let’s look at some code:

First of all, we need to import several items from different parts of Reportlab. Next we need to do the following call to register the font with Reportlab:

pdfmetrics.registerFont(TTFont("Free 3 of 9 Regular", barcode_font))

Occasionally this doesn’t work for some reason. Perhaps the ttf file is poorly defined or corrupt. I don’t really know. But if you’re lucky, the font might have come with afm and pfb files. If so, you can register it that way. I found this out when I was playing with the demo copy of a MICR font I was using for creating checks. You can get the trial version if you like.

Most of the time, the first method works. However, you should know about this fallback method in case you run into weird issues with your TrueType fonts. Now we’re ready to learn about Reportlab’s standard fonts.

How to Switch Between Fonts in Reportlab

Reportlab supports several fonts internally. You can think of them as standard or default fonts. We’ll create a simple piece of code that is based on one of their examples to create a document with different fonts in it. Here’s the code:

This script will create two PDFs: StandardFonts_MacRoman.pdf and StandardFonts_WinAnsi.pdf. As you can see, we just use a nested pair of for loops to extract the various fonts and register them with Reportlab. This shows one way of setting fonts. There’s actually another way to do this where we don’t have to register the font. Reportlab supports some XML tags, so you can use that to set the font. Let’s take a look at that code:

If you run the code above, you should get something like the following:

As you can see, all we had to do was use a font tag and specify the font name. I have yet to find a full list of default fonts that Reportlab supports, so you’ll have to play around with that. However, I think these are the main three, plus some variants thereof. You’ll also note that Reportlab is not case sensitive in regards to the font name. Note: we use the createParagraph method just to reduce code repetition.

Now we’re ready to discuss changing font colors!

How to Change Font Colors in Reportlab

Changing colors of fonts in Reportlab is extremely easy if you use the tag method we used in the last example. All you have to do is add on to the font definition and specify a color. We’ll just modify the last piece of code for this example:

If you run the code above, you’ll get a document that is very similar to the last one, except that each line will be a different color.

Wrapping Up

Now you know how to manipulate the fonts in Reportlab. If you want to learn additional formatting tricks, I recommend looking at the Paragraph XML Markup Tags section in the Reportlab user guide, which is in chapter 6. You’ll find out how to bold, underline, italicize, and strike-through as well as how to do superscripts and subscripts. There’s also additional information about fonts, especially regarding setting fonts for canvas objects, in the user guide. Good luck and happy coding!