-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13_digital_signatures.py
More file actions
81 lines (64 loc) · 2.34 KB
/
Copy path13_digital_signatures.py
File metadata and controls
81 lines (64 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""
13 - Digital signatures.
- Sign a PDF with an X.509 certificate (.pfx / .p12)
- Visible or invisible signatures
- Verify single and multi-signed documents
Run:
python examples/13_digital_signatures.py
Note: this demo requires a PFX certificate at samples/cert.pfx with the
password 'password'. Generate a self-signed test cert with OpenSSL:
openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 365 -nodes \\
-subj "/CN=Demo Signer"
openssl pkcs12 -export -out cert.pfx -inkey key.pem -in cert.pem -password pass:password
If samples/cert.pfx doesn't exist, the script reports that and exits cleanly.
"""
from _common import (
header, info, success, warn, ensure_output_dir, get_sample_pdf,
SAMPLES_DIR, init_license,
)
import exis_pdfeditor
def main() -> None:
init_license()
header("Demo 13 - Digital Signatures")
sample = get_sample_pdf()
out = ensure_output_dir()
cert_path = SAMPLES_DIR / "cert.pfx"
if not cert_path.exists():
warn("samples/cert.pfx not found - generate a test cert (see script header) and re-run.")
return
# Sign with an invisible signature
info("Signing with an invisible signature...")
exis_pdfeditor.sign(
str(sample),
str(out / "13-signed-invisible.pdf"),
cert_path=str(cert_path),
cert_password="password",
reason="Demo signature",
location="Demo",
signer_name="Demo Signer",
)
success("Signed (invisible)")
# Sign with a visible signature box
info("Signing with a visible signature box on page 1...")
exis_pdfeditor.sign(
str(sample),
str(out / "13-signed-visible.pdf"),
cert_path=str(cert_path),
cert_password="password",
visible=True,
page=1,
rect={"x": 50, "y": 50, "width": 200, "height": 60},
reason="Reviewed and approved",
)
success("Signed (visible)")
# Verify
info("Verifying signatures...")
sig_info = exis_pdfeditor.verify(str(out / "13-signed-invisible.pdf"))
print(f" Signed: {sig_info.isSigned}")
print(f" Signer: {sig_info.signerName}")
print(f" Valid: {sig_info.isValid}")
print(f" Reason: {sig_info.reason}")
print(f" Date: {sig_info.signDate}")
success("Verification complete")
if __name__ == "__main__":
main()