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?
8 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
If you have pdftk already installed you can skip step1
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
If you don't want to install pdftk there is another utility 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
For detailed information take a look at this HTG tutorial
- 5,602
- 6
- 24
- 43
-
4Unfortunately, `pdftk` failed to open my PDF: `Error: Invalid PDF: unknown.encryption.type.r`. (Meanwhile, Evince is able to open it perfectly fine.) qpdf did the trick for me. – balu Aug 19 '21 at 22:28
-
2pdftk issue: https://gitlab.com/pdftk-java/pdftk/-/issues/87 – user1338062 Sep 04 '21 at 09:37
-
In MacOS Monterey PDFTK has the same issues. But `qpdf` solves the issue. `brew install qpdf`. – vijay v Jan 24 '22 at 08:06
-
The GUI qpdfview will not print to file (pdf). pdftk must be used. – Geoffrey Wheeler Jul 03 '22 at 15: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 first and then save as PDF.
- 781
- 5
- 5
-
7Awesome! pdftk didn't work for me due to encryption (`InvalidPdfException: unknown.encrpytion.type.r`), but qpdf did! – Christian Benke Apr 27 '20 at 19:59
-
I used this script to unencrypt all the pdfs in the current directory `mkdir dec; ls *.pdf | xargs -i qpdf --password= --decrypt {} dec/{}` – AmanicA Dec 12 '21 at 18:18
-
2A safer version to do all files in a directory.. `for i in *.pdf; do qpdf --password="$mypass" --decrypt "$i" "${i%.pdf}.decrypted.pdf"; done` – shalomb Jun 22 '22 at 17:59
-
Actually `qpdf --decrypt in.pdf out.pdf` (no password) worked fine for me – Déjà vu Aug 22 '22 at 08:13
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.
- 7,823
- 3
- 38
- 56
- 172
- 8
-
-
3Didn't work for me either. This works by putting the user_pw param just after input.pdf (it seems params are positional). So "pdftk input.pdf user_pw YOURPASSWORD-HERE output output.pdf " should work (I've used input_pw instead of user_pw). – Bozzy Feb 12 '21 at 11:57
-
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.
- 311
- 2
- 13
-
1In more recent versions of ghostscript, you'll need to remove the `-c .setpdfwrite -f` from the above command line. Otherwise, version 9.54 or later will fail with `Error: /undefined in .setpdfwrite`. (I don't know why there's a forward-slash in that.) And version 9.53.x will give you a warning message stating `The .setpdfwrite operator has been deprecated and will be removed entirely in the next release of Ghostscript` – AJM Mar 17 '22 at 13:41
-
I've seen this command many times yet when I run it I get an error `This file requires a password for access`. So at least in recent versions of `gs` it doesn't seem to be working or at least not for me. – rbaleksandar Apr 26 '22 at 19:20
-
_almost_ works, except instead of being prompted for a password when I try to save an edit, it just freezes (on `Preview.app`) – ijoseph Jan 01 '23 at 00:13
-
edit: oh wait, if I then used `Preview.app` to Export it (as PDF) again, I can now successfully save edits. Score. – ijoseph Jan 01 '23 at 00:15
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 accepts a @- option which reads an argument from standard in. That given, you can use:
qpdf @- --decrypt protected.pdf out.pdf
Once you enter that, qpdf will be waiting for input from stdin. You can then type:
--password=<yourpassword>
then RET and Ctrl+d (Ctrl+d sends EOF in Linux. In Windows, I think it would be Ctrl+z, but I'm not sure).
- 284
- 3
- 8
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
- 6
-
1Are you sure you do not need a password? If this works, password protection of PDF is equivalent to no protection. – Marc Vanhoomissen Jun 27 '22 at 12:51
-
-
I tried and got a message `This file requires a password for access. Error: /invalidfileaccess in pdf_process_Encrypt`. – Marc Vanhoomissen Jun 29 '22 at 09:54
-
i think you are right, this removes the protection when the file is locked for edition, not for reading – baobab33 Jul 09 '22 at 12:48
-
does not work on readable pdfs with password: `Error: /undefined in .setpdfwrite` – oarfish Aug 13 '22 at 13:17
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.
- 67,112
- 21
- 233
- 243
- 21
- 1
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
- 444
- 3
- 14