From ac43e294b3eae721055393a76150b24ed8429ea7 Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Mon, 18 Jul 2011 04:04:22 -0400 Subject: [PATCH] Added PDF page count support to the python converter backend --- apps/converter/backends/python/base.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/apps/converter/backends/python/base.py b/apps/converter/backends/python/base.py index 616e997d3f..25448346ff 100644 --- a/apps/converter/backends/python/base.py +++ b/apps/converter/backends/python/base.py @@ -1,3 +1,4 @@ +import slate from PIL import Image from django.utils.translation import ugettext_lazy as _ @@ -9,12 +10,28 @@ from converter.literals import TRANSFORMATION_RESIZE, \ TRANSFORMATION_ROTATE, TRANSFORMATION_ZOOM from converter.literals import QUALITY_DEFAULT, DEFAULT_PAGE_NUMBER, \ DEFAULT_FILE_FORMAT +from converter.utils import get_mimetype + class ConverterClass(ConverterBase): def get_page_count(self, input_filepath): page_count = 1 - im = Image.open(input_filepath) - + + 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) + except IOError: #cannot identify image file + # Return a page count of 1, to atleast allow the document + # to be created + return 1 + try: while 1: im.seek(im.tell()+1)