generateBooklet.sh 2.58 KB
Newer Older
Giuseppe Lombardi's avatar
Giuseppe Lombardi committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
#!/bin/bash
# Imposition of a PDF document to generate a booklet
# target format is selectable (A5, A4, ...)
# default format is A4
#
# Depends on packages
#  * pdfjam
#  * poppler-utils
#
# Parameter :
#   nothing (anymore)
#
# Version history :
#   26/09/2012, V1.0 - Creation by N. Bernaerts
#   05/05/2014, V2.0 - Add target format selection (thanks to Robin idea)
#   2015 - Added Zenity eyecandy, and pages selection by Fernando Virdia

FILE=`zenity --file-selection --title="Select a File"`

case $? in
         0)
                echo "\"$FILE\" selected."
		;;
         1)
                echo "No file selected."
		exit
		;;
        -1)
                echo "An unexpected error has occurred."
		exit
		;;
esac

# suffix to be added at the end of booklet file
DOC_SUFFIX="book"
SPL_SUFFIX="splitted"

# determine file names
DOC_ORIGINAL="$FILE"
DOC_SPLITTED=$(echo "$FILE" | sed 's/\(.*\)\..*/\1/')-$SPL_SUFFIX.pdf
DOC_BOOKLET=$(echo "$FILE" | sed 's/\(.*\)\..*/\1/')-$DOC_SUFFIX.pdf

# select pages
PAGES=`zenity --entry --text "Pagine da stampare (lascia vuoto per tutte le pagine)"`

# select target format
ARR_FORMAT=('false' 'A2' 'false' 'A3' 'true' 'A4' 'false' 'A5' 'false' 'Letter' 'false' 'Executive' 'false' 'Legal')
TITLE=`basename "$FILE"`
TEXT="Booklet will be generated from this document.\nPlease select target format.\n"
FORMAT=`zenity --list --radiolist --width 350 --height 380 --title "$TITLE" --text "$TEXT" --column="Choice" --column="Format" ${ARR_FORMAT[@]}`

# if a format has been selected
if [ "$FORMAT" != "" ]
  then

  # determine number of pages of original PDF
  NUM_PAGES_ORIG=`pdfinfo "$DOC_ORIGINAL" | grep Pages | sed 's/^Pages:[ ]*\([0-9]*\).*$/\1/g'`

  if [ "$PAGES" == "" ]
    then
      PAGES="1-"
  fi

  # split file
  pdfjam "$DOC_ORIGINAL" $PAGES --outfile "$DOC_SPLITTED"
  
  # set target format as expected by pdfbook
  FORMAT_OPTION="--${FORMAT,,}paper"

  # determine number of pages of original PDF
  NUM_PAGES=`pdfinfo "$DOC_SPLITTED" | grep Pages | sed 's/^Pages:[ ]*\([0-9]*\).*$/\1/g'`

  # determine if there is a need to add blank pages at the end
  DIV_RESULT=`expr $NUM_PAGES % 4`
  if [ $DIV_RESULT -gt 0 ]
    then
    # add pages to get multiple of 4
    NUM_PAGES=`expr $NUM_PAGES + 4 - $DIV_RESULT`
  fi

  # generate document with proper page number (multiple of 4)
  zenity --info --text="Costruzione iniziata. Potrebbe metterci un po'. Chiudi questa finestra."
  pdfbook $FORMAT_OPTION --signature $NUM_PAGES --booklet true --landscape "$DOC_SPLITTED" -o "$DOC_BOOKLET"
  rm -rf "$DOC_SPLITTED"
  zenity --info --text="Libretto completato. Nuovo file: $DOC_BOOKLET"
fi