From 4dd1e7fb15f768adf3c872197885cd93d915483d Mon Sep 17 00:00:00 2001 From: Vinayak Mehta Date: Sun, 4 Jul 2021 18:52:38 +0530 Subject: [PATCH] Call pdftopng in subprocess --- camelot/backends/ghostscript_backend.py | 4 +-- camelot/backends/poppler_backend.py | 11 ++++-- camelot/parsers/lattice.py | 1 - tests/test_common.py | 48 +++++++++---------------- 4 files changed, 27 insertions(+), 37 deletions(-) diff --git a/camelot/backends/ghostscript_backend.py b/camelot/backends/ghostscript_backend.py index cba3e83..e64510d 100644 --- a/camelot/backends/ghostscript_backend.py +++ b/camelot/backends/ghostscript_backend.py @@ -35,7 +35,7 @@ class GhostscriptBackend(object): "here: https://camelot-py.readthedocs.io/en/master/user/install-deps.html" ) - gs_args = [ + gs_command = [ "gs", "-q", "-sDEVICE=png16m", @@ -44,4 +44,4 @@ class GhostscriptBackend(object): f"-r{resolution}", pdf_path, ] - ghostscript.Ghostscript(*gs_args) + ghostscript.Ghostscript(*gs_command) diff --git a/camelot/backends/poppler_backend.py b/camelot/backends/poppler_backend.py index c806098..76c4667 100644 --- a/camelot/backends/poppler_backend.py +++ b/camelot/backends/poppler_backend.py @@ -1,8 +1,15 @@ # -*- coding: utf-8 -*- -from pdftopng import pdftopng +import subprocess class PopplerBackend(object): def convert(self, pdf_path, png_path): - pdftopng.convert(pdf_path, png_path) + pdftopng_command = ["pdftopng", 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) diff --git a/camelot/parsers/lattice.py b/camelot/parsers/lattice.py index 937e867..02ef794 100644 --- a/camelot/parsers/lattice.py +++ b/camelot/parsers/lattice.py @@ -6,7 +6,6 @@ import copy import locale import logging import warnings -import subprocess import numpy as np import pandas as pd diff --git a/tests/test_common.py b/tests/test_common.py index ddb8de2..88055d6 100644 --- a/tests/test_common.py +++ b/tests/test_common.py @@ -55,19 +55,17 @@ def test_repr_poppler(): tables = camelot.read_pdf(filename) assert repr(tables) == "" assert repr(tables[0]) == "" - assert ( - repr(tables[0].cells[0][0]) == "" - ) + assert repr(tables[0].cells[0][0]) == "" def test_repr_ghostscript(): filename = os.path.join(testdir, "foo.pdf") - tables = camelot.read_pdf(filename, backend=ImageConversionBackend(backend="ghostscript")) + tables = camelot.read_pdf( + filename, backend=ImageConversionBackend(backend="ghostscript") + ) assert repr(tables) == "" assert repr(tables[0]) == "
" - assert ( - repr(tables[0].cells[0][0]) == "" - ) + assert repr(tables[0].cells[0][0]) == "" def test_url_poppler(): @@ -75,19 +73,17 @@ def test_url_poppler(): tables = camelot.read_pdf(url) assert repr(tables) == "" assert repr(tables[0]) == "
" - assert ( - repr(tables[0].cells[0][0]) == "" - ) + assert repr(tables[0].cells[0][0]) == "" def test_url_ghostscript(): url = "https://camelot-py.readthedocs.io/en/master/_static/pdf/foo.pdf" - tables = camelot.read_pdf(url, backend=ImageConversionBackend(backend="ghostscript")) + tables = camelot.read_pdf( + url, backend=ImageConversionBackend(backend="ghostscript") + ) assert repr(tables) == "" assert repr(tables[0]) == "
" - assert ( - repr(tables[0].cells[0][0]) == "" - ) + assert repr(tables[0].cells[0][0]) == "" def test_pages_poppler(): @@ -95,23 +91,17 @@ def test_pages_poppler(): tables = camelot.read_pdf(url) assert repr(tables) == "" assert repr(tables[0]) == "
" - assert ( - repr(tables[0].cells[0][0]) == "" - ) + assert repr(tables[0].cells[0][0]) == "" tables = camelot.read_pdf(url, pages="1-end") assert repr(tables) == "" assert repr(tables[0]) == "
" - assert ( - repr(tables[0].cells[0][0]) == "" - ) + assert repr(tables[0].cells[0][0]) == "" tables = camelot.read_pdf(url, pages="all") assert repr(tables) == "" assert repr(tables[0]) == "
" - assert ( - repr(tables[0].cells[0][0]) == "" - ) + assert repr(tables[0].cells[0][0]) == "" def test_pages_ghostscript(): @@ -119,23 +109,17 @@ def test_pages_ghostscript(): tables = camelot.read_pdf(url) assert repr(tables) == "" assert repr(tables[0]) == "
" - assert ( - repr(tables[0].cells[0][0]) == "" - ) + assert repr(tables[0].cells[0][0]) == "" tables = camelot.read_pdf(url, pages="1-end") assert repr(tables) == "" assert repr(tables[0]) == "
" - assert ( - repr(tables[0].cells[0][0]) == "" - ) + assert repr(tables[0].cells[0][0]) == "" tables = camelot.read_pdf(url, pages="all") assert repr(tables) == "" assert repr(tables[0]) == "
" - assert ( - repr(tables[0].cells[0][0]) == "" - ) + assert repr(tables[0].cells[0][0]) == "" def test_table_order():