3

I made a simple script that converts pdfs inside the current directory to images. I want to make it into a standalone .exe file so that someone who doesn't have python installed on his PC can use it.

The problem is that pyinstaller fails to include poppler to the exe file so pdf2image does not run correctly and the built exe fails. Here's the error message:

pdf2image.exceptions.PDFInfoNotInstalledError: Unable to get page count. Is poppler installed and in PATH? [32024] Failed to execute script bulk_pdf2img

I'm currently working on conda environment that has pyinstaller and pdf2image and poppler installed from conda install command. It works just fine when I execute the python script from the prompt but when the script is converted to exe, it raises the above error.

I tried the following approaches:

1. Add --add-data option

I tried to add poppler data by doing this.

$ pyinstaller --onedir --add-data="C:/Users/myusername/anaconda3/pkgs/poppler-0.89.0-h20e2fe3_4/Library/include/poppler/*;./poppler" bulk_pdf2img.py

Doesn't work.

2. Add additional-hooks-dir option

I added projectdirectory/hooks/hook-pdf2image.py that has

from PyInstaller.utils.hooks import collect_all

data, binaries, hiddenimports = collect_all('pdf2image')

inside and ran

$ pyinstaller --onefile --additional-hooks-dir=hooks bulk_pdf2img.py

Also doesn't work.

I googled almost every Stackoverflow question that is apparently having the exact same problem as I am but couldn't find any valid solution. What should I do now?

user8491363
  • 2,924
  • 5
  • 19
  • 28
  • https://www.reddit.com/r/learnpython/comments/lp1hi3/how_do_i_include_nonpython_dependencies_to/ I finally made it work though the solution was a bit ugly. – user8491363 Feb 23 '21 at 16:35

1 Answers1

3

From the docs

You will then have to add the bin/ folder to PATH or use poppler_path = r"C:\path\to\poppler-xx\bin" as an argument in convert_from_path.

Now, if you are using the --onefile option of pyinstaller it will unpack all the files into a temporary folder during the execution, you might want to look into this answer and the related post, to get the path right.

You can do any of the following

  • Execute this at the beginning to add the bin folder of poppler to PATH.

    os.environ["PATH"]+=os.pathsep+os.path.join('path/to/poppler','bin')
    
  • pdf2image.convert_from_path('path/to/pdf',poppler_path=r"path\to\poppler\bin") 
    
astqx
  • 2,058
  • 1
  • 10
  • 21
  • Thanks! Yeah the PATH was a big problem for me because pdfinfo.exe was NOT able to find related .dll files when I set the argument `poppler_path=r"C:\path\to\poppler-xx\bin"`. It doesn't work if you only specify pdfinfo.exe's bin/ location. – user8491363 Feb 23 '21 at 16:36
  • I kind-of resolved it with an ugly way of manually copy-pasting all .dll files from `C:\Users\my_username\anaconda3\envs\my_envname\Library\bin` but in the future, I'll try your way of setting PATH env variable in the runtime. – user8491363 Feb 23 '21 at 16:38