2023-06-16 08:47:24 +00:00
|
|
|
#!/bin/sh -e
|
2023-08-07 15:45:59 +00:00
|
|
|
# Overlay a PDF with another or a text
|
|
|
|
# Trims everything beyond the given page number
|
2022-04-06 14:54:41 +00:00
|
|
|
test "$1" = "-q" && quiet=$1 && shift
|
2023-05-09 12:02:32 +00:00
|
|
|
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
|
2021-07-10 19:47:34 +00:00
|
|
|
|
2021-04-21 17:49:30 +00:00
|
|
|
tmp_base=/tmp/sign
|
|
|
|
mkdir -p $tmp_base
|
2023-07-27 15:16:41 +00:00
|
|
|
|
|
|
|
texttopdf() {
|
|
|
|
out="$tmp_base/stamp.pdf"
|
|
|
|
echo "$1" | enscript -B -f Arial12 -o- | ps2pdf - >"$out"
|
|
|
|
echo "$out"
|
|
|
|
}
|
|
|
|
signature=$(test -f "$5" && echo "$5" || pass info/signature$(test -n "$5" && echo "-$5") || texttopdf "$5")
|
|
|
|
|
|
|
|
# TODO make filenames unique and don't rerun unneccessarily
|
2023-08-07 15:45:59 +00:00
|
|
|
tmp_signed=$tmp_base/$1_${page:-last}-signature.pdf
|
2021-04-21 17:49:30 +00:00
|
|
|
tmp_reversed=$tmp_base/$1_reverse.pdf
|
|
|
|
sig=$tmp_base/signature_offset.pdf
|
2023-04-06 14:21:36 +00:00
|
|
|
result="${1%.pdf}_$(basename "${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
|
2023-06-16 08:47:24 +00:00
|
|
|
echo "Using offset ${h}x and ${v}y"
|
2023-03-31 17:09:55 +00:00
|
|
|
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}"
|
2023-09-27 13:38:23 +00:00
|
|
|
rm -f "$result"
|
2023-05-09 12:02:32 +00:00
|
|
|
pdfjam $quiet "$1" ${page:-last} "$sig" --outfile "$tmp_signed" --delta "0 -842pt" --nup "1x2" --fitpaper true
|
2023-04-21 07:27:19 +00:00
|
|
|
# TODO consider pdftk stamp
|
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
|
2023-05-09 12:02:32 +00:00
|
|
|
if test "$page"
|
2023-08-07 15:45:59 +00:00
|
|
|
then test "$page" -gt 1 && pdfjam $quiet "$1" 1-$(expr $page - 1) "$tmp_signed" 1 "$1" $(expr $page + 1)-last --outfile "$result"
|
2023-05-09 12:02:32 +00:00
|
|
|
elif test "$(pdftk "$1" dump_data | grep Pages | cut -d' ' -f2)" -gt 1
|
|
|
|
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
|
|
|
fi
|
2023-08-07 15:45:59 +00:00
|
|
|
test -f "$result" || cp -v "$tmp_signed" "$result"
|