Shrink PDF size with this command line trick

Use Ghostscript to compress PDF files on Linux.
108 readers like this.
Files in a folder

My wife tutors students throughout the year, and recently she wanted to assign homework by scanning some workbook pages into a file and emailing them to her students. She asked for my help, so I used Simple Scan, a document scanning application for GNOME.

Simple Scan has two resolution settings, Images and Text, and saves files as PDFs. Unfortunately, the quality of the scans—the words, numbers, and graphics—is unsuitable using the Text setting, but the files are quite large—up to 5MB for just a few pages—using the Image setting. I found an answer to the latter problem using the commands pdf2ps and ps2pdf, which are part of the Ghostscript package. This solution shrinks the size of PDF files, making them easier to share via email.

The man pages for these commands are terse, and it was challenging to find good documentation for them and their settings. So I pieced together the following by combining information from the man pages with various other bits of advice I found on the internet.

How to shrink a PDF

First, make a backup of the original PDF file with a different name (in case you make an error somewhere).

In this example, I'll show you how to shrink a file named Lesson5.pdf. The first step is to convert it to a PostScript file by entering:

pdf2ps -dLanguageLevel=3 Lesson5.pdf

This creates a file named Lesson5.ps, and if you look at its size, you might be alarmed. For example, this example file was 3.1MB as a PDF and ballooned to 29MB as a PS file! Have faith.

Next, enter:

ps2pdf -dPDFSETTINGS=/ebook Lesson5.ps

This overwrites your previous Lesson5.pdf with a smaller file (a good reason for making a backup). If you want, you can specify another name for the new PDF with:

ps2pdf -dPDFSETTINGS=/ebook -sOutputFile=Lesson5b.pdf Lesson5.ps

I find using the /ebook setting for the compression is a good compromise between file size and quality. In this example, the compressed PDF was 715KB, about one-fourth the size of the original.

Use Ghostscript to compress a PDF

I also learned that I can use the gs command to accomplish everything in one go:

gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook \
-dNOPAUSE -dBATCH -dColorImageResolution=150 \
-sOutputFile=output.pdf someBigFile.pdf

But, looking at the gs man page, I don't think there is any way I would have come up with this on my own. To learn about the parameters not included in the man page, refer to the Ghostscript online documentation.

I think I'll stick with the two much shorter commands that are easier for me to remember.

What to read next
Greg Pittman
Greg is a retired neurologist in Louisville, Kentucky, with a long-standing interest in computers and programming, beginning with Fortran IV in the 1960s. When Linux and open source software came along, it kindled a commitment to learning more, and eventually contributing. He is a member of the Scribus Team.

3 Comments

Thanks for the tip Greg but bear in mid that pdf image files should be a last resort. Getting pdf files to play nicely with screen readers and similar assistive technology software can be a pain, if the pdf is not text it will never be accessible.

Thanks, Peter. Sometimes we don't have a choice about the format, and a PDF is all we can get.

Thanks, But will it effect quality of PDF ?

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.