-
Notifications
You must be signed in to change notification settings - Fork 2
/
pdfunlock
executable file
·137 lines (116 loc) · 3.52 KB
/
pdfunlock
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
#!/bin/bash
# PDFUnlock
# Copyright (c) 2021 Stephan Brunner <s.brunner@stephan-brunner.net>
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
VERSION=0.2
set -e
function findExecutable() {
E=`which "${1}"`
if [ ! -x "${E}" ]
then
echo $1 not found! >&2
exit 1
fi
echo -n $E
}
PSTOPDF=$(findExecutable ps2pdf)
PDFTOPS=$(findExecutable pdftops)
PDFINFO=$(findExecutable pdfinfo)
PDFUNITE=$(findExecutable pdfunite)
MKTEMP=$(findExecutable mktemp)
if [ $# -ne 2 ] && [ $# -ne 4 ]
then
echo "PDFUnlock v${VERSION}"
echo "Copyright (c) 2021 Stephan Brunner <s.brunner@stephan-brunner.net>"
echo
echo Usage: $0 [Locked PDF] [Unlocked PDF] \([PasswordType] [Password]\)
echo
echo Available password types: user, owner
echo
echo "Examples:"
echo "- Remove editing password when no view password is set:"
echo " pdfunlock protected.pdf unprotected.pdf"
echo "- Remove view password when view/user password is known:"
echo " pdfunlock protected.pdf unprotected.pdf user viewPassword"
echo "- Remove modification password when editing/owner password is known:"
echo " pdfunlock protected.pdf unprotected.pdf owner editPassword"
exit 2
fi
LOCKED_PDF=${1}
UNLOCKED_PDF=${2}
PASSWORD_TYPE=${3}
PASSWORD=${4}
PASSWORD_OPT=""
if [ "x${PASSWORD_TYPE}" = "x" ]
then
PASSWORD_OPT=""
elif [ "x${PASSWORD_TYPE}" = "xuser" ]
then
PASSWORD_OPT="-upw ${PASSWORD}"
elif [ "x${PASSWORD_TYPE}" = "xowner" ]
then
PASSWORD_OPT="-opw ${PASSWORD}"
else
echo "Invalid password type!"
exit 3
fi
if [ "x${PASSWORD_TYPE}" != "x" ] && [ "x${PASSWORD}" = "x" ]
then
echo "password type is set, but no password given"
exit 3
fi
if [ ! -r "${LOCKED_PDF}" ]
then
echo Cannot read locked pdf
exit 3
fi
rm "${UNLOCKED_PDF}" >/dev/null 2>&1 || true
touch "${UNLOCKED_PDF}" >/dev/null 2>&1 || true
if [ ! -w "${UNLOCKED_PDF}" ]
then
echo Cannot write unlocked pdf
exit 4
fi
NUMPAGES=$(pdfinfo ${PASSWORD_OPT} "${LOCKED_PDF}" | grep Pages | awk '{print $2}')
echo Detected $NUMPAGES pages...
if ! [[ "${NUMPAGES}" =~ ^[0-9]+$ ]]
then
echo Could not determine number of pages.
exit 5
fi
if [ $NUMPAGES -le 0 ]
then
echo Locked PDF is empty.
exit 6
fi
TMPDIR=$($MKTEMP -d)
CONCATLIST=""
for (( PAGE=1; PAGE<=$NUMPAGES; PAGE++ ))
do
echo Processing page $PAGE...
$PDFTOPS ${PASSWORD_OPT} -eps -f $PAGE -l $PAGE "${LOCKED_PDF}" $TMPDIR/page.$PAGE.eps
$PSTOPDF -dEPSCrop $TMPDIR/page.$PAGE.eps $TMPDIR/page.$PAGE.pdf
CONCATLIST="${CONCATLIST} $TMPDIR/page.$PAGE.pdf"
done
echo Generating final PDF...
$PDFUNITE ${CONCATLIST} "${UNLOCKED_PDF}"
echo Cleaning up...
rm -R $TMPDIR