forked from matteosecli/pdf2archive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdf2mark
executable file
·149 lines (123 loc) · 2.63 KB
/
pdf2mark
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/bin/bash
source $(dirname $0)/pdf2common
#=====# CHECKS #=====#
if [ "$(which gs)" == "" ]; then
echo "ERROR: Ghostscript is not installed or it's not in the path"
exit
fi
if [ "$1" == "" ]; then
exit
fi
#=====# INPUT PARSER #=====#
while [ "$1" != "" ]; do
PARAM=`echo $1 | awk -F= '{print $1}'`
VALUE=`echo $1 | awk -F= '{print $2}'`
case $PARAM in
-h | --help)
echo -e "\\nVisit project repo: https://github.com/Kuszki/pdf2archive\\n"
exit
;;
-v | --version)
echo $VERSION
exit
;;
--debug)
DEBUG=true
MSGOPTS=""
;;
--good)
DONE_FILE=$VALUE
;;
--bad)
BAD_FILE=$VALUE
;;
--pass)
PASS_FILE=$VALUE
;;
--validate)
VALIDATE=$VALUE
;;
--mark)
WATERMARK=$VALUE
;;
*.pdf)
if [ "$INPUT" == "" ]; then
INPUT=$PARAM
elif [ "$OUTPUT" == "" ]; then
OUTPUT=$PARAM
else
echo "ERROR: too many PDF files as input!"
exit 1
fi
;;
*)
echo "ERROR: unknown parameter \"$PARAM\""
exit 1
;;
esac
shift
done
OUTDIR=$(dirname "${OUTPUT}")
INFILE=$(basename "${INPUT}")
if [ "$WATERMARK" == "" ]; then
exit 1
fi
if [ ! -f "$INPUT" ]; then
log_pass
exit 1
fi
if [ ! -d "$OUTDIR" ]; then
mkdir -p "$OUTDIR" && DIRMADE=true
if [ $? != 0 ]; then
log_bad
exit 1
fi
fi
PSFILE=$(mktemp -p /dev/shm/ XXXXXXXXXX.ps)
TMPFILE=$(mktemp -p /dev/shm/ XXXXXXXXXX.pdf)
if $DEBUG; then
echo " DEBUG: running PDF2MARK, version $VERSION"
echo " DEBUG: using Ghostscript binary at $(which gs), version $(gs --version)"
echo " DEBUG: the input file is '$INPUT'"
echo " DEBUG: the output file is '$OUTPUT'"
echo " DEBUG: the intermediate processing file is $TMPFILE"
echo " DEBUG: PDF watermark '$WATERMARK'"
fi
echo "%% Watermark ps file
/watermarkText { ($WATERMARK) } def
/watermarkFont { /TimesNewRomanBold 20 selectfont } def
/watermarkColor { 1 0 0 setrgbcolor } def
/watermarkAlpha { 0.5 .setfillconstantalpha } def
/watermarkAngle { 0 } def
/pageWidth { currentpagedevice /PageSize get 0 get } def
/pageHeight { currentpagedevice /PageSize get 1 get } def
<<
/EndPage
{
2 eq { pop false }
{
gsave
watermarkFont
watermarkColor
watermarkAlpha
pageWidth pageHeight translate
0 0 moveto
watermarkText false charpath flattenpath pathbbox
4 2 roll pop pop
-5 -5 moveto
watermarkAngle rotate
-1 mul exch -1 mul exch
rmoveto
watermarkText show
grestore
true
} ifelse
} bind
>> setpagedevice" >> "$PSFILE"
run gs $MSGOPTS \
-dBATCH -dNOPAUSE -dALLOWPSTRANSPARENCY -sDEVICE=pdfwrite \
-sOutputFile="$TMPFILE" "$PSFILE" "$INPUT" && \
cp "$TMPFILE" "$OUTPUT"
do_finalize $? "$TMPFILE"
rm "$PSFILE"
rm "$TMPFILE"