Can I use ImageMagick to remove the password from a PDF?

5

1

If I have a PDF and I have its password, I can merely send it to a PDF printer to get a new PDF with the same contents and no password. The process is not exactly painless though (Ubuntu's inbuilt PDF printer, for example, doesn't seem too interested in honoring my page orientation settings; PDFCreator for Windows merely looked at me funny and printed an error message, etc.)

I'd like to make this process a little less painful and I imagine ImageMagick could come to my help. However...

convert foo.pdf -authenticate baz bar.pdf

...doesn't give me joy.

   **** This file requires a password for access.
   **** The file was produced by: 
   **** >>>> �Sh�Ў,bd�  a���߰�
                                �u�|!
                                      ���� <<<<
Error: /invalidfileaccess in pdf_process_Encrypt

Now... I guess that if there's pdf_process_Encrypt, chances are there's also support for a pdf_process_Decrypt operation, right?

How can I do this then?

badp

Posted 2012-05-06T17:36:53.197

Reputation: 3 457

1

Wouldn't this be a lot easier?

– Flimzy – 2012-05-06T17:41:54.617

@Flimzy None of those options appeal to me, to be honest... They're all various shades of unnecessarily shady. – badp – 2012-05-06T17:46:37.817

1Heh... And password-circumvention isn't already shady? The phrase "honest thief" comes to mind :) – Flimzy – 2012-05-06T17:47:28.183

@Flimzy uh? I have the password already. I didn't need to crack it or anything. There's absolutely no "thievery" involved here. – badp – 2012-05-06T17:48:00.187

I'm not really accusing you of being a thief. I just think it's funny to be picky about how to remove a password (whether for honest purposes or not). Anyway, I don't have enough knowledge to answer your question directly. Good luck. – Flimzy – 2012-05-06T17:49:47.570

Answers

4

pdftk may help:

pdftk secured.pdf input_pw foopass output unsecured.pdf

On Windows, A-PDF Restrictions Remover is also good, although payware.

user1686

Posted 2012-05-06T17:36:53.197

Reputation: 283 655

@badp This answer really should be a footnote to the other answers, but the other answers need to note that they are rasterizing the entire PDF, not just stripping the authentication. – Caleb – 2016-11-10T11:29:31.230

Thank you for your answer, but it probably belongs on this other question rather than this one. Although I have the growing suspicion that the command in my question would work fine, it's just the encryption scheme that most PDF libraries fail to actually support. Poke me if you do move your answer over there; I'll bounty you 100 for the trouble :)

– badp – 2012-05-06T18:25:04.087

3

This can be done very simply using ImageMagick though I only found it through trial and error. The command that I use to remove restrictions from PD's is:

mogrify c:\workingdirectory\password_protected.pdf

Basically it will cause ImageMagick to recreate the PDF exactly the way it is, but since ImageMagick is unable to add restrictions the result is the same PDF without restrictions. Let me know if you have any issues and I can send you an example of the script I use for it.

Levi

Posted 2012-05-06T17:36:53.197

Reputation: 39

Note this will also remove some other PDF features and in most cases rasterize the entire file. I realize this question wanted an answer using ImageMagick but it would be good to include a disclaimer about the huge side effect this produces and suggest a tools like pdftk as in this answer that actually understands Postscript and writes back out a similar file to what came it (including text and vector elements as text and vectors instead of pixels).

– Caleb – 2016-11-10T11:26:08.757

0

As Levi answered, you can use ImageMagick's mogrify. However to expand on that I found to actually get mogrify to work with a password protected file you may need to specify the password as:

mogrify -authenticate yourpassword c:\workingdirectory\password_protected.pdf

Warning! this will overwrite the file in-place

Furthermore, the quality of the pdf may be deteriorated by this because I believe the default density setting is 72dpi. So to maintain the quality try using 300dpi (or whatever you prefer):

mogrify -authenticate yourpassword -density 300 c:\workingdirectory\password_protected.pdf

User

Posted 2012-05-06T17:36:53.197

Reputation: 2 430

Note even with the density setting this is not maintaining the quality of the PDF because it is rasterizing the entire file. I realize this question wanted an answer using ImageMagick but it would be good to include a disclaimer about the huge side effect this produces and suggest a tools like pdftk as in this answer that actually understands Postscript and writes back out a similar file to what came it (including text and vector elements as text and vectors instead of pixels).

– Caleb – 2016-11-10T11:27:09.230