Remove all direct use of ghost script, add PDFTOPPM_PATH to allow configurable pdftoppm path, raise ConvertError on coverterror and not UnknownFile exception blindly
This commit is contained in:
@@ -187,6 +187,13 @@ Default: ``/usr/bin/libreoffice``
|
|||||||
Path to the libreoffice binary used to call LibreOffice for office document conversion.
|
Path to the libreoffice binary used to call LibreOffice for office document conversion.
|
||||||
|
|
||||||
|
|
||||||
|
**CONVERTER_PDFTOPPM_PATH**
|
||||||
|
|
||||||
|
Default: ``/usr/bin/pdftoppm``
|
||||||
|
|
||||||
|
Path to the Popple program pdftoppm.
|
||||||
|
|
||||||
|
|
||||||
Storage
|
Storage
|
||||||
=======
|
=======
|
||||||
|
|
||||||
|
|||||||
@@ -1,33 +1,30 @@
|
|||||||
from __future__ import absolute_import
|
from __future__ import absolute_import
|
||||||
|
|
||||||
|
import io
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import tempfile
|
import tempfile
|
||||||
|
|
||||||
try:
|
|
||||||
from cStringIO import StringIO
|
|
||||||
except ImportError:
|
|
||||||
from StringIO import StringIO
|
|
||||||
|
|
||||||
import slate
|
import slate
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
import sh
|
import sh
|
||||||
|
|
||||||
try:
|
|
||||||
pdftoppm = sh.Command('/usr/bin/pdftoppm')
|
|
||||||
except sh.CommandNotFound:
|
|
||||||
pdftoppm = None
|
|
||||||
else:
|
|
||||||
pdftoppm = pdftoppm.bake('-png')
|
|
||||||
|
|
||||||
from common.utils import fs_cleanup
|
from common.utils import fs_cleanup
|
||||||
from mimetype.api import get_mimetype
|
from mimetype.api import get_mimetype
|
||||||
|
|
||||||
from . import ConverterBase
|
from . import ConverterBase
|
||||||
from ..exceptions import UnknownFileFormat
|
from ..exceptions import ConvertError, UnknownFileFormat
|
||||||
from ..literals import (DEFAULT_FILE_FORMAT, DEFAULT_PAGE_NUMBER,
|
from ..literals import (DEFAULT_FILE_FORMAT, DEFAULT_PAGE_NUMBER,
|
||||||
TRANSFORMATION_RESIZE, TRANSFORMATION_ROTATE,
|
TRANSFORMATION_RESIZE, TRANSFORMATION_ROTATE,
|
||||||
TRANSFORMATION_ZOOM)
|
TRANSFORMATION_ZOOM)
|
||||||
|
from ..settings import PDFTOPPM_PATH
|
||||||
|
|
||||||
|
try:
|
||||||
|
pdftoppm = sh.Command(PDFTOPPM_PATH)
|
||||||
|
except sh.CommandNotFound:
|
||||||
|
pdftoppm = None
|
||||||
|
else:
|
||||||
|
pdftoppm = pdftoppm.bake('-png')
|
||||||
|
|
||||||
Image.init()
|
Image.init()
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@@ -72,7 +69,7 @@ class Python(ConverterBase):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
if mimetype == 'application/pdf' and pdftoppm:
|
if mimetype == 'application/pdf' and pdftoppm:
|
||||||
image_buffer = StringIO()
|
image_buffer = io.BytesIO()
|
||||||
pdftoppm(input_filepath, f=page, l=page, _out=image_buffer)
|
pdftoppm(input_filepath, f=page, l=page, _out=image_buffer)
|
||||||
image_buffer.seek(0)
|
image_buffer.seek(0)
|
||||||
im = Image.open(image_buffer)
|
im = Image.open(image_buffer)
|
||||||
@@ -81,6 +78,8 @@ class Python(ConverterBase):
|
|||||||
except Exception as exception:
|
except Exception as exception:
|
||||||
logger.error('Error converting image; %s', exception)
|
logger.error('Error converting image; %s', exception)
|
||||||
# Python Imaging Library doesn't recognize it as an image
|
# Python Imaging Library doesn't recognize it as an image
|
||||||
|
raise ConvertError
|
||||||
|
except IOError: # cannot identify image file
|
||||||
raise UnknownFileFormat
|
raise UnknownFileFormat
|
||||||
finally:
|
finally:
|
||||||
if tmpfile:
|
if tmpfile:
|
||||||
|
|||||||
@@ -14,5 +14,6 @@ register_settings(
|
|||||||
{'name': u'GM_SETTINGS', 'global_name': u'CONVERTER_GM_SETTINGS', 'default': u''},
|
{'name': u'GM_SETTINGS', 'global_name': u'CONVERTER_GM_SETTINGS', 'default': u''},
|
||||||
{'name': u'GRAPHICS_BACKEND', 'global_name': u'CONVERTER_GRAPHICS_BACKEND', 'default': u'converter.backends.python.Python', 'description': _(u'Graphics conversion backend to use. Options are: converter.backends.imagemagick.ImageMagick, converter.backends.graphicsmagick.GraphicsMagick and converter.backends.python.Python')},
|
{'name': u'GRAPHICS_BACKEND', 'global_name': u'CONVERTER_GRAPHICS_BACKEND', 'default': u'converter.backends.python.Python', 'description': _(u'Graphics conversion backend to use. Options are: converter.backends.imagemagick.ImageMagick, converter.backends.graphicsmagick.GraphicsMagick and converter.backends.python.Python')},
|
||||||
{'name': u'LIBREOFFICE_PATH', 'global_name': u'CONVERTER_LIBREOFFICE_PATH', 'default': u'/usr/bin/libreoffice', 'exists': True, 'description': _(u'Path to the libreoffice program.')},
|
{'name': u'LIBREOFFICE_PATH', 'global_name': u'CONVERTER_LIBREOFFICE_PATH', 'default': u'/usr/bin/libreoffice', 'exists': True, 'description': _(u'Path to the libreoffice program.')},
|
||||||
|
{'name': u'PDFTOPPM_PATH', 'global_name': u'CONVERTER_PDFTOPPM_PATH', 'default': u'/usr/bin/pdftoppm', 'exists': True, 'description': _(u'Path to the Popple program pdftoppm.')},
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user