Allow the signature_index to be None

When the signature_index is not set, the name's sinature zone is generated from a random int.
This commit is contained in:
Julien Fastré 2024-10-15 07:47:58 +02:00
parent 89cb05fce1
commit 34857ae4b0
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
2 changed files with 5 additions and 4 deletions

View File

@ -1,4 +1,5 @@
import io
from random import randint
from typing import Optional
from pyhanko import stamp
@ -49,8 +50,8 @@ class SignOrchestrator:
reason=reason,
)
def sign(self, reason: str, signature_index: int, input_content: Buffer, on_page: int, box_place: (int, int, int, int), signer_text: str) -> io.BytesIO:
field_name = 'Signature' + str(signature_index)
def sign(self, reason: str, signature_index: int|None, input_content: Buffer, on_page: int, box_place: (int, int, int, int), signer_text: str) -> io.BytesIO:
field_name = 'Signature' + str(signature_index) if signature_index is not None else 'Signature'+ str(randint(1000, 99999999999))
signature_meta = self._make_signature_metadata(reason, field_name)
pdf_signer = signers.PdfSigner(

View File

@ -13,7 +13,7 @@ orchestrator = SignOrchestrator('./assets/dummy.p12',
pkcs12_password=None)
with open('./assets/test.pdf', 'rb') as input:
signed_content = orchestrator.sign(reason="first signer", signature_index=0,
signed_content = orchestrator.sign(reason="first signer", signature_index=None,
input_content=input.read(), box_place=(300, 600, 500, 660), on_page=0,
signer_text="Mme Caroline Diallo")
@ -21,7 +21,7 @@ with open('./assets/test.pdf', 'rb') as input:
output.write(signed_content.read())
with open('./assets/test_signed_0.pdf', 'rb') as input:
signed_content = orchestrator.sign(reason="second signer", signature_index=1,
signed_content = orchestrator.sign(reason="second signer", signature_index=None,
input_content=input.read(), box_place=(100, 600, 300, 660), on_page=0,
signer_text="M. Bah Mamadou")