pdfmerge-smart 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #!/bin/bash
  2. #/##################################################################\
  3. #| Script to merge multiple pdf's in one file adding white pages |
  4. #| if the the number of each one is odd. |
  5. #| Guzmán Castanedo (guzman@castanedo.es) |
  6. #| February 2017 |
  7. #| Licence: GPL 3.0 -> https://www.gnu.org/licenses/gpl-3.0.en.html |
  8. #\##################################################################/
  9. function usage {
  10. printf "USAGE:\t"$(basename "$0")" [PDFin.pdf] <PDFout.pdf>\n"
  11. printf "INFO:\n"
  12. printf "\tMerge PDFin.pdf in one PDFout.pdf adding white pages if PDFin.pdf\n"
  13. printf "\thas an odd number of pages.\n"
  14. printf "OPTIONS:\n"
  15. printf "\t-h, --help help\n"
  16. printf "\t-v, --verbose verbose mode\n"
  17. printf "\t-y, --yes enable overwrite mode\n"
  18. }
  19. #Check if pdfunite, pdfinfo and convert are installed
  20. echo
  21. if [ ! -x /usr/bin/pdfinfo ]; then
  22. printf "ERROR: pdfinfo is not installed\n"
  23. exit 1
  24. fi
  25. if [ ! -x /usr/bin/pdfunite ]; then
  26. printf "ERROR: pdfunite is not installed\n"
  27. exit 1
  28. fi
  29. if [ ! -x /usr/bin/convert ]; then
  30. printf "ERROR: convert is not installed\n"
  31. exit 1
  32. fi
  33. #Parse args
  34. verbose=false
  35. overwrite=false
  36. #With getopts
  37. TEMP="$(getopt -q -o hvy --long help,verbose,yes -n "$(basename "$0")" -- "$@")"
  38. eval set -- "$TEMP"
  39. unset TEMP
  40. while true; do
  41. case "$1" in
  42. -h|--help)
  43. #Help
  44. usage
  45. exit
  46. ;;
  47. -v|--verbose)
  48. #Verbose
  49. verbose=true
  50. shift
  51. ;;
  52. -y|--yes)
  53. #Overwrite
  54. overwrite=true
  55. shift
  56. ;;
  57. --)
  58. #Last one
  59. shift
  60. break
  61. ;;
  62. *)
  63. #Unspected
  64. usage
  65. echo "ERROR: Invalid option $1"
  66. exit 1
  67. ;;
  68. esac
  69. done
  70. #Create white pdf (random name)
  71. whitepdf=$(cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 32 | head -n 1)
  72. whitepdf="$whitepdf.pdf"
  73. while [ -e $whitepdf ]; do
  74. #Extremly rare, but possible->Choose another one
  75. whitepdf=$(cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 32 | head -n 1)
  76. whitepdf="$whitepdf.pdf"
  77. done
  78. #Make whitepdf with convert
  79. convert xc:none -page A4 $whitepdf
  80. if [ $verbose = true ];then
  81. printf "White PDF:\t%s\n" $whitepdf
  82. fi
  83. #Parse arguments
  84. unite="pdfunite"
  85. cont=0
  86. for i in $@; do
  87. cont=$((cont+1))
  88. unite+=" $i"
  89. if [ $cont -lt $# ]; then
  90. temp=$(pdfinfo $i 2>/dev/null)
  91. if [ ! $? -eq 0 ]; then
  92. printf "ERROR: %s aren't a valid PDF file.\n" $i
  93. usage
  94. exit 1
  95. fi
  96. pages=$(pdfinfo $i|grep Pages:|awk '{print $2}')
  97. if [ $verbose = true ]; then
  98. printf "PDFin:\t%s (%s Pages)\n" $i $pages
  99. fi
  100. if [ ! $((pages%2)) -eq 0 ]; then
  101. #Odd number
  102. unite+=" $whitepdf"
  103. fi
  104. else
  105. #Last one
  106. if [ $overwrite = false ]; then
  107. if [ -e $i ]; then
  108. printf "Warning: Output file $i exists. Overwrite? [y/n]: "
  109. read confirm
  110. case ${confirm:0:1} in
  111. y|Y)
  112. ;;
  113. *)
  114. rm $whitepdf
  115. exit 1
  116. ;;
  117. esac
  118. fi
  119. fi
  120. #Execute pdfunite
  121. $($unite)
  122. if [ ! $? -eq 0 ];then
  123. printf "Warning: something was wrong\n"
  124. else
  125. printf "Creation Successful\n"
  126. if [ $verbose = true ]; then
  127. pages=$(pdfinfo $i|grep Pages:|awk '{print $2}')
  128. printf "PDFOut:\t$s (%s Pages)\n" $i $pages
  129. fi
  130. fi
  131. fi
  132. done
  133. #Remove whitepdf
  134. if [ $verbose = true ]; then
  135. printf "Deletting temporal files\n"
  136. fi
  137. rm $whitepdf