4

i've tried to copy a dejavucondensed.ttf into pyfpdf/font/ But no luck when i tried

>>> from pyfpdf import *
>>> p = FPDF()
>>> p.add_font('dejavucondensed')
SyntaxError: Non-ASCII character '\xfd' in file pyfpdf/font/dejavucondensed.font 
on line 2, 
but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
mwolfe02
  • 23,787
  • 9
  • 91
  • 161
adesst
  • 297
  • 3
  • 7

2 Answers2

1

This is probably a Unicode/encoding problem (see the PEP linked in the message). I do not have pypdf installed here, but according to the reference documentation for add_font you need to use the uni parameter:

pdf.add_font('DejaVu', '', 'DejaVuSansCondensed.ttf', uni=True)
Florian Brucker
  • 9,621
  • 3
  • 48
  • 81
1

For anyone who's looking at this all this time later, try this. I'm in Ubuntu Linux using Python 3.8. Go to your installation of fpdf, which for me was in /usr/local/lib/python3.8/dist-packages/fpdf.

If there is not a folder named fonts there, do a mkdir fonts. Grab the ttf file for your font from any number of sources and place it in the fonts folder. I wanted Calibri Regular and named my file calibri_regular.ttf.

Then in your interpreter or script:

from fpdf import FPDF
pdf = FPDF()

pdf.add_font('Calibri Regular', '', 'calibri_regular.ttf', uni=True)
pdf.set_font('Calibri Regular', '', 12)

At this point you're ready to write to the PDF in that font.

Pat Jones
  • 876
  • 8
  • 18