|
| 1 | +import shutil |
| 2 | +import json |
| 3 | +import logging |
| 4 | +from pathlib import Path |
| 5 | +from asposepdfcloud import ApiClient, PdfApi, Color, Bookmark |
| 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 | + NEW_BOOKMARK_TITLE = "• Increased performance.." #"• Productivity improvement" |
| 17 | + PARENT_BOOKMARK_FOR_APPEND = "" #The parent bookmark path. Specify an empty string when adding a bookmark to the root. |
| 18 | + NEW_BOOKMARK_PAGE_NUMBER = 3 |
| 19 | + BOOKMARK_PAGE_POSITION_X = 89 |
| 20 | + BOOKMARK_PAGE_POSITION_Y = 564 |
| 21 | + |
| 22 | + |
| 23 | +class PdfBookmarks: |
| 24 | + """Class for managing PDF bokkmarks using Aspose PDF Cloud API.""" |
| 25 | + def __init__(self, credentials_file: Path = Config.CREDENTIALS_FILE): |
| 26 | + self.pdf_api = None |
| 27 | + self._init_api(credentials_file) |
| 28 | + |
| 29 | + def _init_api(self, credentials_file: Path): |
| 30 | + """Initialize the API client.""" |
| 31 | + try: |
| 32 | + with credentials_file.open("r", encoding="utf-8") as file: |
| 33 | + credentials = json.load(file) |
| 34 | + api_key, app_id = credentials.get("key"), credentials.get("id") |
| 35 | + if not api_key or not app_id: |
| 36 | + raise ValueError("init_api(): Error: Missing API keys in the credentials file.") |
| 37 | + self.pdf_api = PdfApi(ApiClient(api_key, app_id)) |
| 38 | + except (FileNotFoundError, json.JSONDecodeError, ValueError) as e: |
| 39 | + logging.error(f"init_api(): Failed to load credentials: {e}") |
| 40 | + |
| 41 | + def upload_document(self): |
| 42 | + """Upload a PDF document to the Aspose Cloud server.""" |
| 43 | + if self.pdf_api: |
| 44 | + file_path = Config.LOCAL_FOLDER / Config.PDF_DOCUMENT_NAME |
| 45 | + try: |
| 46 | + self.pdf_api.upload_file(Config.PDF_DOCUMENT_NAME, str(file_path)) |
| 47 | + logging.info(f"upload_document(): File {Config.PDF_DOCUMENT_NAME} uploaded successfully.") |
| 48 | + except Exception as e: |
| 49 | + logging.error(f"upload_document(): Failed to upload file: {e}") |
| 50 | + |
| 51 | + def download_result(self): |
| 52 | + """Download the processed PDF document from the Aspose Cloud server.""" |
| 53 | + if self.pdf_api: |
| 54 | + try: |
| 55 | + temp_file = self.pdf_api.download_file(Config.PDF_DOCUMENT_NAME) |
| 56 | + local_path = Config.LOCAL_FOLDER / Config.LOCAL_RESULT_DOCUMENT_NAME |
| 57 | + shutil.move(temp_file, str(local_path)) |
| 58 | + logging.info(f"download_result(): File successfully downloaded: {local_path}") |
| 59 | + except Exception as e: |
| 60 | + logging.error(f"download_result(): Failed to download file: {e}") |
| 61 | + |
| 62 | + def append_bookmark_link(self): |
| 63 | + """Append a new bookmark link to a specific page in the PDF document.""" |
| 64 | + if self.pdf_api: |
| 65 | + newBookmark = Bookmark( |
| 66 | + title = Config.NEW_BOOKMARK_TITLE, |
| 67 | + italic = True, |
| 68 | + bold = True, |
| 69 | + color = Color(a=255,r=0,g=255,b=0), |
| 70 | + level = 1, |
| 71 | + page_display_left = Config.BOOKMARK_PAGE_POSITION_X, |
| 72 | + page_display_top = Config.BOOKMARK_PAGE_POSITION_Y, |
| 73 | + page_display_zoom = 2, |
| 74 | + page_number = Config.NEW_BOOKMARK_PAGE_NUMBER |
| 75 | + ) |
| 76 | + |
| 77 | + try: |
| 78 | + response = self.pdf_api.post_bookmark( |
| 79 | + Config.PDF_DOCUMENT_NAME, Config.PARENT_BOOKMARK_FOR_APPEND, [newBookmark] |
| 80 | + ) |
| 81 | + if response.code == 200: |
| 82 | + logging.info(f"append_bookmark_link(): Bookmark '{response.bookmarks.list[0].action}'->'{Config.NEW_BOOKMARK_TITLE}' added to page #{Config.NEW_BOOKMARK_PAGE_NUMBER}.") |
| 83 | + else: |
| 84 | + logging.error(f"append_bookmark_link(): Failed to add bookmark '{Config.NEW_BOOKMARK_TITLE}' to the page #{Config.NEW_BOOKMARK_PAGE_NUMBER}. Response code: {response.code}") |
| 85 | + except Exception as e: |
| 86 | + logging.error(f"append_bookmark_link(): Error while adding bookmark: {e}") |
| 87 | + |
| 88 | + |
| 89 | +if __name__ == "__main__": |
| 90 | + pdf_bookmarks = PdfBookmarks() |
| 91 | + pdf_bookmarks.upload_document() |
| 92 | + pdf_bookmarks.append_bookmark_link() |
| 93 | + pdf_bookmarks.download_result() |
0 commit comments