Optimize Pdfs with Ghostscript in Nodejs

In modern applications, each application is deal with some sort of PDFs to showcase Reports, Invoices, and whatnot. And to save on third-party clouds like AWS S3, Azure Blob storage, etc., without compressing or optimizing file size will cost you more.

Ghostscript is a great package that deals with optimizing pdf file size with just one line command.

First, make sure you’ve installed Ghostscript in your command. In Mac it looks like this:

brew install ghostscript

Then, install this tool globally:

npm install gsx-pdf-optimize –global       

Example:

gsx-pdf-optimize input.pdf output.pdf

To work around on NodeJS we are using The Child processes Module

Why Child Process?

Single-threaded, non-blocking performance in Node.js works great for a single process. But eventually, one process in one CPU is not going to be enough to handle the increasing workload of your application.

No matter how powerful your server may be, a single thread can only support a limited load.

The fact that Node.js runs in a single thread does not mean that we can’t take advantage of multiple processes and, of course, multiple machines as well.

Using multiple processes is the best way to scale a Node application. Node.js is designed for building distributed applications with many nodes. This is why it’s named Node. Scalability is baked into the platform and it’s not something you start thinking about later in the lifetime of an application.

We can easily spin a child process using Node’s child_process module and those child processes can easily communicate with each other with a messaging system.

Here is the code for optimizing pdf files using the gsx command using child_process.

 

Fig: 1 Example with NodeJS

For more options for optimization visit: https://github.com/mattdesl/gsx-pdf-optimize and https://www.ghostscript.com/