From 023492bb8059c1f77b35f2ce8f2cfd7ca18ff50b Mon Sep 17 00:00:00 2001 From: Arthur Moore Date: Tue, 15 Oct 2024 21:06:20 -0400 Subject: [PATCH 1/5] Add decryption to the cat operation --- pdfly/cat.py | 12 ++++++++++-- pdfly/cli.py | 5 ++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/pdfly/cat.py b/pdfly/cat.py index 3481b71..cea631e 100644 --- a/pdfly/cat.py +++ b/pdfly/cat.py @@ -47,13 +47,14 @@ import sys import traceback from pathlib import Path -from typing import List, Tuple +from typing import List, Tuple, Optional from pypdf import PageRange, PdfReader, PdfWriter, parse_filename_page_ranges +from pypdf.errors import FileNotDecryptedError def main( - filename: Path, fn_pgrgs: List[str], output: Path, verbose: bool + filename: Path, fn_pgrgs: List[str], output: Optional[Path], verbose: bool, password: Optional[str] = None ) -> None: filename_page_ranges = parse_filepaths_and_pagerange_args( filename, fn_pgrgs @@ -74,6 +75,9 @@ def main( in_fs[filename] = open(filename, "rb") reader = PdfReader(in_fs[filename]) + if password is not None: + reader.decrypt(password) + num_pages = len(reader.pages) start, end, step = page_range.indices(num_pages) if ( @@ -90,6 +94,10 @@ def main( for page_num in range(*page_range.indices(len(reader.pages))): writer.add_page(reader.pages[page_num]) writer.write(output_fh) + except FileNotDecryptedError as error: + print(str(error), file=sys.stderr) + print(f"Error while reading {filename}", file=sys.stderr) + sys.exit(1) except Exception: print(traceback.format_exc(), file=sys.stderr) print(f"Error while reading {filename}", file=sys.stderr) diff --git a/pdfly/cli.py b/pdfly/cli.py index 4b377f4..ff32e84 100644 --- a/pdfly/cli.py +++ b/pdfly/cli.py @@ -99,8 +99,11 @@ def cat( verbose: bool = typer.Option( False, help="show page ranges as they are being read" ), + password: str = typer.Option( + None, help="Document's user or owner password." + ), ) -> None: - pdfly.cat.main(filename, fn_pgrgs, output, verbose) + pdfly.cat.main(filename, fn_pgrgs, output, verbose, password) @entry_point.command(name="meta") # type: ignore[misc] From cffaf2f91f980a8f4564e24caa19f3a34bd3577d Mon Sep 17 00:00:00 2001 From: Martin Thoma Date: Sun, 8 Dec 2024 10:54:31 +0100 Subject: [PATCH 2/5] trailing comma --- pdfly/cat.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pdfly/cat.py b/pdfly/cat.py index 5ac76c6..ba6bce7 100644 --- a/pdfly/cat.py +++ b/pdfly/cat.py @@ -58,7 +58,7 @@ def main( output: Path, verbose: bool, inverted_page_selection: bool = False, - password: Optional[str] = None + password: Optional[str] = None, ) -> None: filename_page_ranges = parse_filepaths_and_pagerange_args( filename, fn_pgrgs From 31afa748934b62747c82189ac1c804b9ab6bc15d Mon Sep 17 00:00:00 2001 From: Martin Thoma Date: Sun, 8 Dec 2024 11:01:06 +0100 Subject: [PATCH 3/5] Update pdfly/cli.py --- pdfly/cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pdfly/cli.py b/pdfly/cli.py index 5e45773..32dac25 100644 --- a/pdfly/cli.py +++ b/pdfly/cli.py @@ -99,7 +99,7 @@ def cat( None, help="Document's user or owner password." ), ) -> None: - pdfly.cat.main(filename, fn_pgrgs, output, verbose, password) + pdfly.cat.main(filename, fn_pgrgs, output, verbose, password=password) @entry_point.command(name="booklet", help=pdfly.booklet.__doc__) # type: ignore[misc] From fc4783fd184f21009cfb27f98994e02d051a29c3 Mon Sep 17 00:00:00 2001 From: Martin Thoma Date: Sun, 8 Dec 2024 11:09:38 +0100 Subject: [PATCH 4/5] Import sorting --- pdfly/cat.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pdfly/cat.py b/pdfly/cat.py index ba6bce7..6da86f5 100644 --- a/pdfly/cat.py +++ b/pdfly/cat.py @@ -46,7 +46,7 @@ import sys import traceback from pathlib import Path -from typing import List, Tuple, Optional +from typing import List, Optional, Tuple from pypdf import PageRange, PdfReader, PdfWriter, parse_filename_page_ranges from pypdf.errors import FileNotDecryptedError From 0e3b361eb57fe8c52996c64972814605ed1c293a Mon Sep 17 00:00:00 2001 From: Martin Thoma Date: Sun, 8 Dec 2024 11:18:23 +0100 Subject: [PATCH 5/5] bugfix --- pdfly/cat.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pdfly/cat.py b/pdfly/cat.py index 6da86f5..6d88c5e 100644 --- a/pdfly/cat.py +++ b/pdfly/cat.py @@ -78,7 +78,7 @@ def main( if filepath not in in_fs: in_fs[filepath] = open(filepath, "rb") - reader = PdfReader(in_fs[filename]) + reader = PdfReader(in_fs[filepath]) if password is not None: reader.decrypt(password) num_pages = len(reader.pages)