Make ghostscript default backend and add support for string keywords

This commit is contained in:
Vinayak Mehta
2021-07-11 17:25:56 +05:30
parent f43235934b
commit 8abe02528b
8 changed files with 135 additions and 92 deletions
+2 -2
View File
@@ -29,8 +29,8 @@ class GhostscriptBackend(object):
def convert(self, pdf_path, png_path, resolution=300):
if not self.installed():
raise OSError(
"Ghostscript is not installed. Please install it using the instructions"
"here: https://camelot-py.readthedocs.io/en/master/user/install-deps.html"
"Ghostscript is not installed. You can install it using the instructions"
" here: https://camelot-py.readthedocs.io/en/master/user/install-deps.html"
)
import ghostscript
+5 -5
View File
@@ -3,21 +3,21 @@
from .poppler_backend import PopplerBackend
from .ghostscript_backend import GhostscriptBackend
backends = {"poppler": PopplerBackend, "ghostscript": GhostscriptBackend}
BACKENDS = {"poppler": PopplerBackend, "ghostscript": GhostscriptBackend}
class ImageConversionBackend(object):
def __init__(self, backend="poppler", use_fallback=True):
if backend not in backends.keys():
if backend not in BACKENDS.keys():
raise ValueError(f"Image conversion backend '{backend}' not supported")
self.backend = backend
self.use_fallback = use_fallback
self.fallbacks = list(filter(lambda x: x != backend, backends.keys()))
self.fallbacks = list(filter(lambda x: x != backend, BACKENDS.keys()))
def convert(self, pdf_path, png_path):
try:
converter = backends[self.backend]()
converter = BACKENDS[self.backend]()
converter.convert(pdf_path, png_path)
except Exception as e:
import sys
@@ -25,7 +25,7 @@ class ImageConversionBackend(object):
if self.use_fallback:
for fallback in self.fallbacks:
try:
converter = backends[fallback]()
converter = BACKENDS[fallback]()
converter.convert(pdf_path, png_path)
except Exception as e:
raise type(e)(
+1 -1
View File
@@ -9,7 +9,7 @@ class PopplerBackend(object):
pdftopng_executable = shutil.which("pdftopng")
if pdftopng_executable is None:
raise OSError(
"pdftopng is not installed. Please install it using the `pip install pdftopng` command."
"pdftopng is not installed. You can install it using the 'pip install pdftopng' command."
)
pdftopng_command = [pdftopng_executable, pdf_path, png_path]