File remove-pdf-password of Package kimi-utils-ubuntu
#!/usr/bin/env bash
THIS_FILE_NAME="${0##*/}"
FULL_PATH_TO_THIS_FILE="${0}"
function Usage {
echo "Usage: $(basename $0) PDF-file.pdf" 2>&1
echo 'Removes password from PDF-file.'
exit 1
}
# Check if there is an argument
if [ $# -eq 0 ]; then
echo "No PDF-file name provided."
echo
Usage
exit 1
fi
# PDF_FILE is first argument
PDF_FILE="$1"
# Check if PDF-file exists
if [ ! -f "$PDF_FILE" ]; then
echo "No PDF-file with that path or name exist."
echo "Check your spelling."
exit 1
fi
DIRECTORY_OF_FILE=`dirname "$PDF_FILE"`
EXTENSION="${PDF_FILE##*.}"
FILENAME="${PDF_FILE%.*}"
# Every PDF file starts with %PDF. You can compare the first 4 characters of a specified file in order to make sure it's a PDF.
if [ ! $(head -c 4 "$PDF_FILE") = "%PDF" ]; then
echo "It's not a PDF-file!"
fi
# Check that it is a PDF-file
type="$(file -b "$1")"
if [ ! "${type%%,*}" == "PDF document" ]; then
echo "$PDF_FILE is not a PDF file."
exit 1
fi
SaveAs ()
{
echo "Saving file:"
echo " $PDF_FILE"
echo "without password as a new file name:"
echo " ${FILENAME}_unlocked.${EXTENSION}"
echo "in directory:"
echo " $DIRECTORY_OF_FILE"
}
NEW_FULL_FILE_NAME="$DIRECTORY_OF_FILE/${FILENAME}"_unlocked.${EXTENSION}
# Check if qpdf is installed
checkForQpdf() {
if hash qpdf 2>/dev/null; then
# qpdf is installed on the system
true
else
# install qpdf
echo "qpdf is not installed on the system."
echo "I need qpdf for this task."
#echo "I will now install package qpdf for you."
#sudo apt-get -y install qpdf
exit
fi
}
checkForQpdf
echo "Enter PDF password to open locked PDF-file"
echo "(you wont see what you type)"
IFS= read -s -p "Enter PDF password:" pwd
echo
echo
qpdf --decrypt --password="$pwd" "$PDF_FILE" "$NEW_FULL_FILE_NAME"
SaveAs
sleep 5
xdg-open "$NEW_FULL_FILE_NAME"
sleep 5
echo "Done."
exit 0