Search code examples
pythonfpdf

Difficulty Importing Fonts in Python FPDF package


I'm having a tough time trying to get something working which I think should be straightforward.

I'm new to python and learning via projects which in this case is a simple PDF generator. I want to add custom fonts to my program (Poppins) and I can get it to work if I include the fonts in the exact location as main.py, but ideally, I want these stored in a separate folder.

I've tried numerous suggestions but nothing has worked for me yet. Such as:

  • appending/inserting the font filepath to sys.path (link)
  • creating an __init__.py file in the main directory with the following
    import sys
    sys.path.insert(1, '.')
  • Also tried a blank __init__.py file in the font directory

I've attached a screenshot of my code below along with the error message. Thanks in advance for any support. My python code


Solution

  • From the add_font documentation of FPDF:

    Note: the font source files must be accessible. They are searched successively in (if these constants are defined):

    FPDF_FONTPATH (by default, the font folder in the fpdf package directory)

    SYSTEM_TTFONTS (e.g. C:\WINDOWS\FONTS)