1+ import credentials from "../../../Credentials/credentials.json" with { type : "json" } ;
2+ import fs from 'node:fs/promises' ;
3+ import path from 'node:path' ;
4+ import { PdfApi } from "../../src/api/api.js" ;
5+ import { SplitRangePdfOptions } from "../../src/models/splitRangePdfOptions.js" ;
6+ import { PageRange } from "../../src/models/pageRange.js" ;
7+
8+ const configParams = {
9+ LOCAL_FOLDER : "C:\\Samples\\" ,
10+ PDF_DOCUMENT_NAME : "sample.pdf" ,
11+ } ;
12+
13+ const range_1 = new PageRange ( ) ;
14+ range_1 . from = 1 ;
15+ range_1 . to = 3 ;
16+
17+ const range_2 = new PageRange ( ) ;
18+ range_2 . from = 4 ;
19+ range_2 . to = 7 ;
20+
21+ const splitRangesArray = new SplitRangePdfOptions ( ) ;
22+ splitRangesArray . pageRanges = [ range_1 , range_2 ] ;
23+
24+ const pdfApi = new PdfApi ( credentials . id , credentials . key ) ;
25+
26+ const pdfSplitter = {
27+ async uploadDocument ( ) {
28+ const fileNamePath = path . join ( configParams . LOCAL_FOLDER , configParams . PDF_DOCUMENT_NAME ) ;
29+ const pdfFileData = await fs . readFile ( fileNamePath ) ;
30+ await pdfApi . uploadFile ( configParams . PDF_DOCUMENT_NAME , pdfFileData ) ;
31+ console . log ( "Source document uploaded!" ) ;
32+ } ,
33+
34+ async downloadPages ( pageHref , index ) {
35+ const changedPdfData = await pdfApi . downloadFile ( pageHref ) ;
36+ const filePath = path . join ( configParams . LOCAL_FOLDER , 'Page_' + index + '_' + pageHref ) ;
37+ await fs . writeFile ( filePath , changedPdfData . body ) ;
38+ console . log ( "Downloaded: " + filePath ) ;
39+ } ,
40+
41+ async splitRanges ( ) {
42+ const resultPages = await pdfApi . postSplitRangePdfDocument ( configParams . PDF_DOCUMENT_NAME , splitRangesArray ) ;
43+
44+ if ( resultPages . body . code == 200 && resultPages . body . result . documents ) {
45+ resultPages . body . result . documents . forEach ( async ( docPage , index ) => {
46+ await this . downloadPages ( docPage . href , index ) ;
47+ } )
48+ }
49+ else
50+ console . error ( "Unexpected error : can't get splitted documents!!!" ) ;
51+ } ,
52+
53+
54+ }
55+
56+ async function main ( ) {
57+ try {
58+ await pdfSplitter . uploadDocument ( ) ;
59+ await pdfSplitter . splitRanges ( ) ;
60+ } catch ( error ) {
61+ console . error ( "Error:" , error . message ) ;
62+ }
63+ }
64+
65+ main ( ) ;
0 commit comments