Node.js Express Script to Compress PDF Online to Smaller Size Using GhostScript Library Full Project With Deployment 2021

You are currently viewing Node.js Express Script to Compress PDF Online to Smaller Size Using GhostScript Library Full Project With Deployment 2021




Welcome folks today in this post we will be talking about how to compress pdf documents to smaller size in node.js and express using a library called as ghostscript. The full source code of application will be below there.


Live Demo



You can find the Live Demo of Compress PDF on my Website









Get Started




In order to get started we need to initialize the following commands


npm init -y


This will create the package.json file for your node.js project

And then you need to install the following dependencies inside your node.js project


npm i express


npm i multer


npm i ejs


npm i nodemon


After installing all these dependencies we need to make a index.js file and copy paste the following code






So in this block of code we are starting a simple express app at port 5000. For this we are including the express library to create a simple http server.

When you execute this app by running the command


node index.js


You will see your node.js application will start on port 5000




Uploading PDF File For Compression




Now we will be uploading the PDF File from the user so first of all we should have some sort of ui to present to the user so that it can upload the file.

Now we need to set the view engine of our express app to ejs Just write this line inside index.js file as shown below.



Now create a simple get request inside the index.js file as shown below.




After this Just make a views folder inside the root directory of your project and inside that make a file called as compresspdf.ejs and copy paste the following code which is shown below








Now go again inside the index.js file and make a POST to handle the uploading of the pdf file when the user uploads the pdf file and click the upload button




Now we need to make this middleware function which we are using compresspdfupload in order to upload pdf files on the server using multer


So first of all we will include multer inside our application like this at the very top of index.js file




Now we need to make the multer storage object where we will define on which directory we will store the uploaded files and which filename will be used. Before this we need to create the directory called as public inside the root of the application




Inside the public directory create another directory called as uploads which will actually store the files for us


Now we need to set this public directory as static in express. This is very important to tell express it is static directory.

Copy this line of code inside index.js to tell express




Now we will create the multer storage object like this




And after this we will write the multer file filter which will only allow pdf files to upload as we are working only with pdf files. Copy paste the following code inside index.js




Now After this we need to create the actual multer instance passing the storage and filter like this




As you can see we are only uploading one file at a time so we are using the single method to upload only single file at a time.


Now if you run the app it will successfully upload the pdf  file with the progressbar


Now we need to write another post request to actually compress the pdf file like this


For this purpose we are using a open source library called as ghostscript you can go to their official website to download the software and then you can compress pdf files on the go from the command line.

And now we will be using a special module in node.js to execute shell commands in node.js. The module name is child_process. So we need to include this in index.js




Inside this child_process module there is a method called as exec which executes shell commands programmatically in node.js


Now there is a command in ghostscript which is used to compress pdf files. The command is shown below.




So here you will replace the inputfile and outputfile paths respectively.


Now just copy paste the following code inside index.js to execute the above command using exec in node.js





So now at last we need to make a get request to simply download the file once the conversion completes. So below just write this block of code to do this




So here we are using two dependencies extra which are built in node.js which are fs and path modules. Just include it at the very top of the file.



Full Source Code (Index.js File)










Leave a Reply