Skip to content

Commit 7fc4e86

Browse files
PDFCLOUD-4980: added snippets for Compare
1 parent 4a4e9e9 commit 7fc4e86

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import path from 'node:path';
2+
import { pdfComparesHelper, pdfApi } from "../Compares/comparesHelper.js";
3+
4+
export const configParams = {
5+
LOCAL_FOLDER: "C:\\Samples\\",
6+
REMOTE_FOLDER: "Your_Temp_Pdf_Cloud",
7+
PDF_DOCUMENT_1: "sample_compare_1.pdf",
8+
PDF_DOCUMENT_2: "sample_compare_2.pdf",
9+
PDF_OUTPUT: "output_compare.pdf",
10+
REMOTE_FOLDER: "Your_Temp_Pdf_Cloud",
11+
};
12+
13+
export const pdfCompares = {
14+
async comparePdfDocuments(document1, document2, output_document) {
15+
await pdfComparesHelper.uploadFile(document1, configParams.LOCAL_FOLDER, configParams.REMOTE_FOLDER);
16+
await pdfComparesHelper.uploadFile(document2, configParams.LOCAL_FOLDER, configParams.REMOTE_FOLDER);
17+
18+
const remotePdf1 = path.join(configParams.REMOTE_FOLDER, document1);
19+
const remotePdf2 = path.join(configParams.REMOTE_FOLDER, document2);
20+
var remotePdfOut = path.join(configParams.REMOTE_FOLDER, output_document);
21+
22+
const response = await pdfApi.postComparePdf(remotePdf1, remotePdf2, remotePdfOut);
23+
24+
if (response.body.code != 200) {
25+
console.log("Unexpected error!");
26+
}
27+
else{
28+
console.log("Compare was successful!y finished in '" + output_document + "' file.");
29+
await pdfComparesHelper.downloadResult(output_document, configParams.LOCAL_FOLDER, configParams.REMOTE_FOLDER);
30+
}
31+
32+
}
33+
34+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import credentials from "../../../Credentials/credentials.json" with { type: "json" };
2+
import fs from 'node:fs/promises';
3+
import path from 'node:path';
4+
import { PdfApi } from "../../src/api/api.js";
5+
6+
export const pdfApi = new PdfApi(credentials.id, credentials.key);
7+
8+
export const pdfComparesHelper = {
9+
async uploadFile (fileName, localFolder, remoteFolder) {
10+
const localFilePath = path.join(localFolder, fileName);
11+
const fileData = await fs.readFile(localFilePath);
12+
const remoteFilePath = path.join(remoteFolder, fileName);
13+
await pdfApi.uploadFile(remoteFilePath, fileData);
14+
console.log("Uploaded: " + fileName);
15+
},
16+
17+
async downloadResult (fileName, localFolder, remoteFolder) {
18+
const remoteFilePath = path.join(remoteFolder, fileName);
19+
const changedPdfData = await pdfApi.downloadFile(remoteFilePath);
20+
const localFilePath = path.join(localFolder, fileName);
21+
await fs.writeFile(localFilePath, changedPdfData.body);
22+
console.log("Downloaded: " + localFilePath);
23+
},
24+
25+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { pdfCompares, configParams } from "../Compares/comparePdfDocuments.js";
2+
3+
async function main() {
4+
try {
5+
await pdfCompares.comparePdfDocuments(configParams.PDF_DOCUMENT_1, configParams.PDF_DOCUMENT_2, configParams.PDF_OUTPUT);
6+
} catch (error) {
7+
console.error("Error:", error.message);
8+
}
9+
}
10+
11+
await main();

0 commit comments

Comments
 (0)