Repository navigation
Expand file tree
/
Copy path10_encrypt_decrypt.py
More file actions
56 lines (44 loc) · 1.56 KB
/
Copy path10_encrypt_decrypt.py
File metadata and controls
56 lines (44 loc) · 1.56 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
"""
10 - Encrypt & Decrypt.
- Encrypt with user password (open password) and owner password (permissions)
- Granular permissions: Print, ModifyContents, CopyText, AddAnnotations,
FillForms, PrintHighQuality, All
- Decrypt to remove protection (requires the password)
Run:
python examples/10_encrypt_decrypt.py
"""
from _common import (
header, info, success, ensure_output_dir, get_sample_pdf, init_license,
)
import exis_pdfeditor
def main() -> None:
init_license()
header("Demo 10 - Encrypt & Decrypt")
sample = get_sample_pdf()
out = ensure_output_dir()
# Encrypt with both passwords + Print-only permissions
encrypted_path = out / "10-encrypted.pdf"
info("Encrypting with user password 'open123' and owner password 'admin456'...")
info("Permissions: Print only (no editing, copying, or annotations)")
exis_pdfeditor.encrypt(
str(sample),
str(encrypted_path),
user_password="open123",
owner_password="admin456",
permissions=["Print"],
)
success(f"Encrypted: {encrypted_path}")
# Decrypt
info("Decrypting back with the user password...")
decrypted_path = out / "10-decrypted.pdf"
exis_pdfeditor.decrypt(
str(encrypted_path),
str(decrypted_path),
password="open123",
)
success(f"Decrypted: {decrypted_path}")
print()
info("Open 10-encrypted.pdf in any PDF reader - you'll need 'open123' to view it.")
info("Try printing - it works. Try copying text or editing - blocked by permissions.")
if __name__ == "__main__":
main()