jorisschellekens / borb

borb is a library for reading, creating and manipulating PDF files in python.
https://borbpdf.com/
Other
3.4k stars 147 forks source link

Problem when creaton exe "AssertionError: AFM directory not found" #196

Closed vo1s closed 9 months ago

vo1s commented 9 months ago

I am creating a console application using this library and get "AssertionError: AFM directory not found" when launching the exe created py PyInstaller. I tried to include afm directory in --paths but it didnt help. What may cause this error?

jorisschellekens commented 9 months ago

When you're using the default fonts (e.g. Helvetica), borb needs to read an afm (Adobe font metrics) file. This file tells borb how wide/tall each character is.

The AFM files for the standard fonts are included in borb.

You can see that the manifest file includes this directory.

I'm afraid I can't help you. This seems like an issue with how borb is packaged by pyinstaller, rather than an issue with borb itself.

Kind regards, Joris Schellekens

vo1s commented 9 months ago

I dont use standarts fonts. I use .ttf font

jorisschellekens commented 9 months ago
  1. Unless you share your code, I have no idea what you are doing, and which fonts you are using.
  2. Regardless of your font choice, my initial guess is that this is a packaging issue.
chrisbeardy commented 7 months ago

this is a packaging error, however it actually only seems to have appeared between borb version 2.1.17 -> 2.1.22 as I just hit the same issue, I'm not using any custom fonts, just whatever default borb picks. So I expect something has changes here in borb that means pyinstaller can't find the font somewhere. It comes from here:

line 439 of https://github.com/jorisschellekens/borb/blob/master/borb/pdf/canvas/font/simple_font/font_type_1.py

            afm_directory: pathlib.Path = pathlib.Path(__file__).parent / "afm"
            assert afm_directory.exists(), "AFM directory not found"

full call chain from pyinstaller

File "borb\pdf\__init__.py", line 59, in <module>
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "PyInstaller\loader\pyimod02_importers.py", line 419, in exec_module
  File "borb\pdf\canvas\layout\annotation\free_text_annotation.py", line 26, in <module>
  File "borb\pdf\canvas\layout\annotation\free_text_annotation.py", line 44, in FreeTextAnnotation
  File "borb\pdf\canvas\font\simple_font\font_type_1.py", line 439, in __init__

The AFM directory can't be found by pyinstaller so probably now needs to be given as a hidden import or as an extra path, although not sure where the original path of the AFM files is? @jorisschellekens any guidance on where the original AFM file is that borb creates? i.e. what folder/file its looking for in line 439 of font_type_1?

NOt sure whats changed from version 2.1.17 to 2.1.22 in terms of this path handling but not a borb issue, however would be nice to have a fix / workaround for this to know what to do in pyinstaller

chrisbeardy commented 5 months ago

update for anyone that gets this problem in the future: you need to add the font to added datas in pyinstaller, e.g.:

datas = [
    (
        'venv\\Lib\\site-packages\\borb\\pdf\\canvas\\font\\simple_font\\afm',
        'borb\\pdf\\canvas\\font\\simple_font\\afm'
    ),
]