Skip to content
Advertisement

Converting PDF to Image without non-python dependencies

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.

  1. Install pdf2image with pip install pdf2image.
  2. Get poppler windows binaries.
  3. Create a new directory like myproject.
  4. Create a script converter.py inside myproject and add below code.
  5. Create another directory inside myproject and name it poppler.
  6. Copy all files in the binary folder of downloaded poppler into poppler directory. Try to test pdfimages.exe if it is working.
  7. Use pyinstaller converter.py -F --add-data "./poppler/*;./poppler" --noupx
  8. Your executable is now ready. Run it like converter.exe myfile.pdf. Results would be created inside the output directory next to the executable.
  9. 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.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement