Unlocking a protected PDF on Mac OS X

Recently I needed to demonstrate proof of purchasing something via my credit card statement. Easy enough, I download my most recent statement as a PDF file from American Express. Then I wanted to use Adobe Acrobat Pro’s nifty redaction features to redact all the irrelevant information from the appropriate page of the bill. Except Amex has decided that the statement should be a protected PDF, which means you can view it but cannot change it. This is of course totally bogus DRM, it’s my statement afterall! I suppose they hope to curb statement forgeries, but as anyone akamai knows: if I can view it, I can edit it. I think Preview.app on Mac OS X used to ignore DRM and let you edit protected PDFs, but doesn’t seem to on Snow Leopard.

I hunted around for a tool to unlock the PDF. There are lots of tools for Windows, which didn’t interest me. One person suggested opening the PDF and “printing” it to a PDF, but Adobe has disabled those features of the Print dialog box on Mac OS X (presumably since it would allow trivial circumvention of the DRM).

PDFKey Pro looks like a reasonable option for Mac OS X, but it is $25 which seems kinda steep for a single use. They have a downloadable demo, but it will just create an unlocked version of the first page of the PDF, which wasn’t the page I wanted. And of course I can’t edit the source PDF because it is protected, so the demo wasn’t useful to me.

Then I came upon MuPDF, which is a “lightweight PDF viewer and toolkit written in portable C”. It has an X11 GUI component, as well as command line tools. One of the command line tools is “pdfclean”, which will remove the DRM from a PDF.

Unfortunately, MuPDF isn’t in MacPorts yet, so I had to compile it by hand. It uses the Perforce jam tool instead of make, and has three library dependencies: zlib, libjpeg, and freetype2. Luckily, all of these are available in MacPorts, so I was able to install them and then edit the Jamrules file to point at the MacPorts location. Here is the updated section of Jamrules:


if $(OS) = MACOSX
{
    Echo Building for MACOSX ;

    BUILD_X11APP = true ;

    CCFLAGS = -Wall -std=gnu99 -I/opt/local/include -I/opt/local/include/freetype2 ;
    LINKFLAGS = -L/usr/X11R6/lib -L/opt/local/lib ;
    LINKLIBS = -lfreetype -ljpeg -lz -lm ;
    APPLINKLIBS = -lX11 -lXext ;

    if $(BUILD) = debug   { OPTIM = -g -O0 -fno-inline ; }
    if $(BUILD) = release { OPTIM = -O3 ; }

    if $(HAVE_JBIG2DEC) { LINKLIBS += -ljbig2dec ; }
    if $(HAVE_OPENJPEG)    { LINKLIBS += -lopenjpeg ; }
}

pdfclean worked like a charm, removing the DRM from the statement. After that I was able to redact the statement without incident.

Perhaps in my copious spare time I will make a MuPDF portfile for MacPorts, but until then perhaps this will help others who want an open source way to remove bogus PDF DRM.

43 thoughts on “Unlocking a protected PDF on Mac OS X

  1. Robert Brewer Post author

    Thanks for pointing that out! I could have sworn I looked there and despite some references to the Mac OS X executable, I couldn’t find it. Perhaps I just missed it.

    Reply
  2. steve

    Can someone walk me through how to build or compile this? I have never done this before. Thank you.

    Reply
  3. jim

    how do you install the mupdf folder once it;s downloaded? I can’t find step by step instructions anywhere.
    Walkthrough please!?

    Reply
    1. Robert Brewer Post author

      mupdf is a group of command line programs. If you have downloaded the binary version from http://mupdf.com/download/archive/mupdf-0.7-darwin-i386.tar.gz and then double-clicked the archive, you should have a folder called “mupdf-0.7-darwin-i386”. The command line tools are in that folder. If you’ve never used the Mac OS X command line, you’ll want to Google some instructions (outside the scope of this comment).

      From the command line (shell), you will need to type “cd name-of-directory” where you type in the name of the directory where your mupdf folder is. Then type “cd mupdf-0.7-darwin-i386”. Once there, you can run any of the commands by typing “./commandname”. For example, if you want to unlock a protected PDF, you would type “./pdfclean locked.pdf unlocked.pdf” where locked.pdf is the name of your locked PDF file (which might need the whole path to the file if it is in a different directory) and unlocked.pdf is the name you want to save the unlocked version as.

      I hope this helps.

      Reply
      1. Harman

        Thanks Robert.

        awsome… i was wondering here and there for the past 2-3 hours and finally I reached here and it worked like a magic.

      2. William

        Worked like a charm! Thanks much Robert!

        NOTE: It is easiest (does not require a directory path change) if you drop the PDF you would like to ‘unlock’ temporarily into the same folder as muPDF (which you have cd(ed) into already). Then you can follow the instructions exactly as Robert posted above.

  4. jim

    champion. thanks alot that’s all I needed!
    only thing is you put in 0.7-darwin-i386. I have “mupdf-0.5-macosx”. is the darwin version for os X? Is it more recent than the 0.5 i have?

    Reply
    1. Robert Brewer Post author

      Yes, darwin is the name for Apple’s open source Unix that underlies Mac OS X. 0.7 is a newer version, but 0.5 should also work fine for unlocking a PDF (in my experience)

      Reply
  5. Jakub

    Thanks a lot, you’ve helped me so much. Tried many tools, and they didn’t work or were to pricey for a one time use. pdfclean made my day.

    Reply
  6. Jessy Wind

    Thanks to author, for a good post and
    thanks for tha last advice from Darun. I tried this tool and i use it almost every day (i have a usial work with PDF docs).

    Reply
  7. brainstraining

    Thanks for this. The number of academic publishers that stop you copying and pasting from articles is unbelievable. It has to be counter-productive. I’ve got over a thousand papers in my collection and at least 20% are impaired in this fashion. Not anymore!

    Speaking of which can anyone tell me how to write a short shell script that would recursively use the pdfclean command on 1000 files? 😉

    Reply
  8. Pingback: Set public domain content free from locked PDF files

  9. Pingback: Adobe For Mac

  10. peacemahn

    On a mac, you can run the following command to create “unlocked.pdf” from “locked.pdf”:

    gs -sOutputFile=unlocked.pdf -sDEVICE=pdfwrite -dNOPAUSE -dbatch -q locked.pdf </dev/null

    Reply
  11. Debra Smith

    What an awful link, Peacemahn!
    Great post, Robert!
    By the way, I have some proposition to you! Could you contact me ASAP?
    I think you will see my e-mail in the comment form 🙂

    Reply
  12. Ryan

    Thank you thank you thank you! I’ve been working on a script using pdfkey for a few days.. turns out it was actually breaking my pdfs. I was about to go insane when I found this post.. MuPDF is PERFECT 🙂

    And it installed flawlessly with homebrew 🙂

    Reply
  13. Kirsche

    Hej 🙂 I even found a easier way to do it. Download the program “Combine PDFs” for mac. The open the unlocked file in this program, merge the PDF ( even though you don’t merge anything 🙂 ) and then store the file. The merged file will be unlocked 🙂

    Reply
  14. Danny

    I just ran:

    port install mupdf

    then,

    mupdfclean mylockedpdf.pdf myunlockedpdf.pdf

    Awesome! It’s in macports.

    Reply
  15. Jim

    Thank you. Very satisfying to learn that I am not the only one irritated with Amex policy. “It’s MY statement!”

    Reply
  16. Wolf Rentzsch

    MuPDF is now available via homebrew. This worked for me:

    $ brew install mupdf
    $ mutool clean -p thepassword input.pdf output.pdf

    Reply
  17. Pingback: Unlocking a password protected PDF file on Mac OSX/Linux | Pat's Blog

  18. sunk818

    +1 for mutool clean to remove password from PDF. Worked like a charm. http://mupdf.com/

    The version of BeCyPDFMetaEdit I downloaded asked for the password.

    I don’t know what the AmEx password for my statements are, so having mutool was a nice way to remove the password prompt. I didn’t even have to know the password.

    Reply
  19. saiedg

    Thank you Robert Brewer for this post! I have a question. Do you know where I can download a ‘pdfextract’ unix executable file? If it doesn’t exist, how do you recommend I go about creating that? Thank you!

    Reply

Leave a reply to brainstraining Cancel reply