Skip to content

Commit feae984

Browse files
authored
Merge pull request #27 from aspose-pdf-cloud/holub
feat: PDF Pages (add landing pages - add Bookmarks section - nodejs)
2 parents 1c5ba2b + 7b1679b commit feae984

File tree

6 files changed

+730
-80
lines changed

6 files changed

+730
-80
lines changed

pdf/nodejs/attachments/_index.md

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -80,36 +80,42 @@ It is easy to get started with Aspose.PDF Cloud Node.js SDK and there is nothing
8080

8181
```js
8282

83-
const pdfApi = new PdfApi(credentials.id, credentials.key);
84-
const pdfData = await fs.readFile(LOCAL_FILE_NAME);
85-
await pdfApi.uploadFile(STORAGE_FILE_NAME, pdfData);
86-
87-
const result = await pdfApi.getDocumentAttachments(STORAGE_FILE_NAME);
88-
89-
if (result.body.code === 200 && result.body.attachments) {
90-
const attachmentList = result.body.attachments.list || [];
91-
if (!attachmentList.length) {
92-
console.error("No attachments found.");
93-
return;
94-
}
95-
96-
const downloadTasks = attachmentList.map(async (attachment) => {
97-
try {
98-
const attachmentUrl = attachment.links[0].href;
99-
const info = await pdfApi.getDocumentAttachmentByIndex(STORAGE_FILE_NAME, attachmentUrl);
100-
const download = await pdfApi.getDownloadDocumentAttachmentByIndex(STORAGE_FILE_NAME, attachmentUrl);
101-
await fs.writeFile(path.join(LOCAL_PATH, info.body.attachment.name), download.body);
102-
} catch (error) {
103-
console.error("Failed to download attachment:", error);
83+
async function getAllAttachments() {
84+
const LOCAL_PATH = "C:\\Samples\\";
85+
const LOCAL_FILE_NAME = "C:\\Samples\\Attachments\\sample_attachment.pdf";
86+
const STORAGE_FILE_NAME = "sample_attachment.pdf";
87+
try {
88+
const pdfApi = new PdfApi(credentials.id, credentials.key);
89+
const pdfData = await fs.readFile(LOCAL_FILE_NAME);
90+
await pdfApi.uploadFile(STORAGE_FILE_NAME, pdfData);
91+
92+
const result = await pdfApi.getDocumentAttachments(STORAGE_FILE_NAME);
93+
94+
if (result.body.code === 200 && result.body.attachments) {
95+
const attachmentList = result.body.attachments.list || [];
96+
if (!attachmentList.length) {
97+
console.error("No attachments found.");
98+
return;
10499
}
105-
});
106100

107-
await Promise.all(downloadTasks);
108-
} else {
109-
console.error("Failed to retrieve attachments. Status:", result.statusCode);
101+
const downloadTasks = attachmentList.map(async (attachment) => {
102+
try {
103+
const attachmentUrl = attachment.links[0].href;
104+
const info = await pdfApi.getDocumentAttachmentByIndex(STORAGE_FILE_NAME, attachmentUrl);
105+
const download = await pdfApi.getDownloadDocumentAttachmentByIndex(STORAGE_FILE_NAME, attachmentUrl);
106+
await fs.writeFile(path.join(LOCAL_PATH, info.body.attachment.name), download.body);
107+
} catch (error) {
108+
console.error("Failed to download attachment:", error);
109+
}
110+
});
111+
112+
await Promise.all(downloadTasks);
113+
} else {
114+
console.error("Failed to retrieve attachments. Status:", result.statusCode);
115+
}
116+
} catch (error) {
117+
console.error("Error processing PDF attachments:", error);
110118
}
111-
} catch (error) {
112-
console.error("Error processing PDF attachments:", error);
113119
}
114120
```
115121

pdf/nodejs/attachments/append/_index.md

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -76,34 +76,40 @@ It is easy to get started with Aspose.PDF Cloud Node.js SDK and there is nothing
7676

