diff --git a/UsesCases/Attachments/add/add.js b/UsesCases/Attachments/add/add.js new file mode 100644 index 00000000..48d6f07d --- /dev/null +++ b/UsesCases/Attachments/add/add.js @@ -0,0 +1,71 @@ +/***************************************************************************************************************** + 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. 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] + 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 +*********************************************************************************************************/ + +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"); diff --git a/UsesCases/Attachments/get/get.js b/UsesCases/Attachments/get/get.js new file mode 100644 index 00000000..5b03a615 --- /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 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 + 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.attachments; + + await ProcessPdfAttachments(attachments); + } + +} + +await getPdfDocumentAttachments(pdfApi, LOCAL_PATH, FILE_NAME); 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