mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-07-28 18:24:38 -05:00
In case pikepdf fails to convert an image to a PIL image, fall back to converting pages to PIL images
This commit is contained in:
@@ -3,6 +3,7 @@ import shutil
|
||||
import tempfile
|
||||
from unittest import mock
|
||||
|
||||
import pikepdf
|
||||
from django.conf import settings
|
||||
from django.test import override_settings
|
||||
from django.test import TestCase
|
||||
@@ -218,6 +219,66 @@ class TestBarcode(DirectoriesMixin, TestCase):
|
||||
self.assertEqual(pdf_file, test_file)
|
||||
self.assertListEqual(separator_page_numbers, [1])
|
||||
|
||||
def test_scan_file_for_separating_barcodes_pillow_transcode_error(self):
|
||||
"""
|
||||
GIVEN:
|
||||
- A PDF containing an image which cannot be transcoded to a PIL image
|
||||
WHEN:
|
||||
- The image tries to be transcoded to a PIL image, but fails
|
||||
THEN:
|
||||
- The barcode reader is still called, as
|
||||
"""
|
||||
|
||||
def _build_device_n_pdf(self, save_path: str):
|
||||
# Based on the pikepdf tests
|
||||
# https://github.com/pikepdf/pikepdf/blob/abb35ebe17d579d76abe08265e00cf8890a12a95/tests/test_image_access.py
|
||||
pdf = pikepdf.new()
|
||||
pdf.add_blank_page(page_size=(72, 72))
|
||||
imobj = pikepdf.Stream(
|
||||
pdf,
|
||||
bytes(range(0, 256)),
|
||||
BitsPerComponent=8,
|
||||
ColorSpace=pikepdf.Array(
|
||||
[
|
||||
pikepdf.Name.DeviceN,
|
||||
pikepdf.Array([pikepdf.Name.Black]),
|
||||
pikepdf.Name.DeviceCMYK,
|
||||
pikepdf.Stream(
|
||||
pdf,
|
||||
b"{0 0 0 4 -1 roll}", # Colorspace conversion function
|
||||
FunctionType=4,
|
||||
Domain=[0.0, 1.0],
|
||||
Range=[0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0],
|
||||
),
|
||||
],
|
||||
),
|
||||
Width=16,
|
||||
Height=16,
|
||||
Type=pikepdf.Name.XObject,
|
||||
Subtype=pikepdf.Name.Image,
|
||||
)
|
||||
pim = pikepdf.PdfImage(imobj)
|
||||
self.assertEqual(pim.mode, "DeviceN")
|
||||
self.assertTrue(pim.is_device_n)
|
||||
|
||||
pdf.pages[0].Contents = pikepdf.Stream(pdf, b"72 0 0 72 0 0 cm /Im0 Do")
|
||||
pdf.pages[0].Resources = pikepdf.Dictionary(
|
||||
XObject=pikepdf.Dictionary(Im0=imobj),
|
||||
)
|
||||
pdf.save(save_path)
|
||||
|
||||
with tempfile.NamedTemporaryFile(suffix="pdf") as device_n_pdf:
|
||||
# Build an offending file
|
||||
_build_device_n_pdf(self, str(device_n_pdf.name))
|
||||
with mock.patch("documents.barcodes.barcode_reader") as reader:
|
||||
reader.return_value = list()
|
||||
|
||||
_, _ = barcodes.scan_file_for_separating_barcodes(
|
||||
str(device_n_pdf.name),
|
||||
)
|
||||
|
||||
reader.assert_called()
|
||||
|
||||
def test_scan_file_for_separating_qr_barcodes(self):
|
||||
test_file = os.path.join(
|
||||
self.BARCODE_SAMPLE_DIR,
|
||||
|
Reference in New Issue
Block a user