Skip to content

PDFAPPS-6672: added use cases for Attachments #104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions UsesCases/Attachments/add/add.js
Original file line number Diff line number Diff line change
@@ -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");
57 changes: 57 additions & 0 deletions UsesCases/Attachments/get/get.js
Original file line number Diff line number Diff line change
@@ -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);
45 changes: 45 additions & 0 deletions UsesCases/Attachments/get_download.js
Original file line number Diff line number Diff line change
@@ -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);