Search code examples
pythonfontsfpdf

Python FPDF can't find the font


pdf.add_font('DejaVu', '', 'DejaVuSansCondensed.ttf', uni=True)

Works only in PyCharm console, if run from the command line i get an error


File "C:\\Program Files\\Python310\\lib\\site-packages\\fpdf\\fpdf.py", line 469, in add_font
raise RuntimeError("TTF Font file not found: %s" % fname)
RuntimeError: TTF Font file not found: DejaVuSansCondensed.ttf
←\[?25h

the font is at the root

when using pycharm, an additional file is automatically created DejaVuSansCondensed.pkl and everything works fine, but when using the command line, the file is not created and I get an error. Please help me with the solution.


Solution

  • FPDF only works with absolute path, works in this form:

    path = os.path.abspath('DejaVuSansCondensed.ttf')
    pdf.add_font('DejaVu', '', path, uni=True)