{"ref":null,"ref_type":"repository","master_branch":"master","description":"#!/bin/bash # #  bundsteg v1.0 - print pdfs with gutter/gap/inner margin #  (c) copyright by Elmar Stellnberger 05-08-2009  v1.0 # #   pize() { while read tok; do echo \"P$tok\"; done; } calc() {    while [ -n \"$1\" ]; do     var=\"${1%%=*}\"; term=\"${1#*=}\"     term=\"$(sed 's#[A-Za-z][A-Za-z0-9]*#$&#g' <<<\"$term\")\"     eval \"term=$term\"     export $var=$(bc <<<\"scale=8;$term\");     shift   done }  interleave() {   both=$((pages/2)); rem=$((pages-2*both))   for((i=1; i<=both; i++)); do     echo \"O$i\"; echo \"E$i\"; done   [[ rem -gt 0 ]] && echo \"O$((both+1))\" }  dopdfcrop() { {   pdfcrop \"$@\" 2>&1 1>&3 | sed -n \"s#.*Error: Cannot move \\`\\([^']*\\)['] to \\`\\([^']*\\)['].*#move\\n\\1\\n\\2#p\" | {     read command; if [ \"$command\" = \"move\" ]; then       read movefrom; read moveto; mv \"$movefrom\" \"$moveto\";       echo mv \"$movefrom\" \"$moveto\";     else echo \"$command\" >&2; fi   } } 3>&1; }  help() {   cat <<EOQ $(basename $0) --horiz bs[,bs2] input.pdf [output.pdf] $(basename $0) --vert bs[,bs2] input.pdf [output.pdf]   bs: size of gutter in mm; use f.i. 10 or 15   you may specify a different gutter size for even pages (bs2) separated by a colon from the one for odd pages   default output file: input.bundsteg.pdf    EOQ }  checkfound() {   [ -f \"$1\" ] && return 0   echo \"executable required: $1 ${2+-package} $2\"; echo   exit 1 }   if [ $# -ne 3 -a $# -ne 4 ]; then help; else   input=\"$3\"   if [[ $# -eq 4 ]]; then output=\"$4\"; else output=\"${input%.pdf}.bundsteg.pdf\"; fi    pdftools=\"wine /usr/local/advanced_pdf_tools_cmd_v3.0/pdftools.exe\"   checkfound ${pdftools#* } http://www.verypdf.com/pdfinfoeditor/   checkfound $(which pdfcrop) texlive   checkfound $(which pdftools) texlive-context   checkfound $(which pdftk) pdftk      [[ $2 -gt 0 ]] || { echo \"no valid value for bs (use 10 or 15 at 2nd param)\";echo; exit 2; }   bs1=${2%%,*}; bs2=${2#*,}; calc 'bs1=bs1*72/25.4' 'bs2=bs2*72/25.4'   #calc 'li=0.333*72' 're=li' 'ob=0.5*72' 'un=ob'   calc 'li=4.3*72/25.4' 're=li' 'ob=li' 'un=ob'   #calc 'li=0' 're=li' 'ob=0' 'un=ob'    if [ \"$1\" == \"--horiz\" ]; then     calc 'li1=li' 'ob1=ob+bs1'   're2=re' 'un2=un+bs2'   elif [ \"$1\" == \"--vert\" ]; then     calc 'li1=li+bs1' 'ob1=ob'   're2=re+bs2' 'un2=un'   else     echo \"1st param: --horiz / --vert\"; exit 3;   fi      #pages=$(pdftools --countpages \"$input\")   pages=$(pdftools --info \"$input\" | sed -n 's#.*<pages>\\([0-9]*\\)</pages>*#\\1#p')   pdftk P=\"$input\" cat $(seq 1 2 $pages | pize;) output /tmp/odd.pdf   pdftk P=\"$input\" cat $(seq 2 2 $pages | pize;) output /tmp/even.pdf      echo $'\\n' pdfcrop --margins \\\"$li1 $ob1 $re $un\\\" /tmp/odd.pdf /tmp/odd-bs.pdf   dopdfcrop --margins \"$li1 $ob1 $re $un\" /tmp/odd.pdf /tmp/odd-bs.pdf      echo $'\\n' pdfcrop --margins \\\"$li $ob $re2 $un2\\\" /tmp/even.pdf /tmp/even-bs.pdf   dopdfcrop --margins \"$li $ob $re2 $un2\"  /tmp/even.pdf /tmp/even-bs.pdf      echo $'\\n' pdftk E=/tmp/even-bs.pdf O=/tmp/odd-bs.pdf cat $(interleave) output \"$output\"   pdftk E=/tmp/even-bs.pdf O=/tmp/odd-bs.pdf cat $(interleave) output \"$output\"    #rm -f /tmp/even.pdf /tmp/odd.pdf /tmp/even-bs.pdf /tmp/odd-bs.pdf   echo $'\\n'  fi","pusher_type":"user"}