简体   繁体   中英

How do I include poppler to pyinstaller generated exe when using pdf2image?

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?

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM