Skip to content

Instantly share code, notes, and snippets.

@innermond
Created February 21, 2022 14:53
Show Gist options
  • Save innermond/b100cdd01441c2c1f1fd32d2a6cb5e6c to your computer and use it in GitHub Desktop.
Save innermond/b100cdd01441c2c1f1fd32d2a6cb5e6c to your computer and use it in GitHub Desktop.
#~/bin/bash
shopt -s extglob
# usage ./.partition <$1 path to directory taht contains image files to be converted to pdf> <$2 number of images converted in a lap> <$3 scale "x y" optional> >
# ./.partition path/to/directory 10 "0.9 0.9"
# dependencies: pdftk and cpdf
cd "$1"
i=0
ff=""
# natural order
for fn in $(ls -vw0 *.jpg); do
# comcatenate filenames
ff="${ff} ${fn}"
# increment
let i=$i+1
if ((i % $2 == 0)); then
echo "convert $ff $i.pdf"
convert $ff $i.pdf
# reset concatenated
ff=""
fi
done
# exiting from for cycle left some work to be done
if [ ! -z "${ff}" ]; then
echo "convert $ff $i.pdf"
convert $ff $i.pdf
fi
chars=abcd1234ABCD
rnd=""
for i in {1..8} ; do
rnd="${rnd}${chars:RANDOM%${#chars}:1}"
done
random_name="${rnd}.pdf"
# concatenate
pdftk $(ls -vw0 *.pdf) cat output $random_name
# remove all individual pdf if concatenation succeeded
status=$?
if [ $status -eq 0 ]; then
echo "removing individual pdf file"
rm !("$rnd").pdf
fi
# scale if requested
status=$?
if [[ $status -eq 0 && ! -z $3 ]]; then
echo "scaling pdf file"
cpdf -scale-page="$3" "$random_name" -o "${random_name}.scale"
mv "${random_name}.scale" "$random_name"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment