Shrink huge pdf

Shrink huge pdf via terminal on ubuntu

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 an online pdf compressors like Smallpdf (personal favourite) , I Love PDF , PDF compress and PDF compressor and the likes, then compressing your PDF 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. Usually as a back-end to a program such as ghostview, it can display PostScript and PDF documents in an X11 environment. Furthermore, it can render PostScript and PDF files as graphics to be printed on non-PostScript printers. Supported printers include common dot-matrix, inkjet and laser models.

Method 1.
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 step is to run the command to do the actual compression. Make sure you are located in the same directory as the file to be compressed. if your 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     

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 man gs command for more options.

Method 2.
Using Shrinkpdf. This method proved to reduce the file size impressively from megabytes to kilobytes. The only downside is that PDFs containing images came out a little blurred. Shrinkpdf is a simple wrapper around Ghostscript to shrink PDFs (as in reduce filesize) under Linux. The script feeds a PDF 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 my running the following command
sudo chmod a=r+w+x path_to_file/
replace path_to_file with the actual path where the file was saved.

Next we run the following command to perform the compression
./ 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 3.
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 sudo apt-get install ghostscript​  to install it.
Now the compression begins with converting the pdf to ps with the following
pdf2ps large.pdf  
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 following
ps2pdf small.pdf 
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

Follow us

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

Most discussed

%d bloggers like this: