pythonpython-3.xunicodeutf-8pyfpdf

AttributeError: 'table__n_a_m_e' object has no attribute 'getBestFullName' error on fpdf2


I am tring to run the example code from fpdf2 pyfpdf.github.io/fpdf2/Unicode.html. It gives me the error

Traceback (most recent call last): File "fpdf2.py", line 12, in pdf.add_font('DejaVu', fname='DejaVuSansCondensed.ttf') File "C:\Program Files\Python36\lib\site-packages\fpdf\fpdf.py", line 1916, in add_font "name": re.sub("[ ()]", "", font["name"].getBestFullName()), AttributeError: 'table__n_a_m_e' object has no attribute 'getBestFullName'

I have installed the DejaVuSansCondensed font on my system. I couldn't figure out the error it gives me. Here is the code.

from fpdf import FPDF

pdf = FPDF()
pdf.add_page()

# Add a DejaVu Unicode font (uses UTF-8)
# Supports more than 200 languages. For a coverage status see:
# http://dejavu.svn.sourceforge.net/viewvc/dejavu/trunk/dejavu-fonts/langcover.txt
pdf.add_font('DejaVu', fname='DejaVuSansCondensed.ttf')
pdf.set_font('DejaVu', size=14)

text = u"""
English: Hello World
Greek: Γειά σου κόσμος
Polish: Witaj świecie
Portuguese: Olá mundo
Russian: Здравствуй, Мир
Vietnamese: Xin chào thế giới
Arabic: مرحبا العالم
Hebrew: שלום עולם
"""

for txt in text.split('\n'):
    pdf.write(8, txt)
    pdf.ln(8)

# Add a Indic Unicode font (uses UTF-8)
# Supports: Bengali, Devanagari, Gujarati, 
#           Gurmukhi (including the variants for Punjabi) 
#           Kannada, Malayalam, Oriya, Tamil, Telugu, Tibetan
pdf.add_font('gargi', fname='gargi.ttf')
pdf.set_font('gargi', size=14)
pdf.write(8, u'Hindi: नमस्ते दुनिया')
pdf.ln(20)

# Add a AR PL New Sung Unicode font (uses UTF-8)
# The Open Source Chinese Font (also supports other east Asian languages)
pdf.add_font('fireflysung', fname='fireflysung.ttf')
pdf.set_font('fireflysung', size=14)
pdf.write(8, u'Chinese: 你好世界\n')
pdf.write(8, u'Japanese: こんにちは世界\n')
pdf.ln(10)

# Add a Alee Unicode font (uses UTF-8)
# General purpose Hangul truetype fonts that contain Korean syllable 
# and Latin9 (iso8859-15) characters.
pdf.add_font('eunjin', fname='Eunjin.ttf')
pdf.set_font('eunjin', size=14)
pdf.write(8, u'Korean: 안녕하세요')
pdf.ln(20)

# Add a Fonts-TLWG (formerly ThaiFonts-Scalable) (uses UTF-8)
pdf.add_font('waree', fname='Waree.ttf')
pdf.set_font('waree', size=14)
pdf.write(8, u'Thai: สวัสดีชาวโลก')
pdf.ln(20)

# Select a standard font (uses windows-1252)
pdf.set_font('helvetica', size=14)
pdf.ln(10)
pdf.write(5, 'This is standard built-in font')

pdf.output("unicode.pdf")

Solution

  • I just ran into the same issue. Since this question ist still quite new, here's what I did to solve it:

    First, note: the problem seems to be a known bug in fpdf2, stemming from the introduction of the library fonttools starting with fpdf2 v2.5.7.

    https://github.com/PyFPDF/fpdf2/issues/524

    So i uninstalled fonttools and fpdf2, then reinstalled a different version of fpdf2:

    pip install fpdf2==2.4.6
    

    Now, the AttributeError is gone.