From 30095598407dadc1c1c9a8a02ece7d32db78079e Mon Sep 17 00:00:00 2001 From: Dmitriy-Xawstov Date: Tue, 21 Jan 2025 14:49:41 +0200 Subject: [PATCH 1/4] attachments-uses-cases --- UsesCases/Attachments/add/add.js | 70 +++++++++++++++++++++++++++ UsesCases/Attachments/get/get.js | 57 ++++++++++++++++++++++ UsesCases/Attachments/get_download.js | 45 +++++++++++++++++ 3 files changed, 172 insertions(+) create mode 100644 UsesCases/Attachments/add/add.js create mode 100644 UsesCases/Attachments/get/get.js create mode 100644 UsesCases/Attachments/get_download.js diff --git a/UsesCases/Attachments/add/add.js b/UsesCases/Attachments/add/add.js new file mode 100644 index 00000000..a9fe5cf1 --- /dev/null +++ b/UsesCases/Attachments/add/add.js @@ -0,0 +1,70 @@ +/***************************************************************************************************************** + 1. Load your Application Secret and Key from the JSON file or set credentials in another way + 2. Create an object to connect to the Pdf.Cloud API + 3. Create an AttachmentInfo object, which maust be inserted into Pdf file + 4. Perform append attachment into Pdf document using postAddDocumentAttachment() function and (AttachmentInfo) + object [https://github.com/aspose-pdf-cloud/aspose-pdf-cloud-node.js/blob/master/docs/AttachmentInfo.md] + 5. Check result and perform some actions with result.body object in + in format (Attachment)[https://github.com/aspose-pdf-cloud/aspose-pdf-cloud-node.js/blob/master/docs/Attachment.md] + + All values of variables starting with "YOUR_****" should be replaced by real user values +*********************************************************************************************************/ + +import credentials from "./credentials.json" with { type: "json" }; +import fs from 'node:fs/promises'; +import { PdfApi } from "asposepdfcloud"; + +const LOCAL_PATH = "C:\\Samples\\"; +const FILE_NAME = "Sample-Document-01"; +const LOCAL_FILE_NAME = LOCAL_PATH + FILE_NAME + ".pdf"; +const ATTACHMENT_IMDEX = 1; + +const STORAGE_FILE_NAME = FILE_NAME + ".pdf"; + +async function ProcessPdfAttachments(attachments) +{ + if (attachments != null && attachments != undefined) + { + await Promise.all( + attachments.map(async (attachment, index) => { + const downloadRes = await pdfApi.downloadFile(attachment.href); + await fs.writeFile( LOCAL_PATH + FILE_NAME_SHORT + "_attachment_" + (index + 1) + ".data", downloadRes.body); + }) + ); + } + else + console.log("Unexpected error : can't download attachments"); +} + +const addAttachment = async function (atName, atPath, atDescription, atMIME) +{ + try { + + const pdfApi = new PdfApi(credentials.id, credentials.key); + + const buffer = await fs.readFile(LOCAL_FILE_NAME); + await pdfApi.uploadFile(STORAGE_FILE_NAME, buffer); + + const attachment = new AttachmentInfo(); + attachment.name = atName; + attachment.path = atPath; + attachment.description = atDescription; + attachment.mimeType = atMIME; + + const appendResult = await pdfApi.postAddDocumentAttachment(STORAGE_FILE_NAME, attachment, null, null); + + if (appendResult.statusCode == 200) + { + const attachments = appendResult.body; + + await ProcessPdfAttachments(attachments); + } + else + console.log("Unexpected error : can't download attachments"); + + } catch (error) { + console.error(error.message); + } +} + +await addAttachment("YOUR_ATTACHMENT_NAME", "YOUR_ATTACHMENT_PATH", "YOUR_ATTACHMENT_DESCRIPTION", "type/YOUR_SUBTYPE"); \ No newline at end of file diff --git a/UsesCases/Attachments/get/get.js b/UsesCases/Attachments/get/get.js new file mode 100644 index 00000000..792311e1 --- /dev/null +++ b/UsesCases/Attachments/get/get.js @@ -0,0 +1,57 @@ +/************************************************************************************************** + 1. Load your Application Secret and Key from the JSON file or set credentials in another way + 2. Create a Storage API object for uploading Pdf file + 3. Create an object to connect to the Pdf.Cloud API + 4. Upload your document file using PutCreate() function of the Storage API object + 5. To extract attachments from a PDF document, using getDocumentAttachments() function + 6. Check result and perform some actions with result.body object + in format (Attachments)[https://github.com/aspose-pdf-cloud/aspose-pdf-cloud-node.js/blob/master/docs/Attachments.md] + + All values of variables starting with "YOUR_****" should be replaced by real user values +****************************************************************************************************/ +import credentials from "./credentials.json" with { type: "json" }; +import fs from 'node:fs/promises'; +import { PdfApi } from "asposepdfcloud"; + +const LOCAL_PATH = "C:\\Samples\\"; +const FILE_NAME_SHORT = "Sample-Document"; +const FILE_NAME = FILE_NAME_SHORT + ".pdf"; + +const pdfApi = new PdfApi(credentials.id, credentials.key); + +async function ProcessPdfAttachments(attachments) +{ + if (attachments != null && attachments != undefined) + { + await Promise.all( + attachments.map(async (attachment, index) => { + const downloadRes = await pdfApi.downloadFile(attachment.href); + await fs.writeFile( LOCAL_PATH + FILE_NAME_SHORT + "_attachment_" + (index + 1) + ".data", downloadRes.body); + }) + ); + } + else + console.log("Unexpected error : can't download attachments"); +} + +async function getPdfDocumentAttachments(api, pdfFilePath, pdfFileName) +{ + // Null value means default storage + let storage = null; + + const pdfFileData = await fs.readFile(pdfFilePath + pdfFileName); + + await api.uploadFile(pdfFileName, pdfFileData, storage); + + let result = await pdfApi.getDocumentAttachments(pdfFileName, storage, null); + + if (result.statusCode == 200) + { + const attachments = result.body; + + await ProcessPdfAttachments(attachments); + } + +} + +await getPdfDocumentAttachments(pdfApi, LOCAL_PATH, FILE_NAME); \ No newline at end of file diff --git a/UsesCases/Attachments/get_download.js b/UsesCases/Attachments/get_download.js new file mode 100644 index 00000000..83b4ac1b --- /dev/null +++ b/UsesCases/Attachments/get_download.js @@ -0,0 +1,45 @@ +/************************************************************************************************ + 1. Load your Application Secret and Key from the JSON file or set credentials in another way + 2. Initialize local path, uploading Pdf file and attachment index and get attachment function + 3. Create an object to connect to the Pdf.Cloud API + 4. Upload your document file using uploadFile() function of the Pdf.Cloud Rest API object + 5. To extract attachments from a PDF document, using getDocumentAttachmentByIndex() function + 6. Write extracting attachment stream to local file using createWriteStream() function of the Node.js file system object + + All initialized values of variables should be replaced by real user values +************************************************************************************************/ + +import credentials from "./credentials.json" with { type: "json" }; +import fs from 'node:fs/promises'; +import { PdfApi } from "asposepdfcloud"; + +const LOCAL_PATH = "C:\\Samples\\"; +const FILE_NAME = "Sample-Document-01"; +const LOCAL_FILE_NAME = LOCAL_PATH + FILE_NAME + ".pdf"; +const ATTACHMENT_IMDEX = 1; + +const STORAGE_FILENAME = FILE_NAME + ".pdf"; + +const getAttachment = async function (attachIndex) +{ + try { + const pdfApi = new PdfApi(credentials.id, credentials.key); + + const buffer = await fs.readFile(LOCAL_FILE_NAME); + await pdfApi.uploadFile(STORAGE_FILENAME, buffer); + + await pdfApi.getDownloadDocumentAttachmentByIndex(STORAGE_FILENAME, attachIndex, null, null, + function (response) { + console.log(response.status); + + const outfilename = FILE_NAME + "_attachment_" + (index + 1) + ".data"; + const writeStream = fs.createWriteStream(LOCAL_PATH + outfilename); + writeStream.write(response.body); + }); + + } catch (error) { + console.error(error.message); + } +} + +await getAttachment(ATTACHMENT_IMDEX); \ No newline at end of file From 410f6dd896607dcaf58a5a3a18668dd0e99b723d Mon Sep 17 00:00:00 2001 From: Dmitriy-Xawstov Date: Tue, 21 Jan 2025 14:57:16 +0200 Subject: [PATCH 2/4] Update add.js --- UsesCases/Attachments/add/add.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/UsesCases/Attachments/add/add.js b/UsesCases/Attachments/add/add.js index a9fe5cf1..48d6f07d 100644 --- a/UsesCases/Attachments/add/add.js +++ b/UsesCases/Attachments/add/add.js @@ -1,10 +1,11 @@ /***************************************************************************************************************** 1. Load your Application Secret and Key from the JSON file or set credentials in another way 2. Create an object to connect to the Pdf.Cloud API - 3. Create an AttachmentInfo object, which maust be inserted into Pdf file - 4. Perform append attachment into Pdf document using postAddDocumentAttachment() function and (AttachmentInfo) + 3. Upload your document file using pdfApi.UploadFile() function of the Storage API object + 4. Create an AttachmentInfo object, which maust be inserted into Pdf file + 5. Perform append attachment into Pdf document using postAddDocumentAttachment() function and (AttachmentInfo) object [https://github.com/aspose-pdf-cloud/aspose-pdf-cloud-node.js/blob/master/docs/AttachmentInfo.md] - 5. Check result and perform some actions with result.body object in + 6. Check result and perform some actions with result.body object in in format (Attachment)[https://github.com/aspose-pdf-cloud/aspose-pdf-cloud-node.js/blob/master/docs/Attachment.md] All values of variables starting with "YOUR_****" should be replaced by real user values @@ -67,4 +68,4 @@ const addAttachment = async function (atName, atPath, atDescription, atMIME) } } -await addAttachment("YOUR_ATTACHMENT_NAME", "YOUR_ATTACHMENT_PATH", "YOUR_ATTACHMENT_DESCRIPTION", "type/YOUR_SUBTYPE"); \ No newline at end of file +await addAttachment("YOUR_ATTACHMENT_NAME", "YOUR_ATTACHMENT_PATH", "YOUR_ATTACHMENT_DESCRIPTION", "type/YOUR_SUBTYPE"); From 92b0934fa67921bbc13e337e69437c0ca3a7ac30 Mon Sep 17 00:00:00 2001 From: Dmitriy-Xawstov Date: Tue, 21 Jan 2025 15:00:10 +0200 Subject: [PATCH 3/4] Update get.js --- UsesCases/Attachments/get/get.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/UsesCases/Attachments/get/get.js b/UsesCases/Attachments/get/get.js index 792311e1..815aa2a9 100644 --- a/UsesCases/Attachments/get/get.js +++ b/UsesCases/Attachments/get/get.js @@ -1,7 +1,7 @@ /************************************************************************************************** 1. Load your Application Secret and Key from the JSON file or set credentials in another way - 2. Create a Storage API object for uploading Pdf file - 3. Create an object to connect to the Pdf.Cloud API + 2. Create an object to connect to the Pdf.Cloud API + 3. Upload your document file using UploadFile() function of the Storage API object 4. Upload your document file using PutCreate() function of the Storage API object 5. To extract attachments from a PDF document, using getDocumentAttachments() function 6. Check result and perform some actions with result.body object @@ -54,4 +54,4 @@ async function getPdfDocumentAttachments(api, pdfFilePath, pdfFileName) } -await getPdfDocumentAttachments(pdfApi, LOCAL_PATH, FILE_NAME); \ No newline at end of file +await getPdfDocumentAttachments(pdfApi, LOCAL_PATH, FILE_NAME); From 83c96dbad3405b88d30ba64c2324aabc81816c46 Mon Sep 17 00:00:00 2001 From: Dmitriy-Xawstov Date: Tue, 21 Jan 2025 23:02:12 +0200 Subject: [PATCH 4/4] Update get.js --- UsesCases/Attachments/get/get.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/UsesCases/Attachments/get/get.js b/UsesCases/Attachments/get/get.js index 815aa2a9..5b03a615 100644 --- a/UsesCases/Attachments/get/get.js +++ b/UsesCases/Attachments/get/get.js @@ -47,7 +47,7 @@ async function getPdfDocumentAttachments(api, pdfFilePath, pdfFileName) if (result.statusCode == 200) { - const attachments = result.body; + const attachments = result.body.attachments; await ProcessPdfAttachments(attachments); }