-
Notifications
You must be signed in to change notification settings - Fork 1
/
sign-pdf
executable file
·126 lines (108 loc) · 3.63 KB
/
sign-pdf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/bin/bash
#set -o xtrace
#############################################################
# SignPDF: A simple way to add a signature image to a pdf at given page under linux.
# The inserted signature has the same aspect ratio than the original.
#############################################################
# Description:
#
# Written by : Emmanuel Branlard
# Date : April 2012
# Dependencies : pdftk
# License : Feel free to modify and adapt it
#
# Usage:
# First argument: pdf file
# Second argument: page number where the signature should be put
#
#
# Example: Add a signature to file "my file.pdf" on page 5:
# ./SignPDF "my file.pdf" 5 "signature.png" 100 300 400 500
#
# Note : You need to edit the SIGNATURE variable below for default signature
# This script generates a folder where everything takes place
# The folder is deleted afterwards. See line below:
TMP=tmp-folder-signpdf
if [ $# -ne 7 ]
then
echo "
Usage:
SignPdf <PdfFile> <PageNumber> SignatureFile x1 y2 x2 y2
Example:
SignPdf \"my file.pdf\" 5 signature.png 100 300 400 500
Look at the source for more informations.
"
exit 0
fi
SIGNATURE=$3
x1=$4
y1=$5
x2=$6
y2=$7
echo " -------------- Called with: --------------
File : $1
Page : $2
Signature: $SIGNATURE "
# set -vx
DIR=`pwd`;
mkdir $TMP
cd $TMP
echo "-------------- Bursting pdf -----------------"
pdftk "../$1" burst output page_%d.pdf
# convert -page A4 -units PixelsPerInch -density 100 page_$2.pdf page_$2.png
convert -density 100 page_$2.pdf page_$2.png
#echo "-------------- !!! Click on two points defining the signature box -----------------"
#echo "-------------- !!! Don't resize the window - you can move it though -----------------"
#echo "-------------- Press q to exit -----------------"
##xv -nolimits -D 1 page_$2.png 2> log.log
## cat log.log |grep --colours=none ButtonPress |grep --colours=none mainW
## cat log.log |grep --color=none ButtonPress |grep --color=none mainW |tail -n 2 >buff.log
#cat log.log |grep --color=none ButtonPress |grep --color=none mainW |tail -n 2|cut -c 29- > buff.log
#x1=`awk -F, 'NR==1 {print $1}' buff.log|tr -d -c '[0-9]'`
#y1=`awk -F, 'NR==1 {print $2}' buff.log|tr -d -c '[0-9]'`
#x2=`awk -F, 'NR==2 {print $1}' buff.log|tr -d -c '[0-9]'`
#y2=`awk -F, 'NR==2 {print $2}' buff.log|tr -d -c '[0-9]'`
# dimensions of the rectangle specified by the user
dx=`expr $x2 - $x1`
dy0=`expr $y1 - $y2`
# a flag to see if the user has gone up and down instead of down and up
reverse_flag=0
# checking user input
if [[ $dy0 -lt 0 ]]
then
reverse_flag=1
dy0=`expr $y2 - $y1`
fi
if [[ $dx -lt 0 ]]
then
buffer=$x2
x2=$x1
x1=$buffer
dx=`expr $x2 - $x1`
fi
cd $DIR
# Signature image dimensions
dxs=`identify -format "%[fx:w]" "$SIGNATURE"|tr -d -c '[0-9]'`
dys=`identify -format "%[fx:h]" "$SIGNATURE"|tr -d -c '[0-9]'`
cd $TMP
# Keeping aspect ratio of the signature
dy=`expr $dx \* $dys / $dxs`
if [[ $reverse_flag -eq 0 ]]
then
# Here it's tricky either you use the user input $dy0 or $dyA
y1=`expr $y1 - $dy0`
fi
# The following because I chose to double the density
# dx=`expr $dx \* 2`
# dy=`expr $dy \* 2`
# y1=`expr $y1 \* 2`
# x1=`expr $x1 \* 2`
# composite -geometry 100x100+$y1+$x1 $SIGNATURE page_$2.png composite.png
echo "-------------- Inserting Signature at lower left corner: +$x1+$y1"
cd $DIR
composite -geometry "$dx"x"$dy"+$x1+$y1 "$SIGNATURE" $TMP/page_$2.png $TMP/page_$2b.png
convert -density 100 $TMP/page_$2b.png $TMP/page_$2.pdf
pdftk $TMP/page_*.pdf cat output "${1%.pdf}_signed.pdf"
echo "-------------- File ${1%.pdf}_signed.pdf created ------------- "
echo "-------------- Done -------------"
rm -r $TMP