Compressing PDF files from your Mac or Linux terminal with Ghostscript
I arrived at this solution recently when looking to compress a massive print-quality PDF for the web.
If you search for “compressing pdf files on mac” you’ll get lots of bad results. There are lots of paid applications out there that claim to be able to do this, but they’re (obviously) not free.
If you’re not afraid to get your hands dirty in the terminal you can compress your PDF files from your terminal with a single command. If you find youself needing to do this often, I’ve got a function that I put together to make this easier.
The method I’m using requires ghostscript. Ghostscript is a high quality, high performance Postscript and PDF interpreter and rendering engine [1].
If you’re using a Mac, you can install ghostscript using brew:
brew install ghostscript
If you’re using Linux you can install ghostscript from aptitude, or your package manager of choice [2].
Now, you can use ghostscript to compress your PDF file for the web:
gs -sDEVICE=pdfwrite -dNOPAUSE -dQUIET -dBATCH -dPDFSETTINGS=/screen -dCompatibilityLevel=1.4 -sOutputFile=output.pdf input.pdf
That’s not exactly a memorable command, so I’ve made a function that you can add to your ~/.bash_profile to allow you to easily compress PDF files from your terminal:
# Usage: compresspdf [input file] [output file] [screen*|ebook|printer|prepress]
compresspdf() {
gs -sDEVICE=pdfwrite -dNOPAUSE -dQUIET -dBATCH -dPDFSETTINGS=/${3:-"screen"} -dCompatibilityLevel=1.4 -sOutputFile="$2" "$1"
}
Now you can simply run the following command:
compresspdf "Massive.pdf" "Small.pdf"
Using this command I managed to compress an A3 size PDF (originally around 9MB) down to just under 1MB, which is perfect for the web.
Related posts
- Replacing the macOS audio switcher with SwiftBar
The built-in audio output menu in macOS has always felt a bit limited. The icons aren’t very helpful, the device names can’t be customised, and if you’ve got a few different outputs, it becomes tricky to quickly tell what’s what.
- Installing Ubuntu on MacBookPro 8,2
This MacbookPro8,2 has been through a lot. I did some small upgrades back in 2012, then in 2014 it experienced some issues with the dedicated GPU that many Macbook’s of this model experienced. For a while I managed to keep it running my forcefully disabling the dedicated GPU with some EFI trickery, and running some form of Linux for a while (I think I kept it going with Elementary OS). In 2016 (or there abouts) I took the drastic measure of baking the mainboard in the oven to fix the dedicated GPU issue (by reflowing/reballing the solder), reinstalled macOS and then (besides a short stint as my wife’s computer) the MacBookPro8,2 ended up back on a shelf. until today! out of boredom I’ve decided to revisit this old beast. Installing Ubuntu on my old early-2011 MacBookPro8,2 Install rEFInd - https://www.rodsbooks.com/refind/ Create a Ubuntu USB (I used Rufus on my Windows PC, with the Ubuntu 18.04.2 iso) Boot the iso (hold alt key to get the rEFInd menu, select the USB) And this is where the first headache is. To make Ubuntu play nicely, you need to edit the boot commands so that certain chipset features are poked and enabled. At the Ubuntu boot menu, select the option you want (e.g. Live installer) and press e, which will bring you to the editor. First disable the AMD graphics card by adding the following lines after set gfxpayload=keep outb 0x728 1 outb 0x710 2 outb 0x740 2 outb 0x750 0 Next add the following after quiet splash on the boot command: i915.lvds_channel_mode=2 i915.modeset=1 i915.lvds_use_ssc=0 Then press F10 to boot, and you should successfully boot to the Ubuntu installer. From here you can install Ubuntu. You can delete the macOS partitions if you like, just make sure you keep the ~200mb EFI partition, and create your Ubuntu partitions after that. You’ll need to do this again for your first boot after you’ve got Ubuntu installed, and then you can edit your Grub file permenantly using grub-customizer or something.
- Launching Sketch Toolbox from inside Sketch
I’ve been making use of Sketch a lot lately. One of the coolest features of Sketch is it’s plugin support. I’m using a tool called Sketch Toolbox to manage plugins in Sketch – this functions a lot like Package Control for Sublime Text, but it’s a stand-alone application.
Discussion
- 2016
- K
Hi, this function is amazing but faces problem when it has to handle a file path with blank spaces.
Join the conversation
Reply from your own site with a Webmention (how?).
