Skip to content

A community-supported supercharged document management system: scan, index and archive all your documents

Instead of using PIL directly to convert TIFF to PDF, use the existing library of img2pdf

Trenton H0778c2808bd7
diff --git a/src/documents/barcodes.py b/src/documents/barcodes.py
index 1575b9966..1f520c546 100644
--- a/src/documents/barcodes.py
+++ b/src/documents/barcodes.py
@@ -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
diff --git a/src/documents/tests/test_barcodes.py b/src/documents/tests/test_barcodes.py
index 547a315d9..a1e08c5cf 100644
--- a/src/documents/tests/test_barcodes.py
+++ b/src/documents/tests/test_barcodes.py
@@ -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
+ 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