mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-04-02 13:45:10 -05:00
Instead of using PIL directly to convert TIFF to PDF, use the existing library of img2pdf
This commit is contained in:
parent
567a1bb770
commit
0778c2808b
@ -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
|
||||
|
||||
|
||||
|
@ -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,14 +1041,6 @@ class TestAsnBarcodes(DirectoriesMixin, TestCase):
|
||||
)
|
||||
|
||||
|
||||
try:
|
||||
import zxingcpp
|
||||
|
||||
ZXING_AVAILIBLE = True
|
||||
except ImportError:
|
||||
ZXING_AVAILIBLE = False
|
||||
|
||||
|
||||
@pytest.mark.skipif(
|
||||
not ZXING_AVAILIBLE,
|
||||
reason="No zxingcpp",
|
||||
@ -1057,3 +1048,12 @@ except ImportError:
|
||||
@override_settings(CONSUMER_BARCODE_SCANNER="ZXING")
|
||||
class TestBarcodeZxing(TestBarcode):
|
||||
pass
|
||||
|
||||
|
||||
@pytest.mark.skipif(
|
||||
not ZXING_AVAILIBLE,
|
||||
reason="No zxingcpp",
|
||||
)
|
||||
@override_settings(CONSUMER_BARCODE_SCANNER="ZXING")
|
||||
class TestAsnBarcodesZxing(TestAsnBarcodes):
|
||||
pass
|
||||
|
Loading…
x
Reference in New Issue
Block a user