Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- ## FILE: pdfcut
- ##
- ## DESCRIPTION: Cuts selected pages out of a PDF file
- ##
- ## AUTHOR: Edward Comer
- ##
- ## DATE: 2013-06-27
- ##
- ## VERSION: 1.0
- ##
- ## Requires: zenity and pdf2ps tools
- ##
- ## USAGE: pdfcut -f n -l n sourcepath.pdf [destpath.pdf]
- ## OR: pdfcut sourcepatf.pdf
- ## OR: pdfcut
- ##
- Revision=1.0
- Date=20130627
- TF="/tmp/$(basename $0).$$.tmp"
- vflag=0
- first=
- last=
- out=
- file=
- Dflag=
- declare -r SCRIPT_NAME=$(basename "$BASH_SOURCE" .sh)
- guiargs() {
- zenity --forms --title="Cut PDF Pages" \
- --text="Enter information about PDF" \
- --separator="," \
- --add-entry="First Page to Cut" \
- --add-entry="Last Page to Cut" \
- --add-entry="Destination Filename" > /tmp/pdfcutgui.csv
- case $? in
- 0)
- echo "GUI Arguments accepted"
- ;;
- 1)
- echo "Nothing entered"
- bail
- ;;
- -1)
- echo "An unexpected error has occurred."
- bail
- ;;
- esac
- # Parse the saved parameters from CSV file created above
- cat /tmp/pdfcutgui.csv | while read line
- do
- first=`echo $line | cut -d',' -f1` # get first page num
- last=`echo $line | cut -d',' -f2` # get last page num
- dest=`echo $line | cut -d',' -f3` # get dest filename
- # This nonsense avoids variable scope of while loop
- echo first=$first > $TF
- echo last=$last >> $TF
- echo dest=\"$dest\" >> $TF
- done
- chmod 777 $TF
- . $TF
- rm /tmp/pdfcutgui.csv $TF
- }
- guisrc() {
- FILE=`zenity --file-selection --title="Select a File"`
- case $? in
- 0)
- echo "\"$FILE\" selected."
- source=$FILE
- ;;
- 1)
- echo "No file selected."
- bail
- ;;
- -1)
- echo "An unexpected error has occurred."
- bail
- ;;
- esac
- }
- ## exit the shell(default status code: 1) after printing the message to stderr
- bail() {
- echo -ne "$1" >&2
- exit ${2-1}
- }
- ## help message
- declare -r HELP_MSG="Usage: $SCRIPT_NAME [OPTION] Source.pdf [Dest.pdf]
- -h display this help and exit
- -f n first page is n
- -l n last page is n
- -V print version info
- -v verbose output
- Or, no arguments for GUI input
- "
- ## print the usage and exit the shell(default status code: 2)
- usage() {
- declare status=2
- if [[ "$1" =~ ^[0-9]+$ ]]; then
- status=$1
- shift
- fi
- bail "${1}$HELP_MSG" $status
- }
- while getopts hvVf:l: flag
- do
- case "$flag" in
- (h) help; exit 0;;
- (V) echo "$0: version $Revision ($Date)"; exit 0;;
- (v) vflag=1;;
- (f) first="$OPTARG";;
- (l) last="$OPTARG";;
- (*) usage;;
- esac
- done
- shift $(expr $OPTIND - 1)
- shift $(($OPTIND - 1))
- # If no source passed in cmd line, prompt for one
- if [[ "$#" -eq 0 ]];
- then
- guisrc
- else
- source=$1
- fi
- # Less than 2 args requires dialog to fetch parameters and dest
- if [[ "$#" -lt 2 ]];
- then
- guiargs # Use GUI to fetch parameters
- else
- if [ -n "$2" ]; then
- dest=$2
- fi
- fi
- #==========MAIN CODE BELOW==========
- if [ ! -r "$source" ]; # Does source file exist?
- then
- echo "File $source does not exist"
- usage
- bail
- fi
- if [ $vflag = 1 ]; then
- echo first = $first
- echo last = $last
- echo OPTIND = $OPTIND
- echo "Source file: " $source
- echo "Dest file: " $dest
- echo "pdftops $source - | psselect -p${first}-${last} | ps2pdf14 - $dest"
- exit 0
- fi
- echo Be patient - this can take a while
- pdftops $source - | psselect -p${first}-${last} | ps2pdf14 - $dest
- echo Job Complete, file $dest created
- exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement