|
| 1 | +import shutil |
| 2 | +import json |
| 3 | +import logging |
| 4 | +from pathlib import Path |
| 5 | +from asposepdfcloud import ApiClient, PdfApi |
| 6 | + |
| 7 | +# Configure logging |
| 8 | +logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s") |
| 9 | + |
| 10 | + |
| 11 | +class Config: |
| 12 | + """Configuration parameters.""" |
| 13 | + CREDENTIALS_FILE = Path(r"..s\\credentials.json") |
| 14 | + LOCAL_FOLDER = Path(r"C:\Samples") |
| 15 | + REMOTE_TEMP_FOLDER = "TempPdfCloud" |
| 16 | + PDF_DOCUMENT_NAME = "sample.pdf" |
| 17 | + XML_OUTPUT_FILE = "output_sample.xml" |
| 18 | + FDF_OUTPUT_FILE = "output_sample.fdf" |
| 19 | + LOCAL_RESULT_DOCUMENT_NAME = "output_sample.pdf" |
| 20 | + PAGE_NUMBER = 1 |
| 21 | + |
| 22 | + |
| 23 | +class ParesrHelper: |
| 24 | + """Class with helper methods and properties for Parser""" |
| 25 | + |
| 26 | + def __init__(self, credentials_file: Path = Config.CREDENTIALS_FILE): |
| 27 | + self.pdf_api = None |
| 28 | + self._init_api(credentials_file) |
| 29 | + |
| 30 | + def _init_api(self, credentials_file: Path): |
| 31 | + """Initialize the API client.""" |
| 32 | + try: |
| 33 | + with credentials_file.open("r", encoding="utf-8") as file: |
| 34 | + credentials = json.load(file) |
| 35 | + api_key, app_id = credentials.get("key"), credentials.get("id") |
| 36 | + if not api_key or not app_id: |
| 37 | + raise ValueError("Error: Missing API keys in the credentials file.") |
| 38 | + self.pdf_api = PdfApi(ApiClient(api_key, app_id)) |
| 39 | + except (FileNotFoundError, json.JSONDecodeError, ValueError) as e: |
| 40 | + logging.error(f"Failed to load credentials: {e}") |
| 41 | + |
| 42 | + def upload_document(self, documentName: str, remoteFolder: str): |
| 43 | + """Upload a PDF document to the Aspose Cloud server.""" |
| 44 | + if self.pdf_api: |
| 45 | + file_path = Config.LOCAL_FOLDER / documentName |
| 46 | + try: |
| 47 | + if remoteFolder == None: |
| 48 | + self.pdf_api.upload_file(documentName, str(file_path)) |
| 49 | + else: |
| 50 | + opts = { "folder": remoteFolder } |
| 51 | + self.pdf_api.upload_file(remoteFolder + '/' + documentName, file_path) |
| 52 | + logging.info(f"File {documentName} uploaded successfully.") |
| 53 | + except Exception as e: |
| 54 | + logging.error(f"Failed to upload file: {e}") |
| 55 | + |
| 56 | + def downloadFile(self, document: str, outputDocument: str, localFolder: Path, remoteFolder: str, output_prefix: str): |
| 57 | + """Download the processed PDF document from the Aspose Cloud server.""" |
| 58 | + if self.pdf_api: |
| 59 | + try: |
| 60 | + temp_file = self.pdf_api.download_file(remoteFolder + '/' + document) |
| 61 | + local_path = localFolder / ( output_prefix + outputDocument ) |
| 62 | + shutil.move(temp_file, str(local_path)) |
| 63 | + logging.info(f"download_result(): File successfully downloaded: {local_path}") |
| 64 | + except Exception as e: |
| 65 | + logging.error(f"download_result(): Failed to download file: {e}") |
| 66 | + |
0 commit comments