1
+ import shutil
2
+ import json
3
+ import logging
4
+ from pathlib import Path
5
+ import base64
6
+ from asposepdfcloud import ApiClient , PdfApi , CryptoAlgorithm
7
+
8
+ # Configure logging
9
+ logging .basicConfig (level = logging .INFO , format = "%(asctime)s - %(levelname)s - %(message)s" )
10
+
11
+ class Config :
12
+ """Configuration parameters."""
13
+ CREDENTIALS_FILE = Path (r"C:\\Projects\\ASPOSE\\Pdf.Cloud\\Credentials\\credentials.json" )
14
+ LOCAL_FOLDER = Path (r"C:\Samples" )
15
+ PDF_DOCUMENT_NAME = "sample.pdf"
16
+ LOCAL_RESULT_DOCUMENT_NAME = "output_sample.pdf"
17
+ ENCRYPT_ALGORITHM = CryptoAlgorithm .AESX256
18
+ USER_PASSWORD = 'User-Password'
19
+ OWNER_PASSWORD = 'Owner-Password'
20
+
21
+ class pdfEncryption :
22
+ """Class for managing PDF encryption using Aspose PDF Cloud API."""
23
+ def __init__ (self , credentials_file : Path = Config .CREDENTIALS_FILE ):
24
+ self .pdf_api = None
25
+ self ._init_api (credentials_file )
26
+
27
+ def _init_api (self , credentials_file : Path ):
28
+ """Initialize the API client."""
29
+ try :
30
+ with credentials_file .open ("r" , encoding = "utf-8" ) as file :
31
+ credentials = json .load (file )
32
+ api_key , app_id = credentials .get ("key" ), credentials .get ("id" )
33
+ if not api_key or not app_id :
34
+ raise ValueError ("init_api(): Error: Missing API keys in the credentials file." )
35
+ self .pdf_api = PdfApi (ApiClient (api_key , app_id ))
36
+ except (FileNotFoundError , json .JSONDecodeError , ValueError ) as e :
37
+ logging .error (f"init_api(): Failed to load credentials: { e } " )
38
+
39
+ def upload_document (self ):
40
+ """ Upload a PDF document to the Aspose Cloud server. """
41
+ if self .pdf_api :
42
+ file_path = Config .LOCAL_FOLDER / Config .PDF_DOCUMENT_NAME
43
+ try :
44
+ self .pdf_api .upload_file (Config .PDF_DOCUMENT_NAME , str (file_path ))
45
+ logging .info (f"upload_file(): File '{ Config .PDF_DOCUMENT_NAME } ' uploaded successfully." )
46
+ except Exception as e :
47
+ logging .error (f"upload_document(): Failed to upload file: { e } " )
48
+
49
+ def download_result (self ):
50
+ """ Download the processed PDF document from the Aspose Cloud server. """
51
+ if self .pdf_api :
52
+ try :
53
+ temp_file = self .pdf_api .download_file (Config .PDF_DOCUMENT_NAME )
54
+ local_path = Config .LOCAL_FOLDER / Config .LOCAL_RESULT_DOCUMENT_NAME
55
+ shutil .move (temp_file , str (local_path ))
56
+ logging .info (f"download_result(): File successfully downloaded: { local_path } " )
57
+ except Exception as e :
58
+ logging .error (f"download_result(): Failed to download file: { e } " )
59
+
60
+ def encrypt_document (self ):
61
+ """Encrypt the PDF document."""
62
+ if self .pdf_api :
63
+ try :
64
+ user_password_encoded = base64 .b64encode (bytes (Config .USER_PASSWORD , encoding = 'utf-8' ))
65
+
66
+ owner_password_encoded = base64 .b64encode (bytes (Config .OWNER_PASSWORD , encoding = 'utf-8' ))
67
+
68
+ response = self .pdf_api .post_encrypt_document_in_storage (Config .PDF_DOCUMENT_NAME , user_password_encoded , owner_password_encoded , Config .ENCRYPT_ALGORITHM )
69
+ if response .code == 200 :
70
+ logging .info (f"encrypt_document(): Document #{ Config .PDF_DOCUMENT_NAME } successfully encrypted." )
71
+ else :
72
+ logging .error (f"encrypt_document(): Failed to encrypt document #{ Config .PDF_DOCUMENT_NAME } . Response code: { response .code } " )
73
+ except Exception as e :
74
+ logging .error (f"aencrypt_document(): Error while encrypted document: { e } " )
75
+
76
+
77
+ if __name__ == "__main__" :
78
+ pdf_encrypt = pdfEncryption ()
79
+ pdf_encrypt .upload_document ()
80
+ pdf_encrypt .encrypt_document ()
81
+ pdf_encrypt .download_result ()
0 commit comments