3

I am trying to add a specific font to my fpdf configuration using .add_font(). I used to have all my scripts in a folder called "david" since I rewrote the Code I am working on. Now I migrated it into the parent directory.

Here is the unexpected behaviour: In a function I call

# returns /home/path_to_dir/fonts/DejaVuSans.ttf (as expected)
print(os.path.join(os.getcwd(), "fonts", "DejaVuSans.ttf"))
pdf.add_font(
    "DejaVu",
    "",
    # returns /home/path_to_dir/david/../fonts/DejaVuSans.ttf (old path)
    os.path.join(os.getcwd(), "fonts", "DejaVuSans.ttf"),
    uni=True,
)

I deleted all pycache files and if I add an empty directory called "david" everything works again since the latter path can be resolved.

reproducable minimal example: - To get fpdf working: - pip install fpdf - a fonts directory with a font in it (ttf format)

A file with this code in it

import fpdf
import os


def reproducable_example():
    text = ['This is some test text', 'This is some more test text']
    pdf = fpdf.FPDF()
    print(os.path.join(os.getcwd(), '../fonts', 'DejaVuSans.ttf'))
    pdf.add_font(
        "DejaVu",
        "",
        os.path.join(os.getcwd(), "../fonts", "DejaVuSans.ttf"),
        uni=True,
    )
    pdf.set_font('DejaVu', "", 12)
    pdf.add_page()
    for string in text:
        pdf.multi_cell(0, 10, txt=string)
    pdf.output(os.path.join(os.getcwd(), 'text.pdf'))


reproducable_example()

It would obviously be very strange if the second call to os.getcwd() would include a folder with the name 'david'. Therefore you probably have to put the code in a nested folder with the name 'david' first then run it, and then put it into the parent folder, delete the 'david' directory and run it again.

Also I created a .workspace folder in which i have my launch.json file. In this file I changed the cwd key from ${workspaceRoot} to ${workspaceRoot}/david because of Import issues when using the vscode debugger. I changed this back when I migrated the files to the parent directory.

This is why you can run os.getcwd() in the child folder and then step out of manually

Nowhere in my whole workspace did I declare the wrong filepath but it still gets used by the .add_font() method even when I hardcode the correct path into it

Solution: When I ran the script, a .pkl file was created automatically. You need to delete this file when you change the directory of your script or it will use the stored paths

David Haase
  • 179
  • 1
  • 8
  • Your example program works absolutely fine for me (executed as `python example.py` with `fonts/` next to it). Python 3.9 on macOS. – AKX Oct 15 '21 at 07:51
  • Also, the error is not raised when I add the font in ```pdf.add_font()``` but when I want to output the pdf in ```pdf.output()``` – David Haase Oct 15 '21 at 07:59
  • 1
    Since I get this path ```'/home/david/MPI/annual-stats/david/../fonts/DejaVuSans.ttf'```returned even when I run the example script out of a completly different directory the problem seems to be that this path is stored somewhere globally by fpdf which is also strange because I installed fpdf in a virtual environment – David Haase Oct 15 '21 at 08:07
  • As a workaround I would just store the path outside the `add_font` function call, where it's correct, and use the variable instead. – Sandsten Oct 15 '21 at 08:22
  • @Sandsten this does not work either. Even when I hardcode the filepath directly into ```.add_font()``` it somehow still uses the false, old path – David Haase Oct 15 '21 at 08:29
  • In the documentation it says that if the font isn't found, it will search in `FPDF_FONTPATH` or `SYSTEM_TTFONTS`. Maybe that has something to do with it. – Sandsten Oct 15 '21 at 08:37
  • @Sandsten thought of that aswell but I specified an absolute path to the font, went into the font directory ran ```pwd```and copied the path so there is no reason why the file shouldn't be found. – David Haase Oct 15 '21 at 08:42
  • Have you tried this? https://stackoverflow.com/a/57115989/5903504 – Sandsten Oct 15 '21 at 08:43
  • You don't need `os.getcwd()` to find the current directory: just specify a relative file name. See also [What exactly is current working directory?](https://stackoverflow.com/questions/45591428/what-exactly-is-current-working-directory/66860904) – tripleee Oct 15 '21 at 08:44
  • I found out something. When I put the font in the parent directory it works. – David Haase Oct 15 '21 at 08:46
  • @Sandsten Yes! This fits with my observings since i only copied the Font file itself without its .pkl – David Haase Oct 15 '21 at 08:47
  • @tripleee You are right. But that was not the problem – David Haase Oct 15 '21 at 08:48
  • That's why I posted a comment, not an answer. – tripleee Oct 15 '21 at 09:01
  • @tripleee Thank you, wasn't meant to be mean on my side. – David Haase Oct 15 '21 at 09:02
  • I would try a few things. Use an absolute path to your ttf file. Try (within your code) to open that file. Dump all environment variables to see if there's something there that might be interfering. In particular you could test to see if any environment variables contain the substring 'david' –  Oct 15 '21 at 09:56

0 Answers0