Linux Commands and Scripts

Steps to Compress PDF file size in Linux CLI

In this article, we’ll show how to compress PDF file size in Linux CLI.

We’ll use pdf2ps and ps2pdf command to compress PDF files in Linux. pdf2ps and ps2pdfis are part of Ghostscript package. This solutions shrinks the size of PDF files. ps2pdf a script which convert Post Script format document into PDF format. pdf2ps uses gs to convert the Adobe Portable Document Format (PDF) file “input.pdf” to PostScript(tm) in “output.ps”.

Before performing following task take backup of the PDF file.

Let’s get started.

Install Ghostscript

Install Ghostscript, if it is not already installed in your system.

For CenOS and Redhat based:

# dnf install ghostscript -y

For Ubuntu based:

# apt install ghostscript -y

To verify the installation run following command:

# which ps2pdf

Output:

/usr/bin/ps2pdf

Compress PDF file

The first step is to convert the PDF file to a PostScript file using following command:

# pdf2ps -dLanguageLevel=3 file.pdf

Above command will create a file named file.ps and the size of the file.ps may larger than original file size.

Next, compress the file using following command:

# ps2pdf -dPDFSETTINGS=/ebook file.ps

Above command will overwrites original file. If you want to create another name for the new PDF, use following command:

# ps2pdf -dPDFSETTINGS=/ebook -sOutputFile=file2.pdf file.ps

Here we are using the /ebook. It setting for the compression is a good compromise between file size and quality.

Checkout man page

# man pdf2ps

# man ps2pdf

That’s it. We have successfully compress the PDF file.

In this article, we have seen how to compress PDF file size in Linux CLI.

Get a high performance dual E5 series dedicated server and cheap KVM VPS.

Related Articles