Call pdftopng in python instead of subprocess

Using the poppler backed failed for me because I was running it from an un-activated venv and pdftopng was not in the PATH.

My original workaround was even shorter - I imported the convert function directly into the PopplerBackend class. But this import will fail on initialization if pdftopng is not installed and will prevent using the ghostscript backend, so I converted it to late import.
pull/269/head
orent 2021-09-30 14:10:30 +03:00 committed by GitHub
parent 644bbe7c6d
commit 96b9f04e38
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 2 additions and 14 deletions

View File

@ -6,17 +6,5 @@ import subprocess
class PopplerBackend(object): class PopplerBackend(object):
def convert(self, pdf_path, png_path): def convert(self, pdf_path, png_path):
pdftopng_executable = shutil.which("pdftopng") from pdftopng.pdftopng import convert
if pdftopng_executable is None: convert(pdf_path, png_path)
raise OSError(
"pdftopng is not installed. You can install it using the 'pip install pdftopng' command."
)
pdftopng_command = [pdftopng_executable, pdf_path, png_path]
try:
subprocess.check_output(
" ".join(pdftopng_command), stderr=subprocess.STDOUT, shell=True
)
except subprocess.CalledProcessError as e:
raise ValueError(e.output)