|
| 1 | +import shutil |
| 2 | +import json |
| 3 | +import logging |
| 4 | +from pathlib import Path |
| 5 | +from asposepdfcloud import ApiClient, PdfApi, Stamp, AsposeResponse, HorizontalAlignment, StampType |
| 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 | + LOCAL_RESULT_DOCUMENT_NAME = "output_sample.pdf" |
| 16 | + PAGE_NUMBER = 2 |
| 17 | + STAMP_TEXT = "NEW TEXT STAMP" |
| 18 | + |
| 19 | +class PdfPages: |
| 20 | + """ Class for managing PDF pages using Aspose PDF Cloud API. """ |
| 21 | + def __init__(self, credentials_file: Path = Config.CREDENTIALS_FILE): |
| 22 | + self.pdf_api = None |
| 23 | + self._init_api(credentials_file) |
| 24 | + |
| 25 | + def _init_api(self, credentials_file: Path): |
| 26 | + """ Initialize the API client. """ |
| 27 | + try: |
| 28 | + with credentials_file.open("r", encoding="utf-8") as file: |
| 29 | + credentials = json.load(file) |
| 30 | + api_key, app_id = credentials.get("key"), credentials.get("id") |
| 31 | + if not api_key or not app_id: |
| 32 | + raise ValueError("init_api(): Error: Missing API keys in the credentials file.") |
| 33 | + self.pdf_api = PdfApi(ApiClient(api_key, app_id)) |
| 34 | + except (FileNotFoundError, json.JSONDecodeError, ValueError) as e: |
| 35 | + logging.error(f"init_api(): Failed to load credentials: {e}") |
| 36 | + |
| 37 | + def upload_document(self): |
| 38 | + """ Upload a PDF document to the Aspose Cloud server. """ |
| 39 | + if self.pdf_api: |
| 40 | + file_path = Config.LOCAL_FOLDER / Config.PDF_DOCUMENT_NAME |
| 41 | + try: |
| 42 | + self.pdf_api.upload_file(Config.PDF_DOCUMENT_NAME, str(file_path)) |
| 43 | + logging.info(f"upload_document(): File {Config.PDF_DOCUMENT_NAME} uploaded successfully.") |
| 44 | + except Exception as e: |
| 45 | + logging.error(f"upload_document(): Failed to upload file: {e}") |
| 46 | + |
| 47 | + def download_result(self): |
| 48 | + """ Download the processed PDF document from the Aspose Cloud server. """ |
| 49 | + if self.pdf_api: |
| 50 | + try: |
| 51 | + temp_file = self.pdf_api.download_file(Config.PDF_DOCUMENT_NAME) |
| 52 | + local_path = Config.LOCAL_FOLDER / Config.LOCAL_RESULT_DOCUMENT_NAME |
| 53 | + shutil.move(temp_file, str(local_path)) |
| 54 | + logging.info(f"download_result(): File successfully downloaded: {local_path}") |
| 55 | + except Exception as e: |
| 56 | + logging.error(f"download_result(): Failed to download file: {e}") |
| 57 | + |
| 58 | + def add_page_text_stamp(self): |
| 59 | + """ Adds a text stamp to a specific page in a PDF document. """ |
| 60 | + if self.pdf_api: |
| 61 | + page_stamp: Stamp = Stamp( |
| 62 | + type = StampType.TEXT, |
| 63 | + background = True, |
| 64 | + horizontal_alignment = HorizontalAlignment.CENTER, |
| 65 | + text_alignment = HorizontalAlignment.CENTER, |
| 66 | + value = Config.STAMP_TEXT, |
| 67 | + page_index = Config.PAGE_NUMBER, |
| 68 | + ) |
| 69 | + |
| 70 | + response: AsposeResponse = self.pdf_api.put_page_add_stamp(Config.PDF_DOCUMENT_NAME, Config.PAGE_NUMBER, page_stamp) |
| 71 | + |
| 72 | + if response.code == 200: |
| 73 | + logging.info(f"Text stamp '{Config.STAMP_TEXT}' added to page #{Config.PAGE_NUMBER}.") |
| 74 | + else: |
| 75 | + logging.error(f"Failed to add text stamp '{Config.STAMP_TEXT}' to page #{Config.PAGE_NUMBER}.") |
| 76 | + |
| 77 | +if __name__ == "__main__": |
| 78 | + pdf_pages = PdfPages() |
| 79 | + pdf_pages.upload_document() |
| 80 | + pdf_pages.add_page_text_stamp() |
| 81 | + pdf_pages.download_result() |
0 commit comments