Skip to content

Commit c654ecb

Browse files
authored
Merge pull request #114 from aspose-pdf-cloud/pdfapps-6672-added-use-cases-fo-attachments
PDFAPPS-6672: added use cases for Attachments #2
2 parents 2658c67 + 7cd2efe commit c654ecb

File tree

2 files changed

+151
-0
lines changed

2 files changed

+151
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// 1. Load your Application Secret and Key from the JSON file or set credentials in another way
2+
// 2. Create an object to connect to the Pdf.Cloud API
3+
// 3. Upload your document file
4+
// 4. Create a new Attachment with the required properties
5+
// 5. Append new Attachment to the document using postAddDocumentAttachment() function
6+
// 6. Perform some action after successful addition attachment
7+
// 7. Download result file after appending Attachment
8+
// All values of variables starting with "YOUR_****" should be replaced by real user values
9+
10+
import credentials from "./credentials.json" with { type: "json" };
11+
import fs from 'node:fs/promises';
12+
import path from 'node:path';
13+
import { PdfApi } from "asposepdfcloud";
14+
import { AttachmentInfo } from "asposepdfcloud/src/models/attachmentInfo.js";
15+
16+
const configParams = {
17+
LOCAL_FOLDER: "C:\\Samples\\",
18+
PDF_DOCUMENT_NAME: "sample.pdf",
19+
LOCAL_RESULT_DOCUMENT_NAME: "output_sample.pdf",
20+
NEW_ATTACHMENT_FILE: "sample_video.mp4",
21+
NEW_ATTACHMENT_MIME: "video/mp4",
22+
NEW_ATTACHMENT_DECRIPTION: 'This is a sample attachment',
23+
PAGE_NUMBER: 2,
24+
};
25+
26+
const pdfApi = new PdfApi(credentials.id, credentials.key);
27+
28+
const pdfAttachments = {
29+
async uploadFile (fileName) {
30+
const fileNamePath = path.join(configParams.LOCAL_FOLDER, fileName);
31+
const pdfFileData = await fs.readFile(fileNamePath);
32+
await pdfApi.uploadFile(fileName, pdfFileData);
33+
},
34+
35+
async downloadResult () {
36+
const changedPdfData = await pdfApi.downloadFile(configParams.PDF_DOCUMENT_NAME);
37+
const filePath = path.join(configParams.LOCAL_FOLDER, configParams.LOCAL_RESULT_DOCUMENT_NAME);
38+
await fs.writeFile(filePath, changedPdfData.body);
39+
console.log("Downloaded: " + filePath);
40+
},
41+
42+
async uploadDocument () {
43+
await this.uploadFile(configParams.PDF_DOCUMENT_NAME);
44+
},
45+
46+
async appendAttachment() {
47+
48+
const newAttachment = new AttachmentInfo();
49+
newAttachment.path = configParams.NEW_ATTACHMENT_FILE;
50+
newAttachment.description = configParams.NEW_ATTACHMENT_DECRIPTION;
51+
newAttachment.name = configParams.NEW_ATTACHMENT_FILE;
52+
53+
const response = await pdfApi.postAddDocumentAttachment(configParams.PDF_DOCUMENT_NAME, newAttachment);
54+
const { code } = response.body;
55+
56+
if (code === 200) {
57+
console.log("Appended attachment successfulll");
58+
}
59+
},
60+
};
61+
62+
async function main() {
63+
try {
64+
await pdfAttachments.uploadDocument();
65+
await pdfAttachments.uploadFile(configParams.NEW_ATTACHMENT_FILE);
66+
await pdfAttachments.appendAttachment();
67+
await pdfAttachments.downloadResult();
68+
} catch (error) {
69+
console.error("Error:", error.message);
70+
}
71+
}
72+
73+
main();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// 1. Load your Application Secret and Key from the JSON file or set credentials in another way
2+
// 2. Create an object to connect to the Pdf.Cloud API
3+
// 3. Upload your document file
4+
// 4. Get all document attachments by using getDocumentAttachments() function
5+
// 5. Perform some action with attachments array (for example, show information)
6+
// 6. Get attachment by path using getDocumentAttachmentByIndex() function
7+
// 7. Perform some action after successful retrieving attachmwent file name using getDownloadDocumentAttachmentByIndex() function
8+
// 8. Save response body to local file name
9+
// All values of variables starting with "YOUR_****" should be replaced by real user values
10+
import credentials from "./credentials.json" with { type: "json" };
11+
import fs from 'node:fs/promises';
12+
import path from 'node:path';
13+
import { PdfApi } from "asposepdfcloud";
14+
15+
const configParams = {
16+
LOCAL_FOLDER: "C:\\Samples\\",
17+
PDF_DOCUMENT_NAME: "sample_file_with_attachment.pdf",
18+
LOCAL_RESULT_DOCUMENT_NAME: "output_sample.pdf",
19+
ATTACHMENT_PATH: "", // filed will be setting by using attachments.list[0].links[0].href after call getDocumentAttachments() function
20+
};
21+
22+
const pdfApi = new PdfApi(credentials.id, credentials.key);
23+
24+
const pdfAttachments = {
25+
async uploadDocument() {
26+
const pdfFilePath = path.join(configParams.LOCAL_FOLDER, configParams.PDF_DOCUMENT_NAME);
27+
const pdfFileData = await fs.readFile(pdfFilePath);
28+
await pdfApi.uploadFile(configParams.PDF_DOCUMENT_NAME, pdfFileData);
29+
console.log("Uploaded file: " + configParams.PDF_DOCUMENT_NAME);
30+
},
31+
32+
showAttachments(attachments, prefix) {
33+
if (Array.isArray(attachments) && attachments.length > 0)
34+
{
35+
attachments.forEach(function(attachment) {
36+
console.log(prefix +" => name: '" + attachment.name + "', path: '" + attachment.links[0].href + "'");
37+
});
38+
}
39+
},
40+
41+
async getAttachments() {
42+
const response = await pdfApi.getDocumentAttachments(configParams.PDF_DOCUMENT_NAME);
43+
const { code, attachments } = response.body;
44+
45+
if (code === 200 && attachments.list && attachments.list.length > 0) {
46+
this.showAttachments(attachments.list, "all");
47+
configParams.ATTACHMENT_PATH = attachments.list[0].links[0].href;
48+
}
49+
else
50+
{
51+
console.error("Document has no attachmnets!!!");
52+
}
53+
},
54+
async getAttachmentById() {
55+
const response = await pdfApi.getDocumentAttachmentByIndex(configParams.PDF_DOCUMENT_NAME, configParams.ATTACHMENT_PATH);
56+
const { code, attachment } = response.body;
57+
58+
if (code == 200) {
59+
console.log("Attachment file: " + attachment.name);
60+
const attachmentFile = await pdfApi.getDownloadDocumentAttachmentByIndex(configParams.PDF_DOCUMENT_NAME, configParams.ATTACHMENT_PATH)
61+
const localPath = path.join(configParams.LOCAL_FOLDER, attachment.name);
62+
await fs.writeFile(localPath, attachmentFile.body);
63+
console.log("Downloaded: " + localPath);
64+
}
65+
}
66+
};
67+
68+
async function main() {
69+
try {
70+
await pdfAttachments.uploadDocument();
71+
await pdfAttachments.getAttachments();
72+
await pdfAttachments.getAttachmentById();
73+
} catch (error) {
74+
console.error("Error:", error.message);
75+
}
76+
}
77+
78+
main();

0 commit comments

Comments
 (0)