Advertisement
andruhovski

Aspose.PDF Cloud Demo

Jan 22nd, 2025 (edited)
64
0
3 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 5.22 KB | Software | 0 0
  1. import credentials from "./credentials.json" with { type: "json" };
  2. import fs from 'node:fs/promises';
  3. import path from 'node:path';
  4. import { PdfApi } from "asposepdfcloud";
  5.  
  6.  
  7. const attachments = {
  8.     getAllAttachments: async function () {
  9.         const LOCAL_PATH = "C:\\Samples\\";
  10.         const LOCAL_FILE_NAME = "C:\\Samples\\Attachments\\sample_attachment.pdf";
  11.         const STORAGE_FILE_NAME = "sample_attachment.pdf";
  12.         try {
  13.  
  14.             const pdfApi = new PdfApi(credentials.id, credentials.key);
  15.  
  16.             const pdfData = await fs.readFile(LOCAL_FILE_NAME);
  17.             await pdfApi.uploadFile(STORAGE_FILE_NAME, pdfData);
  18.  
  19.             const result = await pdfApi.getDocumentAttachments(STORAGE_FILE_NAME);
  20.  
  21.             if (result.body.code === 200 && result.body.attachments) {
  22.                 if (!Array.isArray(result.body.attachments.links) || result.body.attachments.links.length === 0) {
  23.                     console.error("Unexpected error: No attachments to download.");
  24.                     return;
  25.                 }
  26.  
  27.                 const downloadPromises = result.body.attachments.links.map(
  28.                     async (attachment, index) => {
  29.                         try {
  30.                             const response = await pdfApi.getDocumentAttachmentByIndex(STORAGE_FILE_NAME, index);
  31.                             //TODO: Check this
  32.                             const filePath = path.join(LOCAL_PATH, response.body.attachment.name);
  33.                             await fs.writeFile(filePath, response.body);
  34.                         } catch (error) {
  35.                             console.error(`Failed to download attachment ${index}:`, error);
  36.                         }
  37.                     });
  38.  
  39.                 await Promise.all(downloadPromises);
  40.             } else {
  41.                 console.error("Failed to retrieve attachments. Status:", result.statusCode);
  42.             }
  43.         } catch (error) {
  44.             console.error("Error processing PDF attachments:", error);
  45.         }
  46.     },
  47.     getSingleAttachment: async function () {
  48.         const LOCAL_PATH = "C:\\Samples\\";
  49.         const LOCAL_FILE_NAME = "C:\\Samples\\Attachments\\sample_attachment.pdf";
  50.         const STORAGE_FILE_NAME = "sample_attachment.pdf";
  51.         try {
  52.             const pdfApi = new PdfApi(credentials.id, credentials.key);
  53.             const pdfData = await fs.readFile(LOCAL_FILE_NAME);
  54.             await pdfApi.uploadFile(STORAGE_FILE_NAME, pdfData);
  55.  
  56.             const result = await pdfApi.getDocumentAttachments(STORAGE_FILE_NAME);
  57.  
  58.             if (result.body.code === 200 && result.body.attachments) {
  59.                 if (!Array.isArray(result.body.attachments.list) || result.body.attachments.list.length === 0) {
  60.                     console.error("Unexpected error: No attachments to download.");
  61.                     return;
  62.                 }
  63.  
  64.                 try {
  65.                     const response = await pdfApi.getDocumentAttachmentByIndex(STORAGE_FILE_NAME, 1);
  66.                     //TODO: Check this
  67.                     const filePath = path.join(LOCAL_PATH, response.body.attachment.name);
  68.                     await fs.writeFile(filePath, response.body);
  69.                 } catch (error) {
  70.                     console.error(`Failed to download attachment ${index}:`, error);
  71.                 }
  72.  
  73.  
  74.             } else {
  75.                 console.error("Failed to retrieve attachments. Status:", result.statusCode);
  76.             }
  77.         } catch (error) {
  78.             console.error("Error processing PDF attachments:", error);
  79.         }
  80.     },
  81.     addAttachment: async function () {
  82.         const LOCAL_FILE_NAME = "C:\\Samples\\Attachments\\sample_attachment.pdf";
  83.         const STORAGE_FILE_NAME = "sample_attachment.pdf";
  84.         const LOCAL_ATTACHMENT_FILE_NAME = "C:\\Samples\\Attachments\\file_example_MP3_700KB.mp3";
  85.         const STORAGE_ATTACHMENT_FILE_NAME = "file_example_MP3_700KB.mp3";
  86.         const RESULT_FILE_NAME = "C:\\Samples\\Attachments\\sample_attachment.pdf";
  87.         try {
  88.  
  89.             const pdfApi = new PdfApi(credentials.id, credentials.key);
  90.             let buffer = await fs.readFile(LOCAL_FILE_NAME);
  91.             await pdfApi.uploadFile(LOCAL_ATTACHMENT_FILE_NAME, buffer);
  92.             buffer = await fs.readFile(LOCAL_ATTACHMENT_FILE_NAME);
  93.             await pdfApi.uploadFile(STORAGE_ATTACHMENT_FILE_NAME, buffer);
  94.  
  95.             const attachment = {
  96.                 name: STORAGE_ATTACHMENT_FILE_NAME,
  97.                 path: STORAGE_ATTACHMENT_FILE_NAME,
  98.                 description: "An example of MP3 file",
  99.                 mimeType: "audio/mpeg"
  100.             }
  101.  
  102.             const appendResult = await pdfApi.postAddDocumentAttachment(STORAGE_FILE_NAME, attachment, null, null);
  103.  
  104.             if (appendResult.body.code == 200) {
  105.                 console.log("Status:", appendResult.body.status);
  106.                 const downloadResult = await pdfApi.downloadFile(STORAGE_FILE_NAME);
  107.                 await fs.writeFile(RESULT_FILE_NAME, downloadResult.body);
  108.             }
  109.             else
  110.                 console.log("Unexpected error : can't download attachments");
  111.  
  112.         } catch (error) {
  113.             console.error(error.message);
  114.         }
  115.     }
  116. }
  117. export default attachments;
Tags: Cloud aspose
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement