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: