41 lines
1.4 KiB
Bash
Executable File
41 lines
1.4 KiB
Bash
Executable File
#!/bin/sh -ex
|
|
test "$1" = "-q" && quiet=$1 && shift
|
|
case "$1" in ([0-9]) page=$1; shift;; esac
|
|
test ! -r "$1" && echo "Usage: sign [page] <document.pdf> [hoffset (-160) [voffset (-310) [scale [signature-image]]]]" && exit 1
|
|
|
|
signature=$(pass info/signature$(test -n "$5" && echo "-$5") || echo "$5")
|
|
|
|
# TODO make filenames unique and don't rerun unneccessarily
|
|
tmp_base=/tmp/sign
|
|
mkdir -p $tmp_base
|
|
tmp_signed=$tmp_base/$1_last-signature.pdf
|
|
tmp_reversed=$tmp_base/$1_reverse.pdf
|
|
sig=$tmp_base/signature_offset.pdf
|
|
result="${1%.pdf}_$(basename "${5:-signed}").pdf"
|
|
|
|
h=${2:--160}
|
|
v=${3:--310}
|
|
if test $# -lt 2; then
|
|
case "$1" in
|
|
(*\ TU.pdf) v=-260;;
|
|
esac
|
|
fi
|
|
export TEXMF=""
|
|
pdfjam $quiet "$signature" --outfile "$sig" --papersize "{595pt, 842pt}" --noautoscale true \
|
|
--offset "${h}pt ${v}pt" --scale "${4:-1}"
|
|
pdfjam $quiet "$1" ${page:-last} "$sig" --outfile "$tmp_signed" --delta "0 -842pt" --nup "1x2" --fitpaper true
|
|
# TODO consider pdftk stamp
|
|
|
|
timg "$tmp_signed"
|
|
|
|
# https://apple.stackexchange.com/questions/198854/shell-command-to-count-pages-in-a-pdf-other-than-pdftk
|
|
if test "$page"
|
|
then pdfjam $quiet "$1" 1-$(expr $page - 1) "$tmp_signed" 1 "$1" $(expr $page + 1)-last --outfile "$result"
|
|
elif test "$(pdftk "$1" dump_data | grep Pages | cut -d' ' -f2)" -gt 1
|
|
then
|
|
pdfjam $quiet "$1" last-1 --outfile "$tmp_reversed"
|
|
pdfjam $quiet "$tmp_reversed" last-2 "$tmp_signed" --outfile "$result"
|
|
else cp "$tmp_signed" "$result"
|
|
fi
|
|
|