Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I use Mac OS X Snow Leopard. I have a PDF with form fields, annotations, and stamps on it. I would like to freeze (or "flatten") that PDF so that the form fields can't be changed and the annotations/stamps are no longer editable. Since I actually have many of these PDFs, I want to do this automatically on the command line.

Some things I've tried/considered, with their degree of success:

  • Open in Preview and Print to File. This creates a totally flat PDF without changing the file size. The only way to automate seems to be to write a kludgy UI-based AppleScript, though, which I've been trying to avoid.
  • Open in Acrobat Pro and use a JavaScript function to flatten. Again, not sure how to automate this on the command line.
  • Use pdftk with the flatten option. But this only flattens form fields, not stamps and other annotations.
  • Use cupsfilter which can create PDF from many file formats. Like pdftk this flattened only the form fields.
  • Use cups-pdf to hook into the Mac's printserver and save a PDF file instead of print. I used the macports version. The resulting file is flat but huge. I tried this on an 8MB file; the flattened PDF was 358MB! Perhaps this can be combined with a ghostscript call as in Ubuntu Tip:Howto reduce PDF file size from command line.

Any other suggestions would be appreciated.

share|improve this question

migrated from stackoverflow.com Dec 21 '11 at 19:46

This question came from our site for professional and enthusiast programmers.

1  
Automator.app has some built-in pdf processing options. Perhaps one of them can help directly or combined with one of the actions above . –  Kassym Dorsel Dec 7 '11 at 16:26
    
Can you provide a PDF sample document that contains these elements? –  Daniel Beck Jan 25 '12 at 13:48
    
I think so. Right now all my samples are student quizzes so I have to mock up something. –  Matthew Leingang Feb 2 '12 at 18:01

4 Answers 4

Try using pdf2ps and ps2pdf in succession. It's a little cumbersome but it helped me reduce my 30MB document down to 17MB. Not as much as I would have liked but still better. I got this idea from a friend who used this method to get rid of password permissions on a pdf.

pdf2ps your_pdf_file.pdf your_pdf_file.ps
ps2pdf your_pdf_file.ps your_pdf_file_from_ps.pdf

Hope that helps!

share|improve this answer
1  
GhostScript FTW! I got it down to a one-liner by using a pipe: pdf2ps file.pdf - | ps2pdf - file_flat.pdf. The file went from 300K to 500K which is big percentage increase but nothing like the 4400% increase the cups-pdf method yielded. –  Matthew Leingang Oct 4 '13 at 12:21
    
Actually, I had better luck with pdf2ps -q -sOutputFile=- file.pdf | ps2pdf - file_flat.pdf. –  Matthew Leingang Feb 21 at 21:01

It might be sub-optional, but have you considered chaining cups-pdf with pdftk or cupsfilter? Like you mentioned, cups-pdf will produce a flat pdf file and then pass it through pdftk or cupsfilter to get it down to an acceptable size?

Alternatively take a look at /System/Library/Printers/Libraries/convert You should be able to do something like

/System/Library/Printers/Libraries/convert -f input_file.pdf -o output_file.pdf -j "application/pdf"
share|improve this answer
    
My mac doesn't have /System/Library/Printers/Libraries/convert. I tried cupsfilter. When converting from PDF to PDF no change was made to the file. When converting from PDF to PS and then to PDF the annotations were lost. –  Matthew Leingang Oct 4 '13 at 12:22

Applescript is probably the best/quickest way to the command line. Once it works you can save it as an application and make an alias in your command shell to run it. Thereafter, you just type your alias at the command line.

share|improve this answer
    
But what's the easiest way to flatten a PDF in AppleScript? I'd rather not script the UI to mimic printing to file. That seems kludgy and slow. –  Matthew Leingang Oct 4 '13 at 12:04
up vote 0 down vote accepted

So this is what I've been using lately. I think functionally it does the same thing as pdf2ps file.pdf - | ps2pdf - file_flat.pdf, but it seemed to work better for me.

gs -sDEVICE=pdfwrite -dPDFSETTINGS=/default -dNOPAUSE -dQUIET -dBATCH -sOutputFile=<newfile> <oldfile>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.