'PDF to image with python and poppler in a single file exe

I need an exe in a compact form (i.e. --onefile and --noconsole commands in pyinstaller) that shall be able to convert pdf pages into png.

I have followed suggestions from many answers here on SO, which are great but they are not enough when both of pyinstaller commands are added. If I delete one of the two, it works like a charm, otherwise it gets stuck with the typical:

pdf2image.exceptions.PDFInfoNotInstalledError: Unable to get page count. Is poppler installed and in PATH?

Given a pdf path in filename, I run the following commands:

from pathlib import Path
from pdf2image import convert_from_path

filename = r'my\path\to\file.pdf'

poppler_path = Path(__file__).joinpath('poppler')  # correct path to poppler from current file
pages = convert_from_path(filename, poppler_path=poppler_path)
for ii, page in enumerate(pages):
    page.save(f'page{ii + 1:01}.png', 'PNG')

The __file__ strategy allows me to move into the sys._MEIPASS directory, indeed if I try to print it I get a path like

"C:\Users\xxxxx\AppData\Local\Temp\_MEI157042\poppler"

and I am able to open the directory to find all of the poppler files, but the application is somehow unable to use them.

Another curious thing is that if I now build another self-contained exe with a hard-routed path to the above poppler folder (while the first exe is still open), it manages to retrieve the files and build the images. So now my question is: is there any solution to this? And if not, may there be a workaround with the main application creating the poppler folder and another sub-application exploiting it?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source