123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- #!/bin/bash
- #/##################################################################\
- #| Script to merge multiple pdf's in one file adding white pages |
- #| if the the number of each one is odd. |
- #| Guzmán Castanedo (guzman@castanedo.es) |
- #| February 2017 |
- #| Licence: GPL 3.0 -> https://www.gnu.org/licenses/gpl-3.0.en.html |
- #\##################################################################/
- function usage {
- printf "USAGE:\t"$(basename "$0")" [PDFin.pdf] <PDFout.pdf>\n"
- printf "INFO:\n"
- printf "\tMerge PDFin.pdf in one PDFout.pdf adding white pages if PDFin.pdf\n"
- printf "\thas an odd number of pages.\n"
- printf "OPTIONS:\n"
- printf "\t-h, --help help\n"
- printf "\t-v, --verbose verbose mode\n"
- printf "\t-y, --yes enable overwrite mode\n"
- }
- #Check if pdfunite, pdfinfo and convert are installed
- echo
- if [ ! -x /usr/bin/pdfinfo ]; then
- printf "ERROR: pdfinfo is not installed\n"
- exit 1
- fi
- if [ ! -x /usr/bin/pdfunite ]; then
- printf "ERROR: pdfunite is not installed\n"
- exit 1
- fi
- if [ ! -x /usr/bin/convert ]; then
- printf "ERROR: convert is not installed\n"
- exit 1
- fi
- #Parse args
- verbose=false
- overwrite=false
- #With getopts
- TEMP="$(getopt -q -o hvy --long help,verbose,yes -n "$(basename "$0")" -- "$@")"
- eval set -- "$TEMP"
- unset TEMP
- while true; do
- case "$1" in
- -h|--help)
- #Help
- usage
- exit
- ;;
- -v|--verbose)
- #Verbose
- verbose=true
- shift
- ;;
- -y|--yes)
- #Overwrite
- overwrite=true
- shift
- ;;
- --)
- #Last one
- shift
- break
- ;;
- *)
- #Unspected
- usage
- echo "ERROR: Invalid option $1"
- exit 1
- ;;
- esac
- done
- #Create white pdf (random name)
- whitepdf=$(cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 32 | head -n 1)
- whitepdf="$whitepdf.pdf"
- while [ -e $whitepdf ]; do
- #Extremly rare, but possible->Choose another one
- whitepdf=$(cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 32 | head -n 1)
- whitepdf="$whitepdf.pdf"
- done
- #Make whitepdf with convert
- convert xc:none -page A4 $whitepdf
- if [ $verbose = true ];then
- printf "White PDF:\t%s\n" $whitepdf
- fi
- #Parse arguments
- unite="pdfunite"
- cont=0
- for i in $@; do
- cont=$((cont+1))
- unite+=" $i"
- if [ $cont -lt $# ]; then
- temp=$(pdfinfo $i 2>/dev/null)
- if [ ! $? -eq 0 ]; then
- printf "ERROR: %s aren't a valid PDF file.\n" $i
- usage
- exit 1
- fi
- pages=$(pdfinfo $i|grep Pages:|awk '{print $2}')
- if [ $verbose = true ]; then
- printf "PDFin:\t%s (%s Pages)\n" $i $pages
- fi
- if [ ! $((pages%2)) -eq 0 ]; then
- #Odd number
- unite+=" $whitepdf"
- fi
- else
- #Last one
- if [ $overwrite = false ]; then
- if [ -e $i ]; then
- printf "Warning: Output file $i exists. Overwrite? [y/n]: "
- read confirm
- case ${confirm:0:1} in
- y|Y)
- ;;
- *)
- rm $whitepdf
- exit 1
- ;;
- esac
- fi
- fi
- #Execute pdfunite
- $($unite)
- if [ ! $? -eq 0 ];then
- printf "Warning: something was wrong\n"
- else
- printf "Creation Successful\n"
- if [ $verbose = true ]; then
- pages=$(pdfinfo $i|grep Pages:|awk '{print $2}')
- printf "PDFOut:\t$s (%s Pages)\n" $i $pages
- fi
- fi
- fi
- done
- #Remove whitepdf
- if [ $verbose = true ]; then
- printf "Deletting temporal files\n"
- fi
- rm $whitepdf
|