DEV Community

Cover image for Merging a pdf using Javascript
Kumaran KM
Kumaran KM

Posted on

Merging a pdf using Javascript

Have you heard about pdflib library, you didn't so here is the documentation page for you.

Pdflib package

Pdflib Package

So here what I have done, First I have installed the package and Add this package to json file as usual. Then I have imported that package on code, Then I have used catch syntax to check for errors.

Then we need to load the pdf that we need to merge with the help of the load method inside the asynchronous function. If you want to get pdf as a user input then you can try multer package, but for now, I don't need it. I will provide a documentation link for multer package below.

Multer package

Then, then with create() method, I have created a new empty document. And then I have added the first pdf file into that document with the help of copyPages() method. Once I have copied the first pdf, then I will concatenate the second pdf with the same copypage method. Then the pdf will get merged into that new document, and now it's time for naming a file. With the help of writeFileSync() I have synced the file and named it with .pdf extension. So here what the output looks like.

After merging
Alt Text

Thanks for taking the time to read my blog.
Happy coding :)

Top comments (2)

Collapse
 
supportic profile image
Supportic

Sometimes code speaks more than words. :D

Collapse
 
kumarankm4 profile image
Kumaran KM • Edited

I hope provided what you want :)
dev-to-uploads.s3.amazonaws.com/up...