I'm trying to find the best way to detect that a given PDF is encrypted with PHP. I don't need to decrypt it or edit it or anything like that. The idea is to simply provide an error message to a user if they upload an encrypted PDF.
Most of the PDF parsing libraries for PHP that are out there seem to require the entire PDF be read into memory in order to do the parse. For my purpose, reading the entire PDF into memory is unacceptable as the PDF's I am dealing with may be tens if not hundreds of megabytes large.
Shelling out to pdfinfo
is not a great option (as I don't like firing up a new process to do this) but that is the solution if there is no other options. I don't know enough about the binary structure of a PDF to even write enough of a parser to detect this so pdfinfo
may be the only choice.
TL;DR is there an easy way that is pure PHP (no C extensions) to detect if a PDF is encrypted (aka password protected) that does not read the entire thing into memory?