#! /bin/bash ### #This script is designed to take a batch file from WorkDay which contains multiple PDFs of paychecks #and convert it into individual PDF files with correct filenames. #Navigate to the directory of your destination, and call this script with the argument of the batch file. #This script will: #- Break apart the batch file to multiple incorrectly named files #- Read each file to obtain the Paycheck Date #- Rename each file to Payslip-[DATE_WITH_UNDERSCRORES].pdf # #The script works by reading the pdf and finding the last line number containing the text "Check Date". #It then reads the value of the next line, which should be the check date with slashes. #The script then replaces slashes with underscores per the current convention. # #If this script ever breaks, it will most likely be a change in paycheck format and thus a failure #to find the 'Check Date' field correctly. ### func_help() { echo "" echo "DESCRIPTION: Takes large file containing multiple paystubs and breaks it into individual pdf files correctly named." echo "" echo "USAGE: $0 [FILENAME]" echo "" } func_paydate() { text=$(pdftotext $1 -) #echo $text linenum=$(pdftotext $1 - | grep -n "Check Date" | tail -n 1 | cut -d: -f1) #echo $linenum ((linenum=linenum+1)) date=$(pdftotext $1 - | sed -n "${linenum}p") } func_rename() { func_paydate $1 newname=Payslip-${date////_}.pdf # echo "Renameing $1 to $newname" mv $1 $newname } func_burst() { echo "Bursting PDF into individual pages..." pdftk $1 burst echo "Cleaning up unused files..." rm doc_data.txt } if [ $# -eq 0 ]; then echo "No filename(s) supplied. Exiting." func_help exit 1 fi if ! [ -f "$1" ]; then echo "Filename $1 does not exist. Exiting." func_help exit 1 fi bulkfile=$1 func_burst $bulkfile ## Break apart PDFs echo "Converting Filenames..." for f in *.pdf; do ## Loop through new files func_rename "$f" ## Rename each file done ## Get user input on weather to delete original bulk file read -n 1 -p "Delete original bulk file? y/n [Default yes]" del echo $del if [ $del == "y" ]; then echo "Deleting file $1" rm $bulkfile else echo "Leaving file $1 in place." fi echo "Script complete. Exiting." exit 0