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 { Rotation } from "../../src/models/rotation.js" ;
6+ import { DocumentConfig } from "../../src/models/documentConfig.js"
7+ import { DocumentProperties } from "../../src/models/documentProperties.js"
8+ import { DocumentProperty } from "../../src/models/documentProperty.js"
9+ import { DisplayProperties } from "../../src/models/displayProperties.js"
10+ import { DefaultPageConfig } from "../../src/models/defaultPageConfig.js"
11+ import { ImageStamp } from "../../src/models/imageStamp.js"
12+ import { HorizontalAlignment } from "../../src/models/horizontalAlignment.js"
13+ import { VerticalAlignment } from "../../src/models/verticalAlignment.js"
14+ import { HtmlDocumentType } from "../../src/models/htmlDocumentType.js" ;
15+ import { OutputFormat } from "../../src/models/outputFormat.js"
16+
17+ export { configParams , pdfApi , PdfChangeLayoutHelper } ;
18+
19+ const configParams = {
20+ LOCAL_FOLDER : "C:\\Samples\\" ,
21+ PDF_DOCUMENT_NAME : "sample.pdf" ,
22+ TEMP_FOLDER : 'TempPdfCloud' ,
23+
24+ ROTATE_ANGLE : Rotation . on90 ,
25+ ROTATE_PAGES : "1-3" ,
26+
27+ CROP_PAGE_TEMP_FILE : "sammple_temp_file.png" ,
28+ CROP_LOCAL_RESULT_DOCUMENT_NAME : "output_sample.pdf" ,
29+ CROP_PAGE_NUMBER : 3 ,
30+ CROP_PAGE_WIDTH : 0 ,
31+ CROP_PAGE_HEIGHT : 0 ,
32+ CROP_HEIGHT : 400 ,
33+ CROP_WIDTH : 300 ,
34+ CROP_LLX : 100 ,
35+ CROP_LLY : 200 ,
36+
37+ RESIZE_PDF_HTML_FILE : "sammple_temp_file.html" ,
38+ RESIZE_RESULT_DOCUMENT_NAME :"output_sample.pdf" ,
39+ RESIZE_PAGE_NUMBER : 2 ,
40+ RESIZE_NEW_PAGE_WIDTH : 1000 ,
41+ RESIZE_NEW_PAGE_HEIGHT : 3000 ,
42+
43+ } ;
44+
45+ const pdfApi = new PdfApi ( credentials . id , credentials . key ) ;
46+
47+ const PdfChangeLayoutHelper = {
48+ async uploadFile ( fileName , localFolder , tempFolder ) {
49+ const fileNamePath = path . join ( localFolder , fileName ) ;
50+ const fileData = await fs . readFile ( fileNamePath ) ;
51+ const storagePath = path . join ( tempFolder , fileName ) ;
52+ await pdfApi . uploadFile ( storagePath , fileData )
53+ . then ( ( ) => console . log ( "File: '" + fileName + "' successfully uploaded." ) ) ;
54+ } ,
55+
56+ async uploadDocument ( document , localFolder , tempFolder ) {
57+ await this . uploadFile ( document , localFolder , tempFolder )
58+ } ,
59+
60+ async downloadResult ( document , localFolder , tempFolder , prefix ) {
61+ const fileName = path . join ( tempFolder , document ) ;
62+ const changedPdfData = await pdfApi . downloadFile ( fileName ) ;
63+ const filePath = path . join ( localFolder , prefix + document ) ;
64+ await fs . writeFile ( filePath , changedPdfData . body ) ;
65+ console . log ( "Downloaded: " + filePath ) ;
66+ } ,
67+
68+ async getPageInfo ( document , pageNumber , tempFolder ) {
69+ const resultPages = await pdfApi . getPage ( document , pageNumber , { folder : tempFolder } ) ;
70+
71+ if ( resultPages . body . code == 200 && resultPages . body . page ) {
72+ this . showPages ( [ resultPages . body . page ] , "page" ) ;
73+ configParams . PAGE_HEIGHT = resultPages . body . page . rectangle . uRY - resultPages . body . page . rectangle . lLY ;
74+ configParams . PAGE_WIDTH = resultPages . body . page . rectangle . uRX - resultPages . body . page . rectangle . lLX ;
75+ return {
76+ "width" : configParams . PAGE_WIDTH ,
77+ "height" : configParams . PAGE_HEIGHT
78+ }
79+ }
80+ else {
81+ console . error ( "Unexpected error : can't get pages!!!" ) ;
82+ return null ;
83+ }
84+ } ,
85+
86+ showPages ( pages , prefix ) {
87+ if ( Array . isArray ( pages ) && pages . length > 0 )
88+ {
89+ pages . forEach ( function ( page ) {
90+ console . log ( prefix + " => id: '" + page . id + "', lLx: '" + page . rectangle . lLX + "', lLY: '" + page . rectangle . lLY + "', uRX: '" + page . rectangle . uRX + "', uRY: '" + page . rectangle . uRY + "'" ) ;
91+ } ) ;
92+ }
93+ else
94+ console . error ( "showPages() error: array of pages is empty!" )
95+ } ,
96+
97+ async extractPdfPage ( document , pageNumber , width , height , localFolder , tempFolder ) {
98+ const response = await pdfApi . getPageConvertToPng ( document , pageNumber , Math . trunc ( width ) , Math . trunc ( height ) , tempFolder ) ;
99+ if ( response . response . status != 200 )
100+ {
101+ console . error ( "extractPdfPage(): Faild to convert page to image!" ) ;
102+ return null ;
103+ }
104+ const filePath = path . join ( localFolder , document + ".png" ) ;
105+ await fs . writeFile ( filePath , response . body ) ;
106+
107+ const imageFile = document + ".png" ;
108+ const imagePath = localFolder ;
109+
110+ await this . uploadFile ( imageFile , localFolder , tempFolder ) ;
111+
112+ console . log ( "Page #" + pageNumber + " extracted as image." ) ;
113+ return imageFile ;
114+ } ,
115+
116+ async createPdfDocument ( document , width , height , tempFolder ) {
117+ const pdfConfig = new DocumentConfig ( ) ;
118+ pdfConfig . pagesCount = 1 ;
119+
120+ pdfConfig . displayProperties = new DisplayProperties ( ) ;
121+ pdfConfig . displayProperties . centerWindow = true ;
122+ pdfConfig . displayProperties . hideMenuBar = true ;
123+
124+ pdfConfig . documentProperties = new DocumentProperties ( ) ;
125+ const docProperty = new DocumentProperty ( ) ;
126+ docProperty . builtIn = false ;
127+ docProperty . name = "prop1" ;
128+ docProperty . value = "Val1" ;
129+
130+ pdfConfig . documentProperties . list = [ docProperty ] ;
131+
132+ pdfConfig . defaultPageConfig = new DefaultPageConfig ( ) ;
133+ pdfConfig . defaultPageConfig . height = height ;
134+ pdfConfig . defaultPageConfig . width = width ;
135+
136+ const response = await pdfApi . postCreateDocument ( document , pdfConfig , null , tempFolder ) ;
137+ console . log ( "Document #" + document + " created." )
138+ return response ;
139+ } ,
140+
141+ async insertPageAsImage ( document , imageFileValue , llx , lly , tempFolder ) {
142+ const stamp = new ImageStamp ( ) ;
143+ stamp . background = true ;
144+ stamp . horizontalAlignment = HorizontalAlignment . None ;
145+ stamp . verticalAlignment = VerticalAlignment . None ;
146+ stamp . opacity = 1 ;
147+ stamp . rotate = Rotation . None ;
148+ stamp . rotateAngle = 0 ;
149+ stamp . xIndent = - llx ;
150+ stamp . yIndent = - lly ;
151+ stamp . zoom = 1 ;
152+ stamp . fileName = configParams . TEMP_FOLDER + '/' + imageFileValue ;
153+
154+ const response = await pdfApi . postPageImageStamps ( document , 1 , [ stamp ] , null , tempFolder ) ;
155+ console . log ( "Image iserted into '" + document + "document on page #1" ) ;
156+ return response ;
157+ } ,
158+ } ;
0 commit comments