How to Compress (More than one at a time) PDF files from terminal

I work with lots of rather large imaged documents ranging from a few bites to multiple gigs of data per file. I need these documents to take up less storage space and especially to be more electronically portable and transmittable.

For single documents, I've been using:

ps2pdf -dPDFSETTINGS=/ebook inputfile.pdf outputfile.pdf

Which code I found here on AskUbuntu, and it works great with individual files yielding around a 1 to 0.42 compression ratio resulting in an output file of less than half of the original size while maintaining an acceptable level of reading quality.

I want to now use this code for my pdf library of documents to convert ALL my previous pdf files using a single command from the terminal. My strategic objective is to avoid having to compress each individual pdf file individually; I want to type a line of code, hit enter, and then go do something else productive and come back and find this done, and I'm hoping to mogrify the files in place (as opposed to converting them, which would produce doubles of each file) in order to avoid having to delete the old files.

All these files are together in a single folder.

Any suggestions?

Thank you in advance!

3

3 Answers

You can use the same command or mogrify as you've said, just by adding a bit of bash.

find $HOME/PDF_Lib -iname '*.pdf' | while read pdf; do ps2pdf -dPDFSETTINGS=/ebook "$pdf" "${pdf:0:(-4)}_new.pdf"; done

OR

find $HOME/PDF_Lib -iname '*.pdf' | while read pdf; do mogrify -resample 150 -compress JPEG -quality 80 "$pdf"; done

create sh file in PDF directory and add this lines:

script.sh

for f in *.pdf
do echo $f; convert -compress Zip $f a$f;
done

now open your terminal in PDF FILES directory:

$ chmod +x script.sh
$ ./script.sh

now this script will compress every PDF file in your directory

I used the following code with success on iOS terminal to compress multiple PDFs recursively. I posted it because I couldn't find something that worked for me with a simple copy and paste.

find . -name '*.pdf' | while read pdf; do gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dNOPAUSE -dQUIET -dBATCH -sOutputFile="${pdf}_new.pdf" "$pdf"; done

Note you may want a different output quality, sou you can change the -dPDFSETTINGS parameter as follows:

-dPDFSETTINGS=/screen: lower quality, smaller size.
-dPDFSETTINGS=/ebook: for better quality, but slightly larger pdfs.
-dPDFSETTINGS=/prepress: output similar to Acrobat Distiller "Prepress Optimized" setting.
-dPDFSETTINGS=/printer: selects output similar to the Acrobat Distiller "Print Optimized" setting.
-dPDFSETTINGS=/default: selects output intended to be useful across a wide variety of uses, possibly at the expense of a larger output file.

2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like