Search code examples
python-2.7pyinstallerfpdf

Can't run app exe on other machines, python2.7, fpdf, pyinstaller


I have an issue with running exe application from pyinstaller on other machines. It is looking for path on PC, where I built application:

console output

I use and added font in pyPDF in following way:

from fpdf import FPDF

pwd = os.path.realpath(os.path.dirname(sys.argv[0])) + "\\font\\DejaVuSansCondensed.ttf"

pdf = FPDF(orientation = 'L', unit = 'mm', format='A4')
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', '', pwd, uni=True)
pdf.set_font('DejaVu', '', 18)
#then I use pdf.write() to write data
#save and close pdf file
pdf.output('C:\\Users\\' + getpass.getuser() + '\\Documents\\pdf_file' + time_stamp + '.pdf', 'F')

I tried to build it in following ways:

pyinstaller app.py
pyinstaller --onefile app.py

There is no issue on machine, where I build code. I suppose there is something in output method from fpdf or settings of pyinstaller, am I right?

I have to create pdf with unicode characters. I use latest versions of fpdf and pyinstaller modules.

I will be thankful for any help.

Thank you in advance,


Solution

  • There is no issue when you build it on your machine since you have DejaVuSansCondensed.ttf where python expects it to be. But when you compile with PyInstaller and run it on another system, it is looking in the same spot (which may not exist on other systems).

    What I would suggest is to copy that ttf file to your current working directory and update the line font line to (or something similar that fits your code):

    pwd = "DejaVuSansCondensed.ttf"
    

    You also have to make sure that the .exe has access to that file, independent of system (relative to the exe). So you have to copy the ttf file to the same directory as your exe so that when you run it on another computer, your code will look in the directory that the exe is in and find the ttf file.