From 6d4cde98b67349fbd8342c8334e17794e91cdd7c Mon Sep 17 00:00:00 2001 From: Matthias Valvekens Date: Thu, 21 Mar 2024 20:32:24 +0100 Subject: [PATCH] Tolerate AcroForms without a Fields entry Fixes #403 --- pyhanko/sign/fields.py | 2 +- pyhanko_tests/test_fields.py | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/pyhanko/sign/fields.py b/pyhanko/sign/fields.py index 6919b808..737ac53e 100644 --- a/pyhanko/sign/fields.py +++ b/pyhanko/sign/fields.py @@ -1473,7 +1473,7 @@ def prepare_sig_field( try: fields = form['/Fields'] except KeyError: - raise PdfError('/AcroForm has no /Fields') + fields = form['/Fields'] = generic.ArrayObject() candidates = enumerate_sig_fields_in( fields, with_name=sig_field_name, refs_seen=set() diff --git a/pyhanko_tests/test_fields.py b/pyhanko_tests/test_fields.py index 3f80dea1..a431e990 100644 --- a/pyhanko_tests/test_fields.py +++ b/pyhanko_tests/test_fields.py @@ -439,8 +439,6 @@ def test_append_sig_field_acro_update(): def test_append_acroform_no_fields(): w = PdfFileWriter() - # Technically, this is not standards-compliant, but our routine - # shouldn't care w.root['/AcroForm'] = generic.DictionaryObject() w.insert_page(simple_page(w, 'Hello world')) out = BytesIO() @@ -449,8 +447,11 @@ def test_append_acroform_no_fields(): sp = fields.SigFieldSpec('InvisibleSig') w = IncrementalPdfFileWriter(out) - with pytest.raises(PdfError, match="has no /Fields"): - fields.append_signature_field(w, sp) + fields.append_signature_field(w, sp) + w.write_in_place() + + r = PdfFileReader(out) + assert len(r.root['/AcroForm']['/Fields']) == 1 def test_append_acroform_reference_broken_nonstrict():