Skip to content

Instantly share code, notes, and snippets.

@uriel1998
Created December 1, 2024 09:22
Show Gist options
  • Save uriel1998/c29629d76b063000057a05db18b94cb4 to your computer and use it in GitHub Desktop.
Save uriel1998/c29629d76b063000057a05db18b94cb4 to your computer and use it in GitHub Desktop.
doitall.sh
#!/bin/bash
# https://unix.stackexchange.com/a/407146
if [ ! -f "${1}" ];then
echo "filename must be first argument"
exit 99
fi
pdftk "${1}" burst
rotate=$(cat doc_data.txt | grep PageMediaRotation | head -1 | awk '{print $2}')
if [ "$rotate" == "90" ]; then
pw=`cat doc_data.txt | grep PageMediaDimensions | head -1 | awk '{print $3}'`
ph=`cat doc_data.txt | grep PageMediaDimensions | head -1 | awk '{print $2}'`
else
pw=`cat doc_data.txt | grep PageMediaDimensions | head -1 | awk '{print $2}'`
ph=`cat doc_data.txt | grep PageMediaDimensions | head -1 | awk '{print $3}'`
fi
w2=$(echo "scale=0; $pw / 2" | bc )
echo "$w2"
w2px=$( echo "scale=0;$w2*10" | bc)
hpx=$(echo "scale=0;$ph*10" | bc)
for f in pg_[0-9]*.pdf ; do
lf=left_$f
rf=right_$f
gs -o ${lf} -sDEVICE=pdfwrite -g${w2px}x${hpx} -c "<</PageOffset [0 0]>> setpagedevice" -f ${f}
gs -o ${rf} -sDEVICE=pdfwrite -g${w2px}x${hpx} -c "<</PageOffset [-${w2} 0]>> setpagedevice" -f ${f}
done
ls -1 [lr]*_[0-9]*pdf | sort -n -k3 -t_ > fl
pdftk `cat fl` cat output newfile.pdf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment