]> git.ipfire.org Git - thirdparty/paperless-ngx.git/commitdiff
Instead of using PIL directly to convert TIFF to PDF, use the existing library of...
authorTrenton H <797416+stumpylog@users.noreply.github.com>
Mon, 20 Mar 2023 20:48:00 +0000 (13:48 -0700)
committerTrenton H <797416+stumpylog@users.noreply.github.com>
Mon, 20 Mar 2023 20:48:05 +0000 (13:48 -0700)
src/documents/barcodes.py
src/documents/tests/test_barcodes.py

index 1575b996623630026266cf90e854f244963b730b..1f520c546eb276029b8db10aa7c71d1d639a2be5 100644 (file)
@@ -5,10 +5,12 @@ import tempfile
 from dataclasses import dataclass
 from functools import lru_cache
 from pathlib import Path
+from subprocess import run
 from typing import Dict
 from typing import List
 from typing import Optional
 
+import img2pdf
 import magic
 from django.conf import settings
 from pdf2image import convert_from_path
@@ -16,7 +18,6 @@ from pdf2image.exceptions import PDFPageCountError
 from pikepdf import Page
 from pikepdf import Pdf
 from PIL import Image
-from PIL import ImageSequence
 
 logger = logging.getLogger("paperless.barcodes")
 
@@ -141,21 +142,21 @@ def convert_from_tiff_to_pdf(filepath: Path) -> Path:
             f"Cannot convert mime type {mime_type} from {filepath} to pdf.",
         )
         return None
-    with Image.open(filepath) as image:
-        images = []
-        for i, page in enumerate(ImageSequence.Iterator(image)):
-            page = page.convert("RGB")
-            images.append(page)
-        try:
-            if len(images) == 1:
-                images[0].save(newpath)
-            else:
-                images[0].save(newpath, save_all=True, append_images=images[1:])
-        except OSError as e:  # pragma: no cover
-            logger.warning(
-                f"Could not save the file as pdf. Error: {str(e)}",
-            )
-            return None
+    with Image.open(filepath) as im:
+        has_alpha_layer = im.mode in ("RGBA", "LA")
+    if has_alpha_layer:
+        run(
+            [
+                settings.CONVERT_BINARY,
+                "-alpha",
+                "off",
+                filepath,
+                filepath,
+            ],
+        )
+    with filepath.open("rb") as img_file:
+        with newpath.open("wb") as pdf_file:
+            pdf_file.write(img2pdf.convert(img_file))
     return newpath
 
 
index 547a315d9eaa9a68ceb0fb3c90d79c8d942d24ac..a1e08c5cfafa6f0518681ac23a94a82acd377c5f 100644 (file)
@@ -14,6 +14,13 @@ from documents.tests.utils import DirectoriesMixin
 from documents.tests.utils import FileSystemAssertsMixin
 from PIL import Image
 
+try:
+    import zxingcpp
+
+    ZXING_AVAILIBLE = True
+except ImportError:
+    ZXING_AVAILIBLE = False
+
 
 @override_settings(CONSUMER_BARCODE_SCANNER="PYZBAR")
 class TestBarcode(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
@@ -672,10 +679,6 @@ class TestBarcode(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
         CONSUMER_ENABLE_BARCODES=True,
         CONSUMER_BARCODE_TIFF_SUPPORT=True,
     )
-    @pytest.mark.skipif(
-        settings.CONSUMER_BARCODE_SCANNER == "ZXING",
-        reason="zxingcpp has issues with tiff",
-    )
     def test_consume_barcode_tiff_file(self):
         """
         GIVEN:
@@ -735,10 +738,6 @@ class TestBarcode(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
         CONSUMER_ENABLE_BARCODES=True,
         CONSUMER_BARCODE_TIFF_SUPPORT=True,
     )
-    @pytest.mark.skipif(
-        settings.CONSUMER_BARCODE_SCANNER == "ZXING",
-        reason="zxingcpp has issues with tiff",
-    )
     def test_consume_barcode_supported_no_extension_file(self):
         """
         GIVEN:
@@ -1042,12 +1041,13 @@ class TestAsnBarcodes(DirectoriesMixin, TestCase):
             )
 
 
-try:
-    import zxingcpp
-
-    ZXING_AVAILIBLE = True
-except ImportError:
-    ZXING_AVAILIBLE = False
+@pytest.mark.skipif(
+    not ZXING_AVAILIBLE,
+    reason="No zxingcpp",
+)
+@override_settings(CONSUMER_BARCODE_SCANNER="ZXING")
+class TestBarcodeZxing(TestBarcode):
+    pass
 
 
 @pytest.mark.skipif(
@@ -1055,5 +1055,5 @@ except ImportError:
     reason="No zxingcpp",
 )
 @override_settings(CONSUMER_BARCODE_SCANNER="ZXING")
-class TestBarcodeZxing(TestBarcode):
+class TestAsnBarcodesZxing(TestAsnBarcodes):
     pass