Coders Packet

PNG/JPG/JPEG Images to a Single PDF using NodeJS, Express and ImageMagick Library.

By Anushka Priya

Single or Multiple images can be converted to a single pdf file. Just have to select image/images from your pc and it will be converted and downloaded in the form of a PDF file.

Requirements -

Make sure you have VS-Code installed along with Node. Also, you have ImageMagick and FFmpeg installed on your computer. These two libraries are necessary in order to build this application. 

We will be executing a shell command inside our Nodejs application. For executing this we have a simple built-in library in NodeJS called child-process. In the terminal, do npm init. This is going to initialize the folder for us which can do all the default values entered a bunch of times. Then, install all the necessary packages -  npm i express multer nodemon . Multer would be useful for uploading image files on the server and whenever we make some changes in our file, nodemon will automatically refresh our application.

npm run devStart will start our nodemon index.js application.

Our file index.js will contain all the posts and the get request made throughout. We require express and set up our port 3000. Our file will just open in the localhost::3000.  We then send out an HTML response file which has a form kind of structure. It has a single input type file where we are receiving input from the user and we process it inside our express. This will go to the section /merge which will be a POST request and the encoding type will be multipart/form-data. We then programmatically create the public file and inside this, we will have the uploads folder where we will have our image files.

We will then write code for uploading image files to the server using multer.   We are making use of the diskStorage function of multer which has two arguments - destination, filename.  We have used a custom function imageFilter to validate the input that is submitted by the user(only accepting the files which are PNG, JPG, and JPEG) . If files other than PNG, JPEG, and JPG are selected then an error will be thrown (only .png, .jpg and.jpeg format is allowed).

After the download is complete, unlinkSync function will delete the files. We can see this when we refresh our application.

 

Optional-

If case you want to deploy this site using git or GitHub and don't want to send all the modules up to the server, you just want to send package.json. So, with .gitignore we ignore all our node modules.

 

Download project

Reviews Report

Submitted by Anushka Priya (Anushkaa)

Download packets of source code on Coders Packet