3 Ways of Compressing a PDF file via the Terminal

PDF (Portable Document Format) is a file format for capturing and sending electronic documents in exactly the intended format.   Ever wanted to reduce the size of that big pdf file you have to send and you do not have the bandwidth to use online pdf compressors like Smallpdf (personal favourite) , I Love PDFPDF compress and PDF compressor and the likes, then compressing a PDF file via the terminal then becomes a viable option especially on a Unix based system like Ubuntu.

This article covers 3 ways of compressing a PDF file via the terminal with Ghostscript. Ghostscript is used for PostScript/PDF preview and printing. As a back-end to a program such as ghostview, usually, it can display PostScript and PDF documents in an X11 environment. Again, it can cause PostScript and PDF files as graphics to be printed on non-PostScript printers. Supported printers include common dot-matrix, inkjet and laser models.

Method One: Using Ghostscript

This method by my test gave me the best result for a pdf containing images. It reduces the file drastically yet still retaining great image quality.
Start by installing Ghostscript by running the following command if it is not already installed. Open your terminal, copy and paste the command below.

  sudo apt-get install ghostscript​

Next, run the command to do the actual compression. Make sure you are located in the same directory as the file to be compressed. If you are not in the same directory then you should know the full path to the file.  Copy and paste the exact command below

gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf

However, replace output.pdf with the intended output name. The input.pdf should be replaced with the name of the file to compress. This is only an example you can run the command below for more options

man gs

Method Two: Using Shrinkpdf

This method proved to reduce the file size impressively from megabytes to kilobytes. The only limitation is that PDFs containing images appeared a little blurred. Shrinkpdf is a simple wrapper around Ghostscript to shrink PDFs (as in reducing filesize) under Linux. A PDF is fed by the script through Ghostscript, which performs lossy recompression by such methods as downsampling the images to 72dpi. The result should be (but not always is) a much smaller file.

Download the script from here.

Make the script executable by running the following command

sudo chmod a=r+w+x path_to_file/shrinkpdf.sh

However, replace path_to_file with the actual path where the file was saved.

Next, we run the following command to perform the compression

./shrinkpdf.sh input.pdf output.pdf

Replace input.pdf with the name of the file to compress and output.pdf with the name to save the file. NB the correct path to the file should be provided.

Method Three: Using Ghostscript to Convert pdf to ps

The third method, just as the first method does a good job with PDFs with images. This process converting pdf to ps file and then reversing the process.
Make sure Ghostscript is installed. if not installed run the command below

sudo apt-get install ghostscript​ 

Now, the compression begins with converting the pdf to ps with the command below:

pdf2ps large.pdf very_large.ps  

Replace large.pdf with the file to compress, the generate ps file would be a very large file.
Next is to convert the ps file back to pdf with the command below:

ps2pdf very_large.ps small.pdf 

That should do the trick Results large.pdf: 6.3MB very_large.ps: 53.4MB small.pdf: 2.4MB. Looks like pretty good compression to me.


In conclusion, all the methods would get the job done. Method 2 does a lossy recompression while the other two methods perform a lossless recompression. Method 1 and 3 are great for pdf with images, but if method 1 would give you the smallest file size possible.

ubuntugeek.com    alfredklomp.com    pandemoniumillusion.wordpress.com
Image Credit cisdem.com

Comments to: How to Shrink Huge pdf files via Terminal on Ubuntu

Your email address will not be published. Required fields are marked *

Attach images - Only PNG, JPG, JPEG and GIF are supported.

This site uses Akismet to reduce spam. Learn how your comment data is processed.


Welcome to Typer

Brief and amiable onboarding is the first thing a new user sees in the theme.
Join Typer
Registration is closed.
%d bloggers like this: