Skip to content

Commit

Permalink
Add --resave flag to addfields subcommand
Browse files Browse the repository at this point in the history
...to allow doing a full resave in order to eliminate any structural
issues from the input PDF at the cost of destroying any earlier
signatures.
  • Loading branch information
MatthiasValvekens committed Sep 29, 2023
1 parent b327a82 commit 90ccbd2
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
13 changes: 11 additions & 2 deletions pyhanko/cli/commands/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from pyhanko.cli.utils import parse_field_location_spec
from pyhanko.pdf_utils.incremental_writer import IncrementalPdfFileWriter
from pyhanko.pdf_utils.reader import PdfFileReader
from pyhanko.pdf_utils.writer import copy_into_new_writer
from pyhanko.sign import fields

__all__ = ['list_sigfields', 'add_sig_field']
Expand Down Expand Up @@ -44,9 +45,17 @@ def list_sigfields(infile, skip_status):
required=True,
help="Field specification (multiple allowed)",
)
def add_sig_field(infile, outfile, field):
@click.option(
'--resave',
is_flag=True,
help="Resave the PDF document instead of creating an incremental update",
)
def add_sig_field(infile, outfile, field, resave):
with pyhanko_exception_manager():
writer = IncrementalPdfFileWriter(infile, strict=False)
if resave:
writer = copy_into_new_writer(PdfFileReader(infile, strict=False))
else:
writer = IncrementalPdfFileWriter(infile, strict=False)

for s in field:
name, spec = parse_field_location_spec(s)
Expand Down
53 changes: 52 additions & 1 deletion pyhanko_tests/cli_tests/test_cli_field_mgmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from pyhanko.pdf_utils.reader import PdfFileReader
from pyhanko.sign import PdfSignatureMetadata, sign_pdf
from pyhanko_tests.cli_tests.conftest import INPUT_PATH
from pyhanko_tests.samples import MINIMAL_TWO_FIELDS, MINIMAL_TWO_PAGES
from pyhanko_tests.samples import MINIMAL, MINIMAL_TWO_FIELDS, MINIMAL_TWO_PAGES
from pyhanko_tests.signing_commons import FROM_CA


Expand Down Expand Up @@ -69,6 +69,57 @@ def test_list_empty_fields_without_status(cli_runner):
assert result.output == 'Sig1\nSig2\n'


def test_cli_add_field_incremental_update_by_default(cli_runner):
with open(INPUT_PATH, 'wb') as inf:
inf.write(MINIMAL)

output_path = 'presign.pdf'
result = cli_runner.invoke(
cli_root,
[
'sign',
'addfields',
'--field',
'1/0,0,100,100/Sig1',
INPUT_PATH,
output_path,
],
)
assert not result.exception, result.output

with open(output_path, 'rb') as inf:
r = PdfFileReader(inf)
name = r.root['/AcroForm']['/Fields'][0]['/T']
assert name == 'Sig1'
assert r.xrefs.total_revisions == 2


def test_cli_add_field_with_resave(cli_runner):
with open(INPUT_PATH, 'wb') as inf:
inf.write(MINIMAL)

output_path = 'presign.pdf'
result = cli_runner.invoke(
cli_root,
[
'sign',
'addfields',
'--resave',
'--field',
'1/0,0,100,100/Sig1',
INPUT_PATH,
output_path,
],
)
assert not result.exception, result.output

with open(output_path, 'rb') as inf:
r = PdfFileReader(inf)
name = r.root['/AcroForm']['/Fields'][0]['/T']
assert name == 'Sig1'
assert r.xrefs.total_revisions == 1


def test_cli_add_field_to_last_page(cli_runner):
with open(INPUT_PATH, 'wb') as inf:
inf.write(MINIMAL_TWO_PAGES)
Expand Down

0 comments on commit 90ccbd2

Please sign in to comment.