问题
is it possible to check if pdf is password protected using ghostscript? what would be the command? I know you can strip pdf password using ghostscript, but all I want to do is just checking if PDF is password protected or security enabled.
回答1:
checkuserpasswdPDF.sh
:
#!/bin/sh
GS=~/gs/bin/gs
output=`$GS -dBATCH -sNODISPLAY "$1" 2>&1`
gsexit=$?
if [ "$gsexit" == "0" ]; then
echo "Not user-password protected"
exit 0;
else
found=`echo "$output" |grep -o "This file requires a password"`
if [ -z "$found" ]; then
echo "Failed to invoke gs"
exit $gsexit
else
echo "Protected"
exit 0;
fi
fi
Checks for user-password protected PDFs: checkuserpasswdPDF.sh test.pdf
.
GS disregards owner-passwords (see this).
回答2:
With pdftk
it is possible to detect a user password or owner password by just trying to do a dump_data
operation.
protected=0
pdftk "input.pdf" dump_data output /dev/null dont_ask || protected=1
The problem here is that you don't know what the password prevents: reading, extracting data, modifying...?
回答3:
Using a bat file, you can do a little workaround by searching for "Encrypt" in the pdfs. Its quiet fast to search through many files.
Findstr /M /I "Encrypt" *.pdf
This will return all filenames that are secured (since "Encrypt" will be written in the file as dos reads it)
Maybe this is something someone can use.
I use:for /f %%a in ('Findstr /M /I "Encrypt" *.pdf') do move %%a c:\tempfiles\
to move all secured pdf's to c:\tempfiles. From there I use ghostscript to remove the security, and move it back to original folder.
回答4:
You can test using pdfinfo
pdfinfo $filename &>/dev/null;
if [[ $? -eq 1 ]] ; then
echo "File can not be opened for reading"
fi
来源:https://stackoverflow.com/questions/4039659/is-it-possible-to-check-if-pdf-is-password-protected-using-ghostscript