Added PDF page count support to the python converter backend
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
import slate
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
@@ -9,11 +10,27 @@ from converter.literals import TRANSFORMATION_RESIZE, \
|
|||||||
TRANSFORMATION_ROTATE, TRANSFORMATION_ZOOM
|
TRANSFORMATION_ROTATE, TRANSFORMATION_ZOOM
|
||||||
from converter.literals import QUALITY_DEFAULT, DEFAULT_PAGE_NUMBER, \
|
from converter.literals import QUALITY_DEFAULT, DEFAULT_PAGE_NUMBER, \
|
||||||
DEFAULT_FILE_FORMAT
|
DEFAULT_FILE_FORMAT
|
||||||
|
from converter.utils import get_mimetype
|
||||||
|
|
||||||
|
|
||||||
class ConverterClass(ConverterBase):
|
class ConverterClass(ConverterBase):
|
||||||
def get_page_count(self, input_filepath):
|
def get_page_count(self, input_filepath):
|
||||||
page_count = 1
|
page_count = 1
|
||||||
|
|
||||||
|
mimetype, encoding = get_mimetype(input_filepath)
|
||||||
|
if mimetype == 'application/pdf':
|
||||||
|
# If file is a PDF open it with slate to determine the page
|
||||||
|
# count
|
||||||
|
with open(input_filepath) as fd:
|
||||||
|
pages = slate.PDF(fd)
|
||||||
|
return len(pages)
|
||||||
|
|
||||||
|
try:
|
||||||
im = Image.open(input_filepath)
|
im = Image.open(input_filepath)
|
||||||
|
except IOError: #cannot identify image file
|
||||||
|
# Return a page count of 1, to atleast allow the document
|
||||||
|
# to be created
|
||||||
|
return 1
|
||||||
|
|
||||||
try:
|
try:
|
||||||
while 1:
|
while 1:
|
||||||
|
|||||||
Reference in New Issue
Block a user