7777
```js
7878

79-
const pdfApi = new PdfApi(credentials.id, credentials.key);
80-
let buffer = await fs.readFile(LOCAL_FILE_NAME);
81-
let uploadResult = await pdfApi.uploadFile(STORAGE_FILE_NAME, buffer);
82-
console.log(`Uploaded: ${uploadResult.body.errors.length === 0}`);
83-
buffer = await fs.readFile(LOCAL_ATTACHMENT_FILE_NAME);
84-
uploadResult = await pdfApi.uploadFile(STORAGE_ATTACHMENT_FILE_NAME, buffer);
85-
console.log(`Uploaded: ${uploadResult.body.errors.length === 0}`);
86-
87-
const attachment = new AttachmentInfo();
88-
attachment.name = STORAGE_ATTACHMENT_FILE_NAME;
89-
attachment.path = STORAGE_ATTACHMENT_FILE_NAME;
90-
attachment.description = "An example of MP3 file";
91-
attachment.mimeType = "audio/mpeg";
92-
93-
const appendResult = await pdfApi.postAddDocumentAttachment(STORAGE_FILE_NAME, attachment, null, null);
94-
95-
if (appendResult.body.code == 200) {
96-
const downloadResult = await pdfApi.downloadFile(STORAGE_FILE_NAME);
97-
await fs.writeFile(RESULT_FILE_NAME, downloadResult.body);
98-
}
99-
else
100-
console.log("Unexpected error : can't download attachments");
101-
102-
}
103-
catch (error) {
104-
console.error(error.message);
79+
async function addAttachment() {
80+
const LOCAL_FILE_NAME = "C:\\Samples\\Attachments\\sample_attachment.pdf";
81+
const STORAGE_FILE_NAME = "sample_attachment.pdf";
82+
const LOCAL_ATTACHMENT_FILE_NAME = "C:\\Samples\\Attachments\\file_example_MP3_700KB.mp3";
83+
const STORAGE_ATTACHMENT_FILE_NAME = "file_example_MP3_700KB.mp3";
84+
const RESULT_FILE_NAME = "C:\\Samples\\Attachments\\sample_attachment.pdf";
85+
try {
86+
const pdfApi = new PdfApi(credentials.id, credentials.key);
87+
let buffer = await fs.readFile(LOCAL_FILE_NAME);
88+
let uploadResult = await pdfApi.uploadFile(STORAGE_FILE_NAME, buffer);
89+
console.log(`Uploaded: ${uploadResult.body.errors.length === 0}`);
90+
buffer = await fs.readFile(LOCAL_ATTACHMENT_FILE_NAME);
91+
uploadResult = await pdfApi.uploadFile(STORAGE_ATTACHMENT_FILE_NAME, buffer);
92+
console.log(`Uploaded: ${uploadResult.body.errors.length === 0}`);
93+
94+
const attachment = new AttachmentInfo();
95+
attachment.name = STORAGE_ATTACHMENT_FILE_NAME;
96+
attachment.path = STORAGE_ATTACHMENT_FILE_NAME;
97+
attachment.description = "An example of MP3 file";
98+
attachment.mimeType = "audio/mpeg";
99+
100+
const appendResult = await pdfApi.postAddDocumentAttachment(STORAGE_FILE_NAME, attachment, null, null);
101+
102+
if (appendResult.body.code == 200) {
103+
const downloadResult = await pdfApi.downloadFile(STORAGE_FILE_NAME);
104+
await fs.writeFile(RESULT_FILE_NAME, downloadResult.body);
105105
}
106+
else
107+
console.log("Unexpected error : can't download attachments");
108+
109+
} catch (error) {
110+
console.error(error.message);
106111
}
112+
}
107113
```
108114

109115
{{% /blocks/products/pf/agp/code-block %}}

pdf/nodejs/attachments/extract/_index.md

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -77,36 +77,42 @@ It is easy to get started with Aspose.PDF Cloud Node.js SDK and there is nothing
7777

7878
```js
7979

80-
const pdfApi = new PdfApi(credentials.id, credentials.key);
81-
const pdfData = await fs.readFile(LOCAL_FILE_NAME);
82-
await pdfApi.uploadFile(STORAGE_FILE_NAME, pdfData);
83-
84-
const result = await pdfApi.getDocumentAttachments(STORAGE_FILE_NAME);
85-
86-
if (result.body.code === 200 && result.body.attachments) {
87-
const attachmentList = result.body.attachments.list || [];
88-
if (!attachmentList.length) {
89-
console.error("No attachments found.");
90-
return;
91-
}
92-
93-
const downloadTasks = attachmentList.map(async (attachment) => {
94-
try {
95-
const attachmentUrl = attachment.links[0].href;
96-
const info = await pdfApi.getDocumentAttachmentByIndex(STORAGE_FILE_NAME, attachmentUrl);
97-
const download = await pdfApi.getDownloadDocumentAttachmentByIndex(STORAGE_FILE_NAME, attachmentUrl);
98-
await fs.writeFile(path.join(LOCAL_PATH, info.body.attachment.name), download.body);
99-
} catch (error) {
100-
console.error("Failed to download attachment:", error);
80+
async function getAllAttachments() {
81+
const LOCAL_PATH = "C:\\Samples\\";
82+
const LOCAL_FILE_NAME = "C:\\Samples\\Attachments\\sample_attachment.pdf";
83+
const STORAGE_FILE_NAME = "sample_attachment.pdf";
84+
try {
85+
const pdfApi = new PdfApi(credentials.id, credentials.key);
86+
const pdfData = await fs.readFile(LOCAL_FILE_NAME);
87+
await pdfApi.uploadFile(STORAGE_FILE_NAME, pdfData);
88+
89+
const result = await pdfApi.getDocumentAttachments(STORAGE_FILE_NAME);
90+
91+
if (result.body.code === 200 && result.body.attachments) {
92+
const attachmentList = result.body.attachments.list || [];
93+
if (!attachmentList.length) {
94+
console.error("No attachments found.");
95+
return;
10196
}
102-
});
10397

104-
await Promise.all(downloadTasks);
105-
} else {
106-
console.error("Failed to retrieve attachments. Status:", result.statusCode);
98+
const downloadTasks = attachmentList.map(async (attachment) => {
99+
try {
100+
const attachmentUrl = attachment.links[0].href;
101+
const info = await pdfApi.getDocumentAttachmentByIndex(STORAGE_FILE_NAME, attachmentUrl);
102+
const download = await pdfApi.getDownloadDocumentAttachmentByIndex(STORAGE_FILE_NAME, attachmentUrl);
103+
await fs.writeFile(path.join(LOCAL_PATH, info.body.attachment.name), download.body);
104+
} catch (error) {
105+
console.error("Failed to download attachment:", error);
106+
}
107+
});
108+
109+
await Promise.all(downloadTasks);
110+
} else {
111+
console.error("Failed to retrieve attachments. Status:", result.statusCode);
112+
}
113+
} catch (error) {
114+
console.error("Error processing PDF attachments:", error);
107115
}
108-
} catch (error) {
109-
console.error("Error processing PDF attachments:", error);
110116
}
111117
```
112118

0 commit comments

Comments
 (0)