FPDF referencing old path

123 Views Asked by At

I wrote a database form using tkinter and I'm using FPDF to generate labels. I get an errno2 when trying to save the label I just made. The path it is reference is on my lab PC but not the PC I'm running the script from. Knowing this I use os.getcwd() and use child directories to make sure the script is portable.

Here is the function that is returning an error:

skid_number = config.get('DEFAULT','skid_number')

pdf = FPDF('L', 'mm', (101.6,152.4))
pdf.add_page()

pdf.add_font('free3of9', '',f'{os.getcwd()}\\free3of9.ttf',uni=True)

pdf.set_font("Arial", size=45,style='B')
pdf.cell(131,10, txt='AUD-PLT',align='C')

pdf.ln(h=16.5)
pdf.set_font("free3of9", size=55,style='')
pdf.cell(131,10, txt='*AUD-PLT*',align='C')

pdf.ln(h=16.5)
pdf.set_font("Arial", size=45,style='B')
pdf.cell(131,10, txt=skid_number,align='C')

pdf.ln(h=16.5)
pdf.set_font("free3of9", size=55,style='')
pdf.cell(131,10, txt=f'*{skid_number}*',align='C')

pdf.ln(h=16.5)
pdf.set_font("Arial", size=45,style='B')
pdf.cell(131,5, txt=date.today().strftime('%b-%d-%y'),align='C')

pdf.output(name='skid_label.pdf', dest=os.getcwd())

and the traceback:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Program Files\Python311\Lib\tkinter\__init__.py", line 1948, in __call__
    return self.func(*args)
           ^^^^^^^^^^^^^^^^
  File "C:\Users\onlineuser\Desktop\gui.py", line 474, in new_pallet
    self.print_skid_label()
  File "C:\Users\onlineuser\Desktop\gui.py", line 398, in print_skid_label
    pdf.output(name='skid_label.pdf', dest=os.getcwd())
  File "C:\Program Files\Python311\Lib\site-packages\fpdf\fpdf.py", line 1065, in output
    self.close()
  File "C:\Program Files\Python311\Lib\site-packages\fpdf\fpdf.py", line 246, in close
    self._enddoc()
  File "C:\Program Files\Python311\Lib\site-packages\fpdf\fpdf.py", line 1637, in _enddoc
    self._putresources()
  File "C:\Program Files\Python311\Lib\site-packages\fpdf\fpdf.py", line 1584, in _    putresources
    self._putfonts()
  File "C:\Program Files\Python311\Lib\site-packages\fpdf\fpdf.py", line 1288, in _putfonts
    ttfontstream = ttf.makeSubset(font['ttffile'], subset)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python311\Lib\site-packages\fpdf\ttfonts.py", line 459, in makeSubset
    self.fh = open(file ,'rb')
              ^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory:     'C:\\Users\\DavidSonnen\\Documents\\Projects\\Silver Inventory Python\\Lib\\site-        packages\\fpdf\\fonts\\free3of9.ttf'

The "No such file or directory" is reference where I built the form on my lab PC. and the error gets thrown on the pdf.output line

I suspect the problem is coming from the "free3of9" font on the add_font line. So I have tried hard coding the path to the ttf. I have also peeked in the other files in the traceback but I didn't see anything that I thought was wrong.

0

There are 0 best solutions below