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

This commit is contained in:
Trenton H 2023-03-20 13:48:00 -07:00
parent 567a1bb770
commit 0778c2808b
No known key found for this signature in database
GPG Key ID: 4815A6E23A56B8D1
2 changed files with 33 additions and 32 deletions

View File

@ -5,10 +5,12 @@ import tempfile
from dataclasses import dataclass from dataclasses import dataclass
from functools import lru_cache from functools import lru_cache
from pathlib import Path from pathlib import Path
from subprocess import run
from typing import Dict from typing import Dict
from typing import List from typing import List
from typing import Optional from typing import Optional
import img2pdf
import magic import magic
from django.conf import settings from django.conf import settings
from pdf2image import convert_from_path from pdf2image import convert_from_path
@ -16,7 +18,6 @@ from pdf2image.exceptions import PDFPageCountError
from pikepdf import Page from pikepdf import Page
from pikepdf import Pdf from pikepdf import Pdf
from PIL import Image from PIL import Image
from PIL import ImageSequence
logger = logging.getLogger("paperless.barcodes") 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.", f"Cannot convert mime type {mime_type} from {filepath} to pdf.",
) )
return None return None
with Image.open(filepath) as image: with Image.open(filepath) as im:
images = [] has_alpha_layer = im.mode in ("RGBA", "LA")
for i, page in enumerate(ImageSequence.Iterator(image)): if has_alpha_layer:
page = page.convert("RGB") run(
images.append(page) [
try: settings.CONVERT_BINARY,
if len(images) == 1: "-alpha",
images[0].save(newpath) "off",
else: filepath,
images[0].save(newpath, save_all=True, append_images=images[1:]) filepath,
except OSError as e: # pragma: no cover ],
logger.warning( )
f"Could not save the file as pdf. Error: {str(e)}", with filepath.open("rb") as img_file:
) with newpath.open("wb") as pdf_file:
return None pdf_file.write(img2pdf.convert(img_file))
return newpath return newpath

View File

@ -14,6 +14,13 @@ from documents.tests.utils import DirectoriesMixin
from documents.tests.utils import FileSystemAssertsMixin from documents.tests.utils import FileSystemAssertsMixin
from PIL import Image from PIL import Image
try:
import zxingcpp
ZXING_AVAILIBLE = True
except ImportError:
ZXING_AVAILIBLE = False
@override_settings(CONSUMER_BARCODE_SCANNER="PYZBAR") @override_settings(CONSUMER_BARCODE_SCANNER="PYZBAR")
class TestBarcode(DirectoriesMixin, FileSystemAssertsMixin, TestCase): class TestBarcode(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
@ -672,10 +679,6 @@ class TestBarcode(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
CONSUMER_ENABLE_BARCODES=True, CONSUMER_ENABLE_BARCODES=True,
CONSUMER_BARCODE_TIFF_SUPPORT=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): def test_consume_barcode_tiff_file(self):
""" """
GIVEN: GIVEN:
@ -735,10 +738,6 @@ class TestBarcode(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
CONSUMER_ENABLE_BARCODES=True, CONSUMER_ENABLE_BARCODES=True,
CONSUMER_BARCODE_TIFF_SUPPORT=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): def test_consume_barcode_supported_no_extension_file(self):
""" """
GIVEN: GIVEN:
@ -1042,14 +1041,6 @@ class TestAsnBarcodes(DirectoriesMixin, TestCase):
) )
try:
import zxingcpp
ZXING_AVAILIBLE = True
except ImportError:
ZXING_AVAILIBLE = False
@pytest.mark.skipif( @pytest.mark.skipif(
not ZXING_AVAILIBLE, not ZXING_AVAILIBLE,
reason="No zxingcpp", reason="No zxingcpp",
@ -1057,3 +1048,12 @@ except ImportError:
@override_settings(CONSUMER_BARCODE_SCANNER="ZXING") @override_settings(CONSUMER_BARCODE_SCANNER="ZXING")
class TestBarcodeZxing(TestBarcode): class TestBarcodeZxing(TestBarcode):
pass pass
@pytest.mark.skipif(
not ZXING_AVAILIBLE,
reason="No zxingcpp",
)
@override_settings(CONSUMER_BARCODE_SCANNER="ZXING")
class TestAsnBarcodesZxing(TestAsnBarcodes):
pass