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