PDF documents are very popular around the world due to their ease of use, portability, compatibility and security. You can easily add passwords to your PDF documents to protect them, in case they contain sensitive information. Linux provides many tools to password protect PDF in Linux. In this article, we will learn how to password protect PDF in Linux.
How to Password Protect PDF in Linux
We will look at how to secure PDF documents using PDF Toolkit and QPDF packages.
1. Using PDF Toolkit
PDF Toolkit is a feature-rich module that allows you to easily create, edit and share PDF documents with others. Open terminal and run the following command to install PDF toolkit on your system.
$ sudo apt-get install pdftk
If you are asked to enter your user password, enter it to begin installation. If you see any prompts during installation, enter y to proceed. Once PDF Toolkit is installed, you can easily add password to your document, with the following command.
$ pdftk input.pdf output output.pdf owner_pw xyz user_pw abc
In the above command, we specify the input.pdf document that does not contain password, output.pdf document that is password protected. We also specify separate passwords for owner and user using owner_pw and user_pw options. You can also keep the same password for owner and user if you want.
For example, if you have pdf document at /home/ubuntu/data.pdf and want to set same password test123 to create password protected file /home/ubuntu/new-data.pdf, you can do so with the following command.
$ pdftk /home/ubuntu/data.pdf output /home/ubuntu/new-data.pdf owner_pw test123 user_pw test123
After you run the above command, new-data.pdf will be password protected.
Alternatively, if you don’t mention owner_pw option, then user password is assigned to owner password.
2. Using QPDF
If your Linux distribution does not support PDF toolkit, you can also use QPDF package to password protect your PDF documents. It also allows you to encrypt, decrypt, merge and split PDF documents. In fact, it comes pre-installed on Ubuntu 14.04+. If it is not present on your system, you can open terminal and run the following command to install it on your system.
$ sudo apt-get install qpdf
After you have installed qpdf, you can easily add password to your PDF with following command syntax.
$ qpdf --encrypt owner_password user_password 256 -- input.pdf output.pdf
In the above command, you can specify owner and user password one after the other in –encrypt option.
For example, if you have pdf document at /home/ubuntu/data.pdf and want to set same password test123 to create password protected file /home/ubuntu/new-data.pdf, you can do so with the following command.
$ qpdf --encrypt test123 test123 256 -- /home/ubuntu/data.pdf /home/ubuntu/new-data.pdf
In the above example, we have used the same password test123 as user and owner password.
Please note, both PDF toolkit and QPDF provide tons of useful features that allow you to easily create, edit, and share PDF documents, in addition to adding/removing password for PDF documents.
You can run the above commands in terminal to manually add password to PDF documents, add it to your shell script to programmatically add password, or even setup a cronjob with these commands to regularly run them.
Also read:
How to Remove PDF Password in Linux
How to Uninstall NVIDIA Drivers in Ubuntu
How to Build Deb Package in Ubuntu
How to Rotate & Resize Images in Linux Terminal
How to Create Fillable PDF Forms in Linux
Related posts:
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.