I want to create an exe that can be deployed onto other computers. The program needs to be able to read pdf’s and turn them into images, but I don’t want other users to have to download dependencies.
My understanding is that py2image and wand both require external dependencies that, if you convert to a exe, other users would also need to download the dependencies themselves.
Are there other options available/ workarounds ?
Advertisement
Answer
Actually, it took me a while to handle this, but I think it worth it. You need to do all steps carefully to make it work.
- Install pdf2image with
pip install pdf2image. - Get poppler windows binaries.
- Create a new directory like
myproject. - Create a script
converter.pyinsidemyprojectand add below code. - Create another directory inside
myprojectand name itpoppler. - Copy all files in the binary folder of downloaded poppler into
popplerdirectory. Try to testpdfimages.exeif it is working. - Use
pyinstaller converter.py -F --add-data "./poppler/*;./poppler" --noupx - Your executable is now ready. Run it like
converter.exe myfile.pdf. Results would be created inside theoutputdirectory next to the executable. - Now your standalone PDF2IMAGE converter app is ready!
converter.py:
import sys
import os
from pdf2image import convert_from_path
def current_path(dir_path):
if hasattr(sys, '_MEIPASS'):
return os.path.join(sys._MEIPASS, dir_path)
return os.path.join(".", dir_path)
if __name__ == "__main__":
if len(sys.argv) < 2:
print("PASS your PDF file: "converter.exe myfile.pdf"")
input()
sys.exit(0)
os.environ["PATH"] += os.pathsep +
os.pathsep.join([current_path("poppler")])
if not os.path.isdir("./output"):
os.makedirs("output")
images = convert_from_path(sys.argv[-1], 500)
for image, i in zip(images, (range(len(images)))):
image.save('./output/out{}.png'.format(i), 'PNG')
PS: If you like it, you can add a GUI and add more settings for pdf2images.