Paycheck_Processor.sh 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #! /bin/bash
  2. ###
  3. #This script is designed to take a batch file from WorkDay which contains multiple PDFs of paychecks
  4. #and convert it into individual PDF files with correct filenames.
  5. #Navigate to the directory of your destination, and call this script with the argument of the batch file.
  6. #This script will:
  7. #- Break apart the batch file to multiple incorrectly named files
  8. #- Read each file to obtain the Paycheck Date
  9. #- Rename each file to Payslip-[DATE_WITH_UNDERSCRORES].pdf
  10. #
  11. #The script works by reading the pdf and finding the last line number containing the text "Check Date".
  12. #It then reads the value of the next line, which should be the check date with slashes.
  13. #The script then replaces slashes with underscores per the current convention.
  14. #
  15. #If this script ever breaks, it will most likely be a change in paycheck format and thus a failure
  16. #to find the 'Check Date' field correctly.
  17. ###
  18. func_help() {
  19. echo ""
  20. echo "DESCRIPTION: Takes large file containing multiple paystubs and breaks it into individual pdf files correctly named."
  21. echo ""
  22. echo "USAGE: $0 [FILENAME]"
  23. echo ""
  24. }
  25. func_paydate() {
  26. text=$(pdftotext $1 -)
  27. #echo $text
  28. linenum=$(pdftotext $1 - | grep -n "Check Date" | tail -n 1 | cut -d: -f1)
  29. #echo $linenum
  30. ((linenum=linenum+1))
  31. date=$(pdftotext $1 - | sed -n "${linenum}p")
  32. }
  33. func_rename() {
  34. func_paydate $1
  35. newname=Payslip-${date////_}.pdf
  36. # echo "Renameing $1 to $newname"
  37. mv $1 $newname
  38. }
  39. func_burst() {
  40. echo "Bursting PDF into individual pages..."
  41. pdftk $1 burst
  42. echo "Cleaning up unused files..."
  43. rm doc_data.txt
  44. }
  45. if [ $# -eq 0 ]; then
  46. echo "No filename(s) supplied. Exiting."
  47. func_help
  48. exit 1
  49. fi
  50. if ! [ -f "$1" ]; then
  51. echo "Filename $1 does not exist. Exiting."
  52. func_help
  53. exit 1
  54. fi
  55. bulkfile=$1
  56. func_burst $bulkfile ## Break apart PDFs
  57. echo "Converting Filenames..."
  58. for f in *.pdf; do ## Loop through new files
  59. func_rename "$f" ## Rename each file
  60. done
  61. ## Get user input on weather to delete original bulk file
  62. read -n 1 -p "Delete original bulk file? y/n [Default yes]" del
  63. echo $del
  64. if [ $del == "y" ]; then
  65. echo "Deleting file $1"
  66. rm $bulkfile
  67. else
  68. echo "Leaving file $1 in place."
  69. fi
  70. echo "Script complete. Exiting."
  71. exit 0