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
+
11
+ import credentials from "../../../../Credentials/credentials.json" with { type : "json" } ; // json-file in this format: { "id": "*****", "key": "*******" }
12
+ import fs from "node:fs/promises" ;
13
+ import path from "node:path" ;
14
+ import { PdfApi } from "../../../src/api/api.js" ;
15
+
16
+ const configParams = {
17
+ LOCAL_FOLDER : "C:\\Samples\\" ,
18
+ PDF_DOCUMENT_NAME : "sample_file_with_attachment.pdf" ,
19
+ LOCAL_RESULT_DOCUMENT_NAME : "output_sample.pdf" ,
20
+ ATTACHMENT_PATH : "" ,
21
+ } ;
22
+
23
+ const pdfApi = new PdfApi ( credentials . id , credentials . key ) ;
24
+
25
+ const pdfAttachments = {
26
+ async uploadDocument ( ) {
27
+ const pdfFilePath = path . join ( configParams . LOCAL_FOLDER , configParams . PDF_DOCUMENT_NAME ) ;
28
+ const pdfFileData = await fs . readFile ( pdfFilePath ) ;
29
+ await pdfApi . uploadFile ( configParams . PDF_DOCUMENT_NAME , pdfFileData ) ;
30
+ console . log ( "Uploaded file: " + configParams . PDF_DOCUMENT_NAME ) ;
31
+ } ,
32
+
33
+ showAttachments ( attachments , prefix ) {
34
+ if ( Array . isArray ( attachments ) && attachments . length > 0 )
35
+ {
36
+ attachments . forEach ( function ( attachment ) {
37
+ console . log ( prefix + " => name: '" + attachment . name + "', path: '" + attachment . links [ 0 ] . href + "'" ) ;
38
+ } ) ;
39
+ }
40
+ } ,
41
+
42
+ async getAttachments ( ) {
43
+ const response = await pdfApi . getDocumentAttachments ( configParams . PDF_DOCUMENT_NAME ) ;
44
+ const { code, attachments } = response . body ;
45
+
46
+ if ( code === 200 && attachments . list && attachments . list . length > 0 ) {
47
+ this . showAttachments ( attachments . list , "all" ) ;
48
+ configParams . ATTACHMENT_PATH = attachments . list [ 0 ] . links [ 0 ] . href ;
49
+ }
50
+ else
51
+ {
52
+ console . error ( "Document has no attachmnets!!!" ) ;
53
+ }
54
+ } ,
55
+ async getAttachmentById ( ) {
56
+ const response = await pdfApi . getDocumentAttachmentByIndex ( configParams . PDF_DOCUMENT_NAME , configParams . ATTACHMENT_PATH ) ;
57
+ const { code, attachment } = response . body ;
58
+
59
+ if ( code == 200 ) {
60
+ console . log ( "Attachment file: " + attachment . name ) ;
61
+ const attachmentFile = await pdfApi . getDownloadDocumentAttachmentByIndex ( configParams . PDF_DOCUMENT_NAME , configParams . ATTACHMENT_PATH )
62
+ const localPath = path . join ( configParams . LOCAL_FOLDER , attachment . name ) ;
63
+ await fs . writeFile ( localPath , attachmentFile . body ) ;
64
+ console . log ( "Downloaded: " + localPath ) ;
65
+ }
66
+ }
67
+ } ;
68
+
69
+ async function main ( ) {
70
+ try {
71
+ await pdfAttachments . uploadDocument ( ) ;
72
+ await pdfAttachments . getAttachments ( ) ;
73
+ await pdfAttachments . getAttachmentById ( ) ;
74
+ } catch ( error ) {
75
+ console . error ( "Error:" , error . message ) ;
76
+ }
77
+ }
78
+
79
+ main ( ) ;
0 commit comments