Category Archives: UNIX

Decrypt PDF files through the command line interface

Have a bunch of password protected PDF files and you’re tired to fill in the credentials each time you want to read any of them?

I recently had a similar issue when one of my clients asked me to add a new page to their website with the brochures of all their products that were, password protected, on their provider site. Although still required to have some kind of protection on them, I thought It would be better to decrypt them and add a single password to the container web page instead.

Keep reading and I’ll show you, step by step, how to decrypt password protected PDF files through the use of the command line interface as well as a few other tricks you can use in the future to solve similar problems.

Step 1: Find the right tool

The first step is finding the right app for the task at hand. We could certainly search for it using our web browser, but I want to point you a few other options available on Linux systems:

Using the man command

The man command is an interface to the online software documentation or manual pages (man-pages) of Linux systems. It’s an indispensable tool for beginners and experts, surely one of my most used shell commands…

Similarly to what we would do with our web browser, with man we can also search for keywords and we’ll also get a list of paged results.

In order to find all available programs with PDF capabilities we can change man mode of operation so it’ll search the short manual page descriptions for our keywords and display any matches. This is done through the use of the -k or --apropos option.

man -k pdf
man --apropos pdf

man-k_pdf_results

The apropos long option takes its name from the English word with the same spelling (and the same pronunciation), which means relevant. It’s also the name of another Linux command that we can use for searching commands without knowing their exact names.

Using the apropos command

apropos searches the manual page names and descriptions for the specified keywords and also prints any matches. So if we do:

apropos pdf

we’ll find the exact same set of results that before

apropos_pdf_results

This is the command that I normally use because, in my opinion, it’s the most semantic, but we have even another option to retrieve the same results.

Using the whatis command

whatis also searches the manual page names and displays the manual page descriptions of any keywords matched.

whatis -r pdf

will also print the same results. Try it by yourself!

The last result in our list seems promising:

qpdf (1)             - PDF transformation software

If we retrieve its manual page by doing:

man qpdf

we can see that:

It is capable of performing a variety of transformations such as linearization (also known as web optimization or fast web viewing), encryption, and decryption of PDF files.

Bingo!. Now we can continue with the second step, installing qpdf…

Step 2: Install QPDF

QPDF is a lightweight command-line program that does structural, content-preserving transformations on PDF files. As written in the QPDF Manual: It could have been called something like pdf-to-pdf.

QPDF’s website is located at http://qpdf.sourceforge.net/ and its source code is hosted on GitHub at https://github.com/qpdf/qpdf.

Installing QPDF on any Debian GNU/Linux based system is as simple as:

sudo apt-get install qpdf

Step 3: Decrypt the files

Once we have installed QPDF on our system, the last step is to decrypt our passwod protected PDF files.

If we run the qpdf --help command, we can see two basics options that are just what we’ve been looking for:

--password=password
Specifies a password for accessing encrypted files.
--decrypt
Removes any encryption on the file. A password must be supplied if the file is password protected.

So, If we want to decrypt a single password protected PDF file we would do:

qpdf --password=your-password-here --decrypt input-file output-file

But, what about a folder of PDF files?… That was our original task, remember?.

find . -iname "*.pdf" -exec qpdf --password=your-user-password --decrypt {} {} \;