script.sh 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/bin/env sh
  2. LATEXCC=${LATEXCC:=pdflatex}
  3. print_help() {
  4. >&2 echo "Usage: $0 <command> [<args>]"
  5. >&2 echo -e "\nThis tool uses latex to cover a specific region of a PDF file on every page.\n"
  6. >&2 echo "<command> can be:"
  7. >&2 echo -e "\thelp:\tdisplay this help message"
  8. >&2 echo -e "\tlist:\tlist the different presets"
  9. >&2 echo -e "\tpreset:\tuse an existing preset\n\t\targuments: <preset> <infile> <outfile>"
  10. >&2 echo -e "\tmanual:\tset coordinates and dimension of the rectangle\n\t\targuments: <infile> <outfile> X Y L H [color=white] [unit=cm] [format=a4]"
  11. }
  12. gen_pdf() { # arguments: infile outfile X Y L H color unit format
  13. if [ $# -ne 9 ]
  14. then
  15. >&2 echo -e "Function gen_pdf: wrong number of arguments\n"
  16. exit 1
  17. fi
  18. infile="$1"
  19. outfile="$2"
  20. X="$3"
  21. Y="$4"
  22. L="$5"
  23. H="$6"
  24. color="$7"
  25. unit="$8"
  26. format="$9"
  27. tmp_dir=`mktemp -d`
  28. act_dir=$(pwd)
  29. cd "$tmp_dir"
  30. echo "\\documentclass[$format""paper]{article}" > main.tex
  31. cat <<EOB >>main.tex
  32. \usepackage{tikz}
  33. \usetikzlibrary{calc}
  34. \usepackage{pdfpages}
  35. \pagestyle{empty}
  36. \begin{document}
  37. \includepdf[pages={-},% include all pages
  38. pagecommand={% is called at the beginning of each inclusion
  39. \begin{tikzpicture}[remember picture,overlay]
  40. EOB
  41. echo "\\draw[color=$color,fill=$color] (\$(current page.north west) + ($X$unit, -$Y$unit)\$) rectangle ++ ($L$unit, -$H$unit);\\end{tikzpicture}}]{$infile}\\end{document}" >>main.tex
  42. $LATEXCC -interaction=nonstopmode main.tex || { >&2 echo -e "\n\n\nLaTeX error during compilation"; rm -r "$tmp_dir"; exit 1; }
  43. $LATEXCC -interaction=nonstopmode main.tex
  44. cp -i main.pdf "$act_dir/$outfile"
  45. rm -r "$tmp_dir"
  46. }
  47. case "$1" in
  48. "list")
  49. echo -n "Available presets: "
  50. preset_list=$(sed '0,/^__DATA__$/d' "$0" | cut -d' ' -f1)
  51. echo $preset_list
  52. ;;
  53. "help")
  54. print_help
  55. ;;
  56. "")
  57. print_help
  58. ;;
  59. "preset")
  60. if [ $# -ne 4 ]
  61. then
  62. >&2 echo -e "preset: wrong number of arguments\n"
  63. print_help
  64. else
  65. read_presets="$(sed '0,/^__DATA__$/d' "$0")"
  66. i=0
  67. while read -r line
  68. do
  69. presets[$i]="$line"
  70. i=`echo $i + 1 | bc`
  71. done <<< "$read_presets"
  72. for elts in "${presets[@]}"
  73. do
  74. x=($elts)
  75. [[ "$2" == ${x[0]} ]] && gen_pdf "$3" "$4" "${x[1]}" "${x[2]}" "${x[3]}" "${x[4]}" "${x[5]}" "${x[6]}" "${x[7]}" && exit 0
  76. done
  77. >&2 echo -e "preset: preset \"$2\" not found\n"
  78. print_help
  79. fi
  80. ;;
  81. "manual")
  82. if [ $# -lt 7 ]
  83. then
  84. >&2 echo -e "manual: wrong number of arguments\n"
  85. print_help
  86. else
  87. color=${8:-white}
  88. unit=${9:-cm}
  89. format=${10:-a4}
  90. gen_pdf "$2" "$3" "$4" "$5" "$6" "$7" "$color" "$unit" "$format" && exit 0
  91. fi
  92. esac
  93. exit
  94. # data format: X Y L H color unit format
  95. __DATA__