From 14868f42648b6e8009c5b2f41d9f9c92f9eb0746 Mon Sep 17 00:00:00 2001 From: Maxime Bombar Date: Tue, 30 Oct 2018 22:21:11 +0100 Subject: [PATCH] [Printer] Handles pdf with fancy encoding in metadata --- printer/utils.py | 8 +++++--- printer/views.py | 7 +++++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/printer/utils.py b/printer/utils.py index 541bc1db..dafce878 100644 --- a/printer/utils.py +++ b/printer/utils.py @@ -32,9 +32,11 @@ def pdfinfo(file_path): cmd_output = subprocess.check_output(['/usr/bin/pdfinfo', file_path]) for line in cmd_output.splitlines(): for label in labels: - if label in line.decode(): - output[label] = _extract(line) - + try: + if label in line.decode(): + output[label] = _extract(line) + except UnicodeDecodeError: + pass return output diff --git a/printer/views.py b/printer/views.py index 3d7690ee..3298ff61 100644 --- a/printer/views.py +++ b/printer/views.py @@ -68,8 +68,11 @@ def new_job(request): job_instance.print_operation = print_operation job_instance.user=request.user metadata = pdfinfo(request.FILES['form-%s-file' % count].temporary_file_path()) - job_instance.pages = metadata["Pages"] - job_instance.save() + if "Pages" in metadata: + job_instance.pages = metadata["Pages"] + job_instance.save() + else: + job_form.erros[count] = {'file': ['Invalid PDF']} except KeyError: job_form.errors[count] = {'file': ['This field is required.']} if job_formset.total_error_count() == 0: