Skip to content

Commit f1faf4c

Browse files
PDFCLOUD-4979: added snippets for Compare
1 parent e3e04bd commit f1faf4c

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import logging
2+
import os
3+
from pathlib import Path
4+
from asposepdfcloud import PdfApi
5+
from compares_helper import CompareMain
6+
7+
class Config:
8+
"""Configuration parameters."""
9+
CREDENTIALS_FILE = Path(r"C:\\Projects\\ASPOSE\\Pdf.Cloud\\Credentials\\credentials.json")
10+
LOCAL_FOLDER = Path(r"C:\Samples")
11+
REMOTE_FOLDER = "Your_Temp_Pdf_Cloud"
12+
PDF_DOCUMENT_1 = "sample_compare_1.pdf"
13+
PDF_DOCUMENT_2 = "sample_compare_2.pdf"
14+
PDF_OUTPUT = "output_compare.pdf"
15+
16+
class ComparePdfDocuments:
17+
def __init__(self, pdf_api: PdfApi, compare_main: CompareMain):
18+
self.pdfApi = pdf_api
19+
self.compareMain = compare_main
20+
21+
def compareDocument(self):
22+
self.compareMain.upload_file(Config.PDF_DOCUMENT_1, Config.LOCAL_FOLDER)
23+
self.compareMain.upload_file(Config.PDF_DOCUMENT_2, Config.LOCAL_FOLDER)
24+
try:
25+
response = self.pdfApi.post_compare_pdf(Config.PDF_DOCUMENT_1, Config.PDF_DOCUMENT_2, Config.PDF_OUTPUT)
26+
if response.code == 200:
27+
logging.info(f"compareDocument(): Succsessfully compared.")
28+
self.compareMain.download_result(Config.PDF_OUTPUT, Config.PDF_OUTPUT, Config.LOCAL_FOLDER)
29+
else:
30+
logging.error(f"compareDocument(): Failed to compare Pdf documents! Response code: {response.code}")
31+
except Exception as e:
32+
logging.error(f"compareDocument(): Error while comparing Pdf documents: {e}")
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import os
2+
import shutil
3+
import json
4+
import logging
5+
from pathlib import Path
6+
from asposepdfcloud import ApiClient, PdfApi
7+
8+
# Configure logging
9+
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
10+
11+
class CompareMain:
12+
def __init__(self, credentials_file: Path):
13+
self.pdf_api = None
14+
self._init_api(credentials_file)
15+
16+
def _init_api(self, credentials_file: Path):
17+
"""Initialize the API client."""
18+
try:
19+
with credentials_file.open("r", encoding="utf-8") as file:
20+
credentials = json.load(file)
21+
api_key, app_id = credentials.get("key"), credentials.get("id")
22+
if not api_key or not app_id:
23+
raise ValueError("Error: Missing API keys in the credentials file.")
24+
self.pdf_api = PdfApi(ApiClient(api_key, app_id))
25+
except (FileNotFoundError, json.JSONDecodeError, ValueError) as e:
26+
logging.error(f"Failed to load credentials: {e}")
27+
28+
def upload_file(self, fileName: str, localFolder: Path):
29+
""" Upload a local fileName to the Aspose Cloud server. """
30+
if self.pdf_api:
31+
file_path = localFolder / fileName
32+
try:
33+
self.pdf_api.upload_file(fileName, str(file_path))
34+
logging.info(f"upload_file(): File '{fileName}' uploaded successfully.")
35+
except Exception as e:
36+
logging.error(f"upload_document(): Failed to upload file: {e}")
37+
38+
def download_result(self, document: str, outputDocument: str, localFolder: Path):
39+
"""Download the processed PDF document from the Aspose Cloud server."""
40+
if self.pdf_api:
41+
try:
42+
temp_file = self.pdf_api.download_file(document)
43+
local_path = localFolder / outputDocument
44+
shutil.move(temp_file, str(local_path))
45+
logging.info(f"download_result(): File successfully downloaded: {local_path}")
46+
except Exception as e:
47+
logging.error(f"download_result(): Failed to download file: {e}")
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from compares_helper import CompareMain
2+
from compare_pdf_documents import Config, ComparePdfDocuments
3+
4+
if __name__ == "__main__":
5+
comparator = CompareMain(Config.CREDENTIALS_FILE)
6+
pdf_compares = ComparePdfDocuments(comparator.pdf_api, comparator)
7+
pdf_compares.compareDocument()

0 commit comments

Comments
 (0)