Skip to content

Commit d77f0d6

Browse files
authored
Added code to merge two pdfs using javascript (#981)
* Added code to merge two pdfs using javascript * Update README.md * Update README.md * Update merge.js * Update merge.js
1 parent 5dabce3 commit d77f0d6

File tree

4 files changed

+107
-0
lines changed

4 files changed

+107
-0
lines changed
9.28 KB
Binary file not shown.
9.86 KB
Binary file not shown.

JavaScript/Merge_pdfs/README.md

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Merging two pdf files
2+
This script can be used to merge any two pdf files into one using JavaScript. Run merge.js and upload the pdf files which are to be merged and the ouput will be a single pdf file obtained by merging the uploaded pdfs.
3+
4+
## Module Required
5+
* pdf-lib
6+
pdf-lib npm is a tool to modify exsisting pdfs using Node.js.
7+
8+
## Installation of the above module
9+
Install the module using<!-- Italics-->*npm install pdf-lib* command.
10+
The module will be added to <!-- Italics-->*package.json*
11+
12+
## How to merge pdfs
13+
* Download or clone the repository.
14+
* Install the requird module (pdf-lib).
15+
* Run merge.js and add the pdfs which are to be merged.
16+
* The merged pdf(named final_merged_pdf.pdf) will appear in the same directory.
17+
18+
## Output
19+
The files I used <!-- Italics-->*hello.pdf* and <!-- Italics-->*world.pdf* have been added to the folder <!-- Italics-->*Merged_pdfs*.
20+
Here's the ouput:
21+
22+
## Pdf1
23+
24+
![hello](https://user-images.githubusercontent.com/60272753/114947077-191d3a80-9e6a-11eb-9c0a-d76b1a9b4c26.PNG)
25+
26+
27+
## Pdf2
28+
29+
![world](https://user-images.githubusercontent.com/60272753/114947110-2508fc80-9e6a-11eb-83b8-47477de381ef.PNG)
30+
31+
32+
33+
## Merged Pdf
34+
35+
https://user-images.githubusercontent.com/60272753/114947119-29cdb080-9e6a-11eb-9858-ddb2eb41d702.mp4

JavaScript/Merge_pdfs/merge.js

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

0 commit comments

Comments
 (0)