Skip to content

Instantly share code, notes, and snippets.

@hogashi
Created November 9, 2016 17:05
Show Gist options
  • Save hogashi/8cbe922cf1bb31eb1639f7e12b1223fd to your computer and use it in GitHub Desktop.
Save hogashi/8cbe922cf1bb31eb1639f7e12b1223fd to your computer and use it in GitHub Desktop.
minimize PDF with JPG compressing
#!/bin/bash
# pdf-unlock-minimize.sh <- base dir is where this script is, you must run this from here
# |- pdf/
# | \- 01.pdf, 02.pdf,... <- put PDFs you want to minimize, then run this script
# |- dec/ <- decrypted PDFs
# |- jpgs/
# | \- 01/ <- compressed JPGs from raw/
# | \- raw/ <- displayed raw JPGs
# | \- 02/
# | ...
# \- mini/ <- minimized PDFs (you wanted to get)
PASSWORD="********"
mkdir -p dec mini
for i in pdf/*.pdf; do
PDFFILE=$(basename ${i})
PDFNUM=${PDFFILE%.pdf}
if [ -e mini/${PDFNUM}_mini.pdf ]; then
continue
fi
mkdir -p jpgs/${PDFNUM}/raw
qpdf --decrypt --password=${PASSWORD} ${i} dec/${PDFNUM}_dec.pdf
convert dec/${PDFNUM}_dec.pdf jpgs/${PDFNUM}/raw/${PDFNUM}-dest.jpg
for j in jpgs/${PDFNUM}/raw/${PDFNUM}-dest-*.jpg; do
JPGFILE=$(basename ${j})
JPGDIR=$(dirname ${j})
convert -resize 600x -unsharp 2x1.4+0.5+0 -colors 65 -quality 100 -verbose ${JPGDIR}/${JPGFILE} ${JPGDIR%raw}${JPGFILE}
done
convert ${JPGDIR%raw}*.jpg mini/${PDFNUM}_mini.pdf
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment