1
+ import credentials from "../../../Credentials/credentials.json" with { type : "json" } ; // json-file in this format: { "id": "*****", "key": "*******" }
2
+ import fs from 'node:fs/promises' ;
3
+ import path from 'node:path' ;
4
+ import { PdfApi } from "../../src/api/api.js" ;
5
+ import { CryptoAlgorithm } from "../../src/models/cryptoAlgorithm.js" ;
6
+
7
+ const configParams = {
8
+ LOCAL_FOLDER : "C:\\Samples\\" ,
9
+ PDF_DOCUMENT_NAME : "sample.pdf" ,
10
+ LOCAL_RESULT_DOCUMENT_NAME : "output_sample.pdf" ,
11
+ ENCRYPT_ALGORITHM : CryptoAlgorithm . AESx256 ,
12
+ USER_PASSWORD : "User-Password" ,
13
+ OWNER_PASSWORD : "Owner-Password" ,
14
+ } ;
15
+
16
+ const pdfApi = new PdfApi ( credentials . id , credentials . key ) ;
17
+
18
+ const pdfEncoder = {
19
+ async uploadDocument ( ) {
20
+ const fileNamePath = path . join ( configParams . LOCAL_FOLDER , configParams . PDF_DOCUMENT_NAME ) ;
21
+ const pdfFileData = await fs . readFile ( fileNamePath ) ;
22
+ await pdfApi . uploadFile ( configParams . PDF_DOCUMENT_NAME , pdfFileData )
23
+ . then ( ( ) => console . log ( "File: '" + configParams . PDF_DOCUMENT_NAME + "' successfully uploaded." ) ) ;
24
+ } ,
25
+
26
+ async downloadResult ( ) {
27
+ const changedPdfData = await pdfApi . downloadFile ( configParams . PDF_DOCUMENT_NAME ) ;
28
+ const filePath = path . join ( configParams . LOCAL_FOLDER , configParams . LOCAL_RESULT_DOCUMENT_NAME ) ;
29
+ await fs . writeFile ( filePath , changedPdfData . body ) ;
30
+ console . log ( "Downloaded: " + filePath ) ;
31
+ } ,
32
+
33
+ async encrypt_document ( ) {
34
+ const user_password_encoded = btoa ( configParams . USER_PASSWORD )
35
+
36
+ const owner_password_encoded = btoa ( configParams . OWNER_PASSWORD )
37
+
38
+ const response = await pdfApi . postEncryptDocumentInStorage ( configParams . PDF_DOCUMENT_NAME , user_password_encoded , owner_password_encoded , configParams . ENCRYPT_ALGORITHM ) ;
39
+
40
+ if ( response . body . code == 200 )
41
+ console . log ( "encrypt_document(): Document #'" + configParams . PDF_DOCUMENT_NAME + "' successfully encrypted." )
42
+ else
43
+ throw new Error ( "encrypt_document(): Failed to encrypt document #'" + configParams . PDF_DOCUMENT_NAME + "'. Response code: {" + response . code + "}" )
44
+ } ,
45
+
46
+ }
47
+
48
+ async function main ( ) {
49
+ try {
50
+ await pdfEncoder . uploadDocument ( ) ;
51
+ await pdfEncoder . encrypt_document ( ) ;
52
+ await pdfEncoder . downloadResult ( ) ;
53
+ } catch ( error ) {
54
+ console . error ( "Error:" , error . message ) ;
55
+ }
56
+ }
57
+
58
+ main ( ) ;
0 commit comments