Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.add_font() -- AttributeError: 'table__n_a_m_e' object has no attribute 'getBestFullName' #524

Closed
TchilDill opened this issue Sep 9, 2022 · 7 comments · Fixed by #538
Closed

Comments

@TchilDill
Copy link

TchilDill commented Sep 9, 2022

When updating to latest release (v2.5.7) and checking out if my older scripts were compatible, I discovered that the add_font created an attribute error and seems to me like a bug.

With code below on Windows assuming that the font file is accessible and all works just fine with fpdf2 v2.4.6.

from fpdf import FPDF, HTMLMixin 
from pathlib import Path, PureWindowsPath

class FPDF(FPDF, HTMLMixin):
    pass

pdf = FPDF()
pdf.add_page()
pdf.add_font('DejaVuSans', '',str(Path(fontfolder/'DejaVuSans.ttf')),uni=True)
pdf.set_font('DejaVuSans', size=14)

I get the following error:


AttributeError Traceback (most recent call last)
~\AppData\Local\Temp\1\ipykernel_33804\142261010.py in
11 pdf = FPDF()
12 pdf.add_page()
---> 13 pdf.add_font('DejaVuSans', '', str(Path(fontfolder/'DejaVuSans.ttf')),uni=True)
14 pdf.set_font('DejaVuSans', size=14)

c:\Users\gmme\Anaconda3\lib\site-packages\fpdf\fpdf.py in add_font(self, family, style, fname, uni)
1914 "i": len(self.fonts) + 1,
1915 "type": "TTF",
-> 1916 "name": re.sub("[ ()]", "", font["name"].getBestFullName()),
1917 "desc": desc,
1918 "up": round(font["post"].underlinePosition * scale),

AttributeError: 'table__n_a_m_e' object has no attribute 'getBestFullName'

Thanks.

@TchilDill TchilDill added the bug label Sep 9, 2022
@Lucas-C Lucas-C added font bug and removed bug labels Sep 9, 2022
@Lucas-C
Copy link
Member

Lucas-C commented Sep 9, 2022

Hi and thanks for reporting this @guillaume-dotcom

I'm sorry but I couldn't reproduce your issue, neither under Linux (Ubuntu) nor Windows (10)

Note also that your sample code is not fully correct.
I defined fontfolder = Path('test/fonts/') in order for it to work when executed at the root of this repository:

from fpdf import FPDF
from pathlib import Path

fontfolder = Path('test/fonts/')

pdf = FPDF()
pdf.add_page()
pdf.add_font('DejaVuSans', '', fontfolder/'DejaVuSans.ttf')
pdf.set_font('DejaVuSans', size=14)

@TchilDill
Copy link
Author

Hi @Lucas-C and thanks for the swift reply,

Point taken. Apologies for leaving one the fontfolder definition.

It is odd. Changing the interpreter to a previous version of fpdf2 worked just fine then. I wonder what could trigger this message.

I also tried to reproduce your code (exactly) and get the same error.


AttributeError Traceback (most recent call last)
~\AppData\Local\Temp\1\ipykernel_26752\1024916228.py in
6 pdf = FPDF()
7 pdf.add_page()
----> 8 pdf.add_font('DejaVuSans', '', fontfolder/'DejaVuSans.ttf')
9 pdf.set_font('DejaVuSans', size=14)

c:\Users\gmme\Anaconda3\lib\site-packages\fpdf\fpdf.py in add_font(self, family, style, fname, uni)
1914 "i": len(self.fonts) + 1,
1915 "type": "TTF",
-> 1916 "name": re.sub("[ ()]", "", font["name"].getBestFullName()),
1917 "desc": desc,
1918 "up": round(font["post"].underlinePosition * scale),

AttributeError: 'table__n_a_m_e' object has no attribute 'getBestFullName'

@gmischler
Copy link
Collaborator

gmischler commented Sep 9, 2022

You don't seem to have a recent enough version of fonttools installed.
The name of the "missing" method getBestFullName() was only added a few months ago.
The version you have was probably installed as a dependency of some other package before that.

It is odd. Changing the interpreter to a previous version of fpdf2 worked just fine then. I wonder what could trigger this message.

That's easy to explain, since fpdf2 only started to use fonttools with release 2.5.7 published just yesterday.

@TchilDill
Copy link
Author

TchilDill commented Sep 12, 2022

Hey @gmischler,

That did the trick. thanks. :)

@Lucas-C, shouldn't we have an updated fonttools requirement in setup.py to ensure this is not a recurring issue?

@gmischler
Copy link
Collaborator

shouldn't we have an updated fonttools requirement in setup.py to ensure this is not a recurring issue?

Yes, setting a minimum version might be useful for several dependencies. We've had similar questions about Pillow in the past.

Some people even reccommend to pin all dependencies to a fixed version that has been checked and tested, in order to prevent supply chain attacks.

@Lucas-C
Copy link
Member

Lucas-C commented Sep 12, 2022

Yes, setting a minimum version might be useful for several dependencies. We've had similar questions about Pillow in the past.

I agree! Would you like to submit a PR introducing a minimum version for fonttools @guillaume-dotcom?

@TchilDill
Copy link
Author

Yes @Lucas-C. I will submit a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants