Skip to content

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

Fix: ignore invalid EXIF orientation when generating image archives (#14203)

Zhiyuan Zheng12314fcaa8d9
diff --git a/src/paperless/parsers/tesseract.py b/src/paperless/parsers/tesseract.py
index f3ebf9925..1091dffda 100644
--- a/src/paperless/parsers/tesseract.py
+++ b/src/paperless/parsers/tesseract.py
@@ -394,7 +394,11 @@ class RasterisedDocumentParser:
plain_pdf_path = Path(self.tempdir) / "image_plain.pdf"
try:
- convert_kwargs: dict = {}
+ convert_kwargs: dict = {
+ # Ignore invalid EXIF orientation values (e.g. 0) instead of
+ # aborting the conversion; valid values are still applied
+ "rotation": img2pdf.Rotation.ifvalid,
+ }
if self.settings.image_dpi is not None:
convert_kwargs["layout_fun"] = img2pdf.get_fixed_dpi_layout_fun(
(self.settings.image_dpi, self.settings.image_dpi),
diff --git a/src/paperless/tests/parsers/test_convert_image_to_pdfa.py b/src/paperless/tests/parsers/test_convert_image_to_pdfa.py
index 615900a25..46261ffcf 100644
--- a/src/paperless/tests/parsers/test_convert_image_to_pdfa.py
+++ b/src/paperless/tests/parsers/test_convert_image_to_pdfa.py
@@ -18,6 +18,7 @@ import img2pdf
import magic
import pikepdf
import pytest
+from PIL import Image
from documents.parsers import ParseError
@@ -139,3 +140,24 @@ class TestConvertImageToPdfa:
tesseract_parser._convert_image_to_pdfa(simple_png_file)
spy.assert_not_called()
+
+ def test_invalid_exif_orientation_is_ignored(
+ self,
+ tesseract_parser: RasterisedDocumentParser,
+ tmp_path: Path,
+ ) -> None:
+ """
+ GIVEN: a JPEG with an invalid EXIF orientation value (0)
+ WHEN: _convert_image_to_pdfa is called
+ THEN: the invalid value is ignored and a valid PDF is produced
+ """
+ image_path = tmp_path / "invalid_orientation.jpg"
+ with Image.new("RGB", (120, 80), "white") as image:
+ exif = image.getexif()
+ exif[274] = 0 # EXIF tag 274: Orientation
+ image.save(image_path, exif=exif)
+
+ result = tesseract_parser._convert_image_to_pdfa(image_path)
+
+ assert result.exists()
+ assert magic.from_file(str(result), mime=True) == "application/pdf"