dotfiles/.local/bin/scripts/sign

36 lines
1.2 KiB
Plaintext
Raw Normal View History

#!/bin/sh -e
2022-04-06 14:54:41 +00:00
test "$1" = "-q" && quiet=$1 && shift
2023-03-31 17:09:55 +00:00
test ! -r "$1" && echo "Usage: sign <document.pdf> [hoffset (-160) [voffset (-310) [scale [signature-image]]]]" && exit 1
2021-07-10 19:47:34 +00:00
signature=$(pass info/signature$(test -n "$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
2021-07-10 19:47:34 +00:00
result="${1%.pdf}_${5:-signed}.pdf"
2021-06-08 18:44:06 +00:00
2023-03-31 17:09:55 +00:00
h=${2:--160}
v=${3:--310}
if test $# -lt 2; then
case "$1" in
(*\ TU.pdf) v=-260;;
esac
fi
export TEXMF=""
2022-04-06 14:54:41 +00:00
pdfjam $quiet "$signature" --outfile "$sig" --papersize "{595pt, 842pt}" --noautoscale true \
2023-03-31 17:09:55 +00:00
--offset "${h}pt ${v}pt" --scale "${4:-1}"
2022-04-06 14:54:41 +00:00
pdfjam $quiet "$1" last "$sig" --outfile "$tmp_signed" --delta "0 -842pt" --nup "1x2" --fitpaper true
2021-06-08 18:44:06 +00:00
2021-11-14 13:25:34 +00:00
timg "$tmp_signed"
2021-06-08 18:44:06 +00:00
# https://apple.stackexchange.com/questions/198854/shell-command-to-count-pages-in-a-pdf-other-than-pdftk
2021-07-10 19:47:34 +00:00
if test "$(pdftk "$1" dump_data | grep Pages | cut -d' ' -f2)" -gt 1
2021-06-08 18:44:06 +00:00
then
2022-04-06 14:54:41 +00:00
pdfjam $quiet "$1" last-1 --outfile "$tmp_reversed"
pdfjam $quiet "$tmp_reversed" last-2 "$tmp_signed" --outfile "$result"
2021-06-08 18:44:06 +00:00
else cp "$tmp_signed" "$result"
fi