qpdf-decode/qpdf-decode

91 lines
2.5 KiB
Bash
Executable File

#!/bin/sh
# qpdf-decode: a script for calling qpdf with a password to decrypt a pdf.
# If the operation succeeds, the output will be a default new file adding a
# suffix "_decrypted" to the source file name. Be careful: if the output file
# already exists, it will be overwritten without warning!
#
# Parameters:
# 1st. PASSWORD
# 2nd. file to be decrypted
# 3rd. (optional) file to save (default: the same name concatenating _decrypted)
#
# Copyright © 2023 Daltux <https://daltux.net/>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
app_title=qpdf-decode
app_version=0.2.0
about() {
echo "$app_title: decrypts a pdf"
echo "Version $app_version"
echo
echo "Copyright (C) 2023 Daltux <https://daltux.net>"
echo "This program comes with ABSOLUTELY NO WARRANTY;"
echo "This is Free Software, and you are welcome to redistribute it under certain conditions. For details, see the GNU General Public License, version 3 or later."
echo
echo "Usage:"
echo " $0 <password> <file to decode> [optional: file to save (default *_decrypted)]"
echo
echo "Be careful: if the output file already exists, it will be overwritten without warning!"
echo
echo "Exit codes meaning:"
echo " 0: no errors or warnings"
echo " 1: unable to invoke qpdf"
echo " 2: qpdf error"
echo " 3: qpdf warning"
echo " 4: missing password for decryption"
echo " 5: missing reference to file"
}
# strFind <string> <substring to find>
strFind() {
case $1 in
(*"$2"*) return 0;;
esac
return 1
}
params=${1+"$@"}
if [ -z "$1" ] || [ "$1" = '-h' ] || strFind "$params" "--help" ; then
about
exit
fi
pass=$1
infile=$2
if [ -z "$pass" ] ; then
echo ERROR: missing a password for decrypting the pdf file.
exit 4
fi
if [ -z "$infile" ] ; then
echo ERROR: missing reference to a pdf file for processing.
exit 5
fi
outfile=${infile}_decrypted
outfile=${3:-$outfile}
if command -v qpdf > /dev/null 2>&1 ; then
qpdf --verbose --password="$pass" --decrypt "$infile" "$outfile"
else
echo qpdf not found!
exit 1
fi