-
Notifications
You must be signed in to change notification settings - Fork 500
Expand file tree
/
Copy pathmerge.js
More file actions
72 lines (40 loc) · 2.04 KB
/
Copy pathmerge.js
File metadata and controls
72 lines (40 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*adding req PDFDocument module of pdf-lib and file systems lib,
need to be downloaded in json package before implementing the code*/
const {PDFDocument} = require('pdf-lib');
const fileSys = require('fs');
// to catch and log errors
mainFun().catch(errors => console.log(errors));
//async main_fun is declared
async function mainFun(){
// an empty pdf is created using create() function of file systems
const emptyPdf = await PDFDocument.create();
/*adding first pdf using readFileSync function of file systems
await is used to make it wait while pdf1 is loading*/
const pdf1 = await PDFDocument.load(fileSys.readFileSync('./Hello.pdf'));
/*adding second pdf
await is used to make it wait while pdf2 is loading*/
const pdf2 = await PDFDocument.load(fileSys.readFileSync('./World.pdf'));
/* pages of pdf1 are copied to empty pdf
getPagesIndices() function of file systems is used to get number of pages in pdf1
to use it as range in for loop while traversing*/
const pagePdf1 = await emptyPdf.copyPages(pdf1, pdf1.getPageIndices());
//for loop is used to traverse pdf within its index to add pages to emptyPdf
for (const page of pagePdf1)
{
//pages added using addPge() fun of file systems
emptyPdf.addPage(page);
}
/* pages of pdf2 are copied to empty pdf
getPagesIndices() function of file systems is used to get number of pages in pdf2
to use it as range in for loop while traversing*/
const pagePdf2 = await emptyPdf.copyPages(pdf2, pdf2.getPageIndices());
//for loop is used to traverse pdf within its index to add pages to emptyPdf
for(const page of pagePdf2){
//pages added using addPge() fun of file systems
emptyPdf.addPage(page);
}
/*using writerFileSync(), function of file sys
contents of emptyPdf are wtitten over FinalMergePdf
save(): to save the pdf, final pdf will appear but won't process otherwise*/
fileSys.writeFileSync('./FinalMergedPdf.pdf', await emptyPdf.save());
}