mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-09-16 21:55:37 -05:00
Merge converters
This commit is contained in:
@@ -13,10 +13,10 @@ from pikepdf import Page
|
||||
from pikepdf import PasswordError
|
||||
from pikepdf import Pdf
|
||||
|
||||
from documents.converters import convert_from_tiff_to_pdf
|
||||
from documents.plugins.base import ConsumeTaskPlugin
|
||||
from documents.plugins.base import StopConsumeTaskError
|
||||
from documents.plugins.helpers import ProgressStatusOptions
|
||||
from paperless.converters import convert_from_tiff_to_pdf
|
||||
from paperless.data_models import ConsumableDocument
|
||||
from paperless.models import Tag
|
||||
from paperless.utils import copy_basic_file_stats
|
||||
|
50
src/paperless/converters.py
Normal file
50
src/paperless/converters.py
Normal file
@@ -0,0 +1,50 @@
|
||||
from pathlib import Path
|
||||
|
||||
import img2pdf
|
||||
from django.conf import settings
|
||||
from PIL import Image
|
||||
|
||||
from paperless.utils import copy_basic_file_stats
|
||||
from paperless.utils import maybe_override_pixel_limit
|
||||
from paperless.utils import run_subprocess
|
||||
|
||||
|
||||
def convert_from_tiff_to_pdf(tiff_path: Path, target_directory: Path) -> Path:
|
||||
"""
|
||||
Converts a TIFF file into a PDF file.
|
||||
|
||||
The PDF will be created in the given target_directory and share the name of
|
||||
the original TIFF file, as well as its stats (mtime etc.).
|
||||
|
||||
Returns the path of the PDF created.
|
||||
"""
|
||||
# override pixel setting if needed
|
||||
maybe_override_pixel_limit()
|
||||
|
||||
with Image.open(tiff_path) as im:
|
||||
has_alpha_layer = im.mode in ("RGBA", "LA")
|
||||
if has_alpha_layer:
|
||||
# Note the save into the temp folder, so as not to trigger a new
|
||||
# consume
|
||||
scratch_image = target_directory / tiff_path.name
|
||||
run_subprocess(
|
||||
[
|
||||
settings.CONVERT_BINARY,
|
||||
"-alpha",
|
||||
"off",
|
||||
tiff_path,
|
||||
scratch_image,
|
||||
],
|
||||
)
|
||||
else:
|
||||
# Not modifying the original, safe to use in place
|
||||
scratch_image = tiff_path
|
||||
|
||||
pdf_path = (target_directory / tiff_path.name).with_suffix(".pdf")
|
||||
|
||||
with scratch_image.open("rb") as img_file, pdf_path.open("wb") as pdf_file:
|
||||
pdf_file.write(img2pdf.convert(img_file))
|
||||
|
||||
# Copy what file stat is possible
|
||||
copy_basic_file_stats(tiff_path, pdf_path)
|
||||
return pdf_path
|
@@ -8,12 +8,12 @@ from typing import Final
|
||||
from django.conf import settings
|
||||
from pikepdf import Pdf
|
||||
|
||||
from documents.converters import convert_from_tiff_to_pdf
|
||||
from documents.plugins.base import ConsumeTaskPlugin
|
||||
from documents.plugins.base import NoCleanupPluginMixin
|
||||
from documents.plugins.base import NoSetupPluginMixin
|
||||
from documents.plugins.base import StopConsumeTaskError
|
||||
from paperless.consumer import ConsumerError
|
||||
from paperless.converters import convert_from_tiff_to_pdf
|
||||
|
||||
logger = logging.getLogger("paperless.double_sided")
|
||||
|
||||
|
Reference in New Issue
Block a user