Shrink huge pdf

How to Shrink Huge pdf files via Terminal on Ubuntu

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.

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

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

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

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

Next, we run the following command to perform the compression

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

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

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:

That should do the trick Results large.pdf: 6.3MB 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.

Image Credit

Allan Barku

People are often unreasonable and self-centered. Forgive them anyway. If you are kind, people may accuse you of ulterior motives. Be kind anyway. If you are honest, people may cheat you. Be honest anyway. If you find happiness, people may be jealous. Be happy anyway.

Add comment

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

Follow us

Don't be shy, get in touch. We love meeting interesting people and making new friends.

Most discussed

%d bloggers like this: