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.py
insidemyproject
and add below code. - Create another directory inside
myproject
and name itpoppler
. - Copy all files in the binary folder of downloaded poppler into
poppler
directory. Try to testpdfimages.exe
if 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 theoutput
directory next to the executable. - Now your standalone PDF2IMAGE converter app is ready!
converter.py
:
JavaScript
x
25
25
1
import sys
2
import os
3
from pdf2image import convert_from_path
4
5
6
def current_path(dir_path):
7
if hasattr(sys, '_MEIPASS'):
8
return os.path.join(sys._MEIPASS, dir_path)
9
return os.path.join(".", dir_path)
10
11
12
if __name__ == "__main__":
13
if len(sys.argv) < 2:
14
print("PASS your PDF file: "converter.exe myfile.pdf"")
15
input()
16
sys.exit(0)
17
os.environ["PATH"] += os.pathsep +
18
os.pathsep.join([current_path("poppler")])
19
20
if not os.path.isdir("./output"):
21
os.makedirs("output")
22
images = convert_from_path(sys.argv[-1], 500)
23
for image, i in zip(images, (range(len(images)))):
24
image.save('./output/out{}.png'.format(i), 'PNG')
25
PS: If you like it, you can add a GUI and add more settings for pdf2images
.