Advertisement
celem

pdfcut

Jun 27th, 2013
476
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.59 KB | None | 0 0
  1. #!/bin/bash
  2. ## FILE: pdfcut
  3. ##
  4. ## DESCRIPTION: Cuts selected pages out of a PDF file
  5. ##
  6. ## AUTHOR: Edward Comer
  7. ##
  8. ## DATE: 2013-06-27
  9. ##
  10. ## VERSION: 1.0
  11. ##
  12. ## Requires: zenity and pdf2ps tools
  13. ##
  14. ## USAGE: pdfcut -f n -l n sourcepath.pdf [destpath.pdf]
  15. ##    OR: pdfcut sourcepatf.pdf
  16. ##    OR: pdfcut
  17. ##
  18. Revision=1.0
  19. Date=20130627
  20.  
  21. TF="/tmp/$(basename $0).$$.tmp"
  22. vflag=0
  23. first=
  24. last=
  25. out=
  26. file=
  27. Dflag=
  28.  
  29. declare -r SCRIPT_NAME=$(basename "$BASH_SOURCE" .sh)
  30.  
  31. guiargs() {
  32.     zenity --forms --title="Cut PDF Pages" \
  33.     --text="Enter information about PDF" \
  34.     --separator="," \
  35.     --add-entry="First Page to Cut" \
  36.     --add-entry="Last Page to Cut" \
  37.     --add-entry="Destination Filename"  > /tmp/pdfcutgui.csv
  38.    
  39.     case $? in
  40.     0)
  41.         echo "GUI Arguments accepted"
  42.     ;;
  43.     1)
  44.         echo "Nothing entered"
  45.         bail
  46.     ;;
  47.     -1)
  48.         echo "An unexpected error has occurred."
  49.         bail
  50.     ;;
  51.     esac
  52.  
  53. # Parse the saved parameters from CSV file created above
  54.     cat /tmp/pdfcutgui.csv | while read line
  55.     do
  56.         first=`echo $line | cut -d',' -f1`  # get first page num
  57.         last=`echo $line | cut -d',' -f2`   # get last page num
  58.         dest=`echo $line | cut -d',' -f3`   # get dest filename
  59.         # This nonsense avoids variable scope of while loop
  60.         echo first=$first > $TF
  61.         echo last=$last >> $TF
  62.         echo dest=\"$dest\" >> $TF
  63.     done
  64.     chmod 777 $TF
  65.     . $TF
  66.             rm /tmp/pdfcutgui.csv $TF
  67. }
  68.  
  69. guisrc() {
  70. FILE=`zenity --file-selection --title="Select a File"`
  71.  
  72. case $? in
  73.          0)
  74.                 echo "\"$FILE\" selected."
  75.                 source=$FILE
  76.                 ;;
  77.          1)
  78.                 echo "No file selected."
  79.                 bail
  80.                 ;;
  81.         -1)
  82.                 echo "An unexpected error has occurred."
  83.                 bail
  84.                 ;;
  85. esac
  86. }
  87.  
  88. ## exit the shell(default status code: 1) after printing the message to stderr
  89. bail() {
  90.     echo -ne "$1" >&2
  91.     exit ${2-1}
  92. }
  93.  
  94. ## help message
  95. declare -r HELP_MSG="Usage: $SCRIPT_NAME [OPTION] Source.pdf [Dest.pdf]
  96.  -h    display this help and exit
  97.  -f n  first page is n
  98.  -l n  last page is n
  99.  -V    print version info
  100.  -v    verbose output
  101.  
  102.  Or, no arguments for GUI input
  103. "
  104.  
  105. ## print the usage and exit the shell(default status code: 2)
  106. usage() {
  107.     declare status=2
  108.     if [[ "$1" =~ ^[0-9]+$ ]]; then
  109.         status=$1
  110.         shift
  111.     fi
  112.     bail "${1}$HELP_MSG" $status
  113. }
  114.  
  115.  
  116. while getopts hvVf:l: flag
  117. do
  118.     case "$flag" in
  119.     (h) help; exit 0;;
  120.     (V) echo "$0: version $Revision ($Date)"; exit 0;;
  121.     (v) vflag=1;;
  122.     (f) first="$OPTARG";;
  123.     (l) last="$OPTARG";;
  124.     (*) usage;;
  125.     esac
  126. done
  127. shift $(expr $OPTIND - 1)
  128.  
  129. shift $(($OPTIND - 1))
  130.  
  131. # If no source passed in cmd line, prompt for one
  132. if [[ "$#" -eq 0 ]];
  133. then
  134.     guisrc
  135. else
  136.     source=$1
  137. fi
  138.  
  139. # Less than 2 args requires dialog to fetch parameters and dest
  140. if [[ "$#" -lt 2 ]];
  141.     then
  142.         guiargs         # Use GUI to fetch parameters
  143.     else
  144.     if [ -n "$2" ]; then
  145.         dest=$2
  146.     fi
  147. fi
  148.  
  149. #==========MAIN CODE BELOW==========
  150.  
  151. if [ ! -r "$source" ]# Does source file exist?
  152. then
  153.     echo "File $source does not exist"
  154.     usage
  155.     bail
  156. fi
  157.  
  158. if [ $vflag = 1 ]; then
  159.     echo first = $first
  160.     echo last = $last
  161.     echo OPTIND = $OPTIND
  162.     echo "Source file: " $source
  163.     echo "Dest   file: " $dest
  164.     echo "pdftops $source - | psselect -p${first}-${last} | ps2pdf14 - $dest"
  165.     exit 0
  166. fi
  167.  
  168. echo Be patient - this can take a while
  169.  
  170. pdftops $source - | psselect -p${first}-${last} | ps2pdf14 - $dest
  171. echo Job Complete, file $dest created
  172. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement