I have a password protected PDF file. I know the password but in order to share the file, I have to remove the password from the PDF and share an unprotected copy. How can I do this in Ubuntu with or without the GUI?
10 Answers
The easiest way GUI (recommended for novice)
Open the protected file and use ctrl+p or use print option to print the file, now save the file as pdf.
Using Command line
Option 1 - qpdf
qpdf which is automatically installed (at least on 16.04 which I am using)
To use qpdf for generating unsecured pdf run following command.
qpdf --password=<your-password> --decrypt /path/to/secured.pdf out.pdf
Option 2 - pdftk
If you have pdftk already installed you can skip step 1.
Step 0: To check if Pdftk is already installed
sudo apt list | grep pdftk
If output contains '[installed]' tag with pdftk then you can skip step1 i.e if the output is like this
pdftk/xenial 2.02-4 amd64 [installed]
Step 1: Install pdftk
sudo apt-get install pdftk
Step 2: Run following command
pdftk /path/to/input.pdf input_pw <yourpassword> output out.pdf
For detailed information take a look at this HTG tutorial
- 107
- 7,022
- 7
- 27
- 43
I tried this in ubuntu mate 19.04:
sudo apt-get install qpdf
qpdf --password=YOURPASSWORD-HERE --decrypt input.pdf output.pdf
EDITED: You can also open the file in chrome to print and then save as PDF.
- 1,061
This is an old question, but seems to be a reference on the matter and, surprisingly, none of the answers tells us how to avoid passing the password on the command line (which may be a source of leakage). Of course, since this is about removing the password protection from the file, maybe you don't care. But maybe you received a pdf from a company which used some data of yours to encrypt the file, and you'd like to avoid leaking it.
With pdftk we can use:
pdftk protected.pdf input_pw output out.pdf do_ask
The password is then queried in the terminal and you can type it.
With qpdf it is a little less direct. qpdf can read a password from stdin passing - to the --password-file= option:
qpdf --password-file=- --decrypt protected.pdf out.pdf
Once you enter that, qpdf will be waiting for input from stdin. You can then type <yourpassword>, RET and Ctrl+d (Ctrl+d sends EOF in Linux. In Windows, I think it would be Ctrl+z, but I'm not sure).
If you have an older version of qpdf (prior to v10.2.0), --password-file= didn't work this way, but one could still read a whole argument from stdin with the @- option. That given, you can use:
qpdf @- --decrypt protected.pdf out.pdf
Then type --password=<yourpassword>, RET and Ctrl+d.
- 414
sudo apt-get install pdftk
pdftk input.pdf output output.pdf user_pw YOURPASSWORD-HERE
This takes your input.pdf, removes the passwords and exports it as output.pdf.
You may want to take a look here to explore additional mehods.
- 8,164
- 182
Use this zsh function:
pdf-unencrypt () {
: "Usage: <file>
Uses ghostscript to rewrite the file without encryption."
local in="$1"
gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile="${in:r}_unencrypted.pdf" -c .setpdfwrite -f "$in"
}
: is a no-operations function. $in:r gets the variable without its extension. You obviously need ghostscript installed.
- 325
If you already have a password, you can use the following to remove the password
gs -dNOPAUSE -dBATCH -q -sDEVICE=pdfwrite -sPDFPassword=password -sOutputFile=output2.pdf -f input.pdf
- 493
if you don't have the password you can still unprotect the pdf document thanks to ghostscript :
gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=unencrypted.pdf -c .setpdfwrite -f my-protected-pdf-file.pdf
if you need to install GhostScript : How to install newer version of ghostscript on server than provided from ubuntu?
- 63
Rising the topic from the dead a little bit here (but I am a new Linux user, so...);
- Anyhow, I also used the file for which I had known the password. But I used "Master PDF Editor 5" (unregistered, free version) to remove the password (File - Properties - Security - No Encription).
- However, since "Master PDF Editor 5" leaves the watermark (which I personally, do not mind), I re-opened (the now unlocked file) in Libre Office Draw and removed the watermark.
- I exported the file in PDF, which additionally resulted in tremendous compression without any losses. It was a very simple file; one sheet only, with text in the table, but the above process reduced the size from 70-ish KB to 22-ish KB.
- 70,557
- 19
The simplest solution to this problem is:
- From your favorite file explorer: right click, open with...
- Open the PDF file in LibreOffice Draw
- Enter the password
- Export as > Export as pdf
This is super newbie friendly.
I think I might as well chime in with a little script I built around pdftk and gum.
#!/bin/bash
if [[ -z "$1" || ! -f "$1" ]]; then
echo "Select a file:"
file="$(gum file)"
echo -e "${file}\n"
else
file="$1"
fi
pdftk "${file}" input_pw output "${file//.pdf/_decrypted.pdf}" do_ask
You can provide a file as the first parameter and the decrypted file will be saved with _decrypted.pdf postfix. If you don't provide a file name as the first parameter it will bring up gum's file chooser.
- 29,597