DEV Community

Cover image for Want to compress your pdf files ?
Can Kahraman
Can Kahraman

Posted on

Want to compress your pdf files ?

Are you tired of scanning images and trying to shrink them under 25 mbs just so you can send them via email ? Look no further, I am here to save you from this trouble. (that kinda rhymed.)


This basic shell script uses ghostscript to compress your scanned pdfs significantly. Just yesterday I scanned 100 pages of documents and it was over 90 mbs. I searched for a way to compress them under 25 mbs and voila. Here I was with only 6 mbs of pdfs. Much wow. Such compression.


Why we need it,

  • Fast.
  • Ez.
  • No need to go online. Especially no need to upload company top-secret documents to any website you see on the Internet.
  • Possible huge compression without notable loss.

Command usage:

./shrinkpdf.sh in.pdf out.pdf

Don't forget to install ghostscript.


If you are in a situation exactly like me where there are 100 pdf files under one folder that you want to compress altogether, a simple for loop will suffice.

import os
import subprocess
import sys

infolder = sys.argv[1]
outfolder = "compressed_" + infolder
os.mkdir(outfolder)

for f in os.listdir(infolder):
    subprocess.run(["./shrinkpdf.sh", os.path.join(infolder, f), os.path.join(outfolder, f)])
Enter fullscreen mode Exit fullscreen mode

Run this python script,

python shrink.py pdfs/

and all of your pdfs will be put under compressed_pdfs/.


http://www.alfredklomp.com/programming/shrinkpdf/

All the relevant details and more, inside. I humbly wanted to let you know such a useful tool exists.

Top comments (0)