Add PDF orientation detection. Closes GitLab issue #387.

Signed-off-by: Roberto Rosario <roberto.rosario.gonzalez@gmail.com>
This commit is contained in:
Roberto Rosario
2017-06-16 22:29:36 -04:00
parent 86a351e826
commit 4150fea2ad
7 changed files with 49 additions and 0 deletions

View File

@@ -420,6 +420,7 @@ class DocumentVersion(models.Model):
self.update_mimetype(save=False)
self.save()
self.update_page_count(save=False)
self.fix_orientation()
logger.info(
'New document version "%s" created for document: %s',
@@ -466,6 +467,15 @@ class DocumentVersion(models.Model):
"""
return self.file.storage.exists(self.file.name)
def fix_orientation(self):
for page in self.pages.all():
degrees = page.detect_orientation()
if degrees:
Transformation.objects.add_for_model(
obj=page, transformation=TransformationRotate,
arguments='{{"degrees": {}}}'.format(360-degrees)
)
def get_intermidiate_file(self):
cache_filename = self.cache_filename
logger.debug('Intermidiate filename: %s', cache_filename)
@@ -693,6 +703,16 @@ class DocumentPage(models.Model):
def document(self):
return self.document_version.document
def detect_orientation(self):
with self.document_version.open() as file_object:
converter = converter_class(
file_object=file_object,
mime_type=self.document_version.mimetype
)
return converter.detect_orientation(
page_number=self.page_number
)
def generate_image(self, *args, **kwargs):
# Convert arguments into transformations
transformations = kwargs.get('transformations', [])