Skip to content

Commit 81ee7a4

Browse files
Update appendImageFooter.py
1 parent 2187bc9 commit 81ee7a4

File tree

1 file changed

+51
-66
lines changed

1 file changed

+51
-66
lines changed

Uses-Cases/HeaderFooter/appendImageFooter.py

Lines changed: 51 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -35,89 +35,74 @@ def _init_api(self, credentials_file: Path):
3535
except (FileNotFoundError, json.JSONDecodeError, ValueError) as e:
3636
logging.error(f"init_api(): Failed to load credentials: {e}")
3737

38-
def _ensure_api_initialized(self):
39-
"""Check if the API is initialized before making API calls."""
40-
if not self.pdf_api:
41-
logging.error("ensure_api_initialized(): PDF API is not initialized. Operation aborted.")
42-
return False
43-
return True
44-
4538
def upload_file(self, fileName: str):
4639
""" Upload a local fileName to the Aspose Cloud server. """
47-
if not self._ensure_api_initialized():
48-
return
49-
50-
file_path = Config.LOCAL_FOLDER / fileName
51-
try:
52-
self.pdf_api.upload_file(fileName, str(file_path))
53-
logging.info(f"upload_file(): File '{fileName}' uploaded successfully.")
54-
except Exception as e:
55-
logging.error(f"upload_document(): Failed to upload file: {e}")
40+
if self.pdf_api:
41+
file_path = Config.LOCAL_FOLDER / fileName
42+
try:
43+
self.pdf_api.upload_file(fileName, str(file_path))
44+
logging.info(f"upload_file(): File '{fileName}' uploaded successfully.")
45+
except Exception as e:
46+
logging.error(f"upload_document(): Failed to upload file: {e}")
5647

5748
def upload_document(self):
5849
""" Upload a PDF document to the Aspose Cloud server. """
5950
self.upload_file(Config.PDF_DOCUMENT_NAME)
6051

6152
def download_result(self):
6253
""" Download the processed PDF document from the Aspose Cloud server. """
63-
if not self._ensure_api_initialized():
64-
return
65-
66-
try:
67-
temp_file = self.pdf_api.download_file(Config.PDF_DOCUMENT_NAME)
68-
local_path = Config.LOCAL_FOLDER / Config.LOCAL_RESULT_DOCUMENT_NAME
69-
shutil.move(temp_file, str(local_path))
70-
logging.info(f"download_result(): File successfully downloaded: {local_path}")
71-
except Exception as e:
72-
logging.error(f"download_result(): Failed to download file: {e}")
54+
if self.pdf_api:
55+
try:
56+
temp_file = self.pdf_api.download_file(Config.PDF_DOCUMENT_NAME)
57+
local_path = Config.LOCAL_FOLDER / Config.LOCAL_RESULT_DOCUMENT_NAME
58+
shutil.move(temp_file, str(local_path))
59+
logging.info(f"download_result(): File successfully downloaded: {local_path}")
60+
except Exception as e:
61+
logging.error(f"download_result(): Failed to download file: {e}")
7362

7463
def append_image_footer(self):
7564
"""Append a new image footer to the PDF document."""
76-
if not self._ensure_api_initialized():
77-
return
78-
79-
new_footer = ImageFooter(
80-
background = True,
81-
horizontal_alignment = HorizontalAlignment.CENTER,
82-
file_name = Config.IMAGE_FOOTER_FILE,
83-
width = 24,
84-
height = 24
85-
)
86-
87-
try:
88-
response = self.pdf_api.post_document_image_footer(
89-
Config.PDF_DOCUMENT_NAME, new_footer
65+
if self.pdf_api:
66+
new_footer = ImageFooter(
67+
background = True,
68+
horizontal_alignment = HorizontalAlignment.CENTER,
69+
file_name = Config.IMAGE_FOOTER_FILE,
70+
width = 24,
71+
height = 24
9072
)
91-
if response.code == 200:
92-
logging.info(f"append_image_footer(): Footer '{new_footer.file_name}' added to the document #{Config.PDF_DOCUMENT_NAME}.")
93-
else:
94-
logging.error(f"append_image_footer(): Failed to add footer '{new_footer.file_name}' to the document #{Config.PDF_DOCUMENT_NAME}. Response code: {response.code}")
95-
except Exception as e:
96-
logging.error(f"append_image_footer(): Error while adding footer: {e}")
73+
74+
try:
75+
response = self.pdf_api.post_document_image_footer(
76+
Config.PDF_DOCUMENT_NAME, new_footer
77+
)
78+
if response.code == 200:
79+
logging.info(f"append_image_footer(): Footer '{new_footer.file_name}' added to the document #{Config.PDF_DOCUMENT_NAME}.")
80+
else:
81+
logging.error(f"append_image_footer(): Failed to add footer '{new_footer.file_name}' to the document #{Config.PDF_DOCUMENT_NAME}. Response code: {response.code}")
82+
except Exception as e:
83+
logging.error(f"append_image_footer(): Error while adding footer: {e}")
9784

9885
def append_image_footer_page(self):
9986
"""Append a new image footer on the page in PDF document."""
100-
if not self._ensure_api_initialized():
101-
return
102-
103-
new_footer = ImageFooter(
104-
background = True,
105-
horizontal_alignment = HorizontalAlignment.RIGHT,
106-
file_name = Config.IMAGE_FOOTER_FILE,
107-
width = 24,
108-
height = 24
109-
)
110-
111-
try:
112-
response = self.pdf_api.post_document_image_footer(
113-
Config.PDF_DOCUMENT_NAME, new_footer, start_page_number=Config.PAGE_NUMBER, end_page_number=Config.PAGE_NUMBER
87+
if self.pdf_api:
88+
new_footer = ImageFooter(
89+
background = True,
90+
horizontal_alignment = HorizontalAlignment.RIGHT,
91+
file_name = Config.IMAGE_FOOTER_FILE,
92+
width = 24,
93+
height = 24
11494
)
115-
if response.code == 200:
116-
logging.info(f"append_image_footer_page(): Footer '{new_footer.file_name}' added to the page #{Config.PAGE_NUMBER}.")
117-
else:
118-
logging.error(f"append_image_footer_page(): Failed to add footer '{new_footer.file_name}' to the document #{Config.PAGE_NUMBER}. Response code: {response.code}")
119-
except Exception as e:
120-
logging.error(f"append_image_footer_page(): Error while adding footer: {e}")
95+
96+
try:
97+
response = self.pdf_api.post_document_image_footer(
98+
Config.PDF_DOCUMENT_NAME, new_footer, start_page_number=Config.PAGE_NUMBER, end_page_number=Config.PAGE_NUMBER
99+
)
100+
if response.code == 200:
101+
logging.info(f"append_image_footer_page(): Footer '{new_footer.file_name}' added to the page #{Config.PAGE_NUMBER}.")
102+
else:
103+
logging.error(f"append_image_footer_page(): Failed to add footer '{new_footer.file_name}' to the document #{Config.PAGE_NUMBER}. Response code: {response.code}")
104+
except Exception as e:
105+
logging.error(f"append_image_footer_page(): Error while adding footer: {e}")
121106

122107

123108
if __name__ == "__main__":

0 commit comments

Comments
 (0)