1
+ import shutil
2
+ import json
3
+ import logging
4
+ from pathlib import Path
5
+ from asposepdfcloud import ApiClient , PdfApi , SplitRangePdfOptions , PageRange , SplitResultResponse
6
+
7
+ # Configure logging
8
+ logging .basicConfig (level = logging .INFO , format = "%(asctime)s - %(levelname)s - %(message)s" )
9
+
10
+ class Config :
11
+ """Configuration parameters."""
12
+ CREDENTIALS_FILE = Path (r"C:\\Projects\\ASPOSE\\Pdf.Cloud\\Credentials\\credentials.json" )
13
+ LOCAL_FOLDER = Path (r"C:\Samples" )
14
+ PDF_DOCUMENT_NAME = "sample.pdf"
15
+
16
+ spltRanges : SplitRangePdfOptions = SplitRangePdfOptions ([PageRange (1 ,3 ), PageRange (4 ,7 )])
17
+
18
+ class PdfSplitter :
19
+ """ Class for managing PDF pages using Aspose PDF Cloud API. """
20
+ def __init__ (self , credentials_file : Path = Config .CREDENTIALS_FILE ):
21
+ self .pdf_api = None
22
+ self ._init_api (credentials_file )
23
+
24
+ def _init_api (self , credentials_file : Path ):
25
+ """ Initialize the API client. """
26
+ try :
27
+ with credentials_file .open ("r" , encoding = "utf-8" ) as file :
28
+ credentials = json .load (file )
29
+ api_key , app_id = credentials .get ("key" ), credentials .get ("id" )
30
+ if not api_key or not app_id :
31
+ raise ValueError ("init_api(): Error: Missing API keys in the credentials file." )
32
+ self .pdf_api = PdfApi (ApiClient (api_key , app_id ))
33
+ except (FileNotFoundError , json .JSONDecodeError , ValueError ) as e :
34
+ logging .error (f"init_api(): Failed to load credentials: { e } " )
35
+
36
+ def upload_document (self ):
37
+ """ Upload a PDF document to the Aspose Cloud server. """
38
+ if self .pdf_api :
39
+ file_path = Config .LOCAL_FOLDER / Config .PDF_DOCUMENT_NAME
40
+ try :
41
+ self .pdf_api .upload_file (Config .PDF_DOCUMENT_NAME , str (file_path ))
42
+ logging .info (f"upload_document(): File { Config .PDF_DOCUMENT_NAME } uploaded successfully." )
43
+ except Exception as e :
44
+ logging .error (f"upload_document(): Failed to upload file: { e } " )
45
+
46
+ def download_result (self , pageName , pageIndex ):
47
+ """ Download the processed PDF document page from the Aspose Cloud server. """
48
+ if self .pdf_api :
49
+ try :
50
+ temp_file = self .pdf_api .download_file (pageName )
51
+ local_path = Config .LOCAL_FOLDER / f"Page_{ pageIndex } _{ pageName } "
52
+ shutil .move (temp_file , str (local_path ))
53
+ logging .info (f"download_result(): File successfully downloaded: { local_path } " )
54
+ except Exception as e :
55
+ logging .error (f"download_result(): Failed to download file: { e } " )
56
+
57
+ def split_document_by_ranges (self ):
58
+ """ Split source document by ranges """
59
+ if self .pdf_api :
60
+ try :
61
+ response : SplitResultResponse = self .pdf_api .post_split_range_pdf_document (Config .PDF_DOCUMENT_NAME , spltRanges )
62
+
63
+ if response .code == 200 :
64
+ i = 0
65
+ for a_doc in response .result .documents :
66
+ self .download_result (a_doc .href , i )
67
+ i = i + 1
68
+ except Exception as e :
69
+ logging .error (f"split_document_by_pages(): Failed in the PdfSplitter: { e } " )
70
+
71
+ if __name__ == "__main__" :
72
+ pdf_splitter = PdfSplitter ()
73
+ pdf_splitter .upload_document ()
74
+ pdf_splitter .split_document_by_ranges ()
0 commit comments