21

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.

3
  • 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 . Dec 7, 2011 at 16:26
  • Can you provide a PDF sample document that contains these elements?
    – Daniel Beck
    Jan 25, 2012 at 13:48
  • I think so. Right now all my samples are student quizzes so I have to mock up something. Feb 2, 2012 at 18:01

5 Answers 5

7

WARNING: gs and pdf2ps|ps2pdf DO NOT Flatten PDFs!

Using gs or pdf2ps followed by ps2pdf will yield a multi-layer PDF with the content under annotations present in original form. You can verify this flaw in Preview by using Select All, then Copy, then Paste into a TextEdit window (in rich text mode). You will see the text or graphics under redaction annotations for example. This is clearly very bad if you legally need that content to be gone from the output.

A Working Solution

ImageMagick can produce a configurable quality, multi-page, single-layer flattened PDF with rasters of each page using the following command:

convert -density 150 document_original.pdf document_flat.pdf

This command rasterizes document_original.pdf, making an pixel-based image of each page, at 150 DPI, and outputs the result as document_flat.pdf.

A Note on Image Quality

Due to the rasterization, it produces a non-scalable (zoom and you'll see the text or original vector images become pixellated) PDF. It will likely have a larger filesize unless the original has very complex vector content like million-point scatter plots.

By changing density, you can trade larger file size for higher resolution output.

All text will be converted to raw pixels in each page image. Text and vector diagrams suffer the most, so experiment with the DPI until you get usable output files.

6
  • 1
    Thank you for contributing this answer. 5 years ago the solution I wrote was working for me. But I will give your answer a try the next time I have to do this (actually very soon). Dec 13, 2019 at 5:35
  • 2
    That makes sense. I imagine the behaviour of Ghostscript changed with respect to flattening annotations. I tried adding -PreserveAnnots=false and it still didn't work. Under the hood convert using Ghostscript for PDF processing. I'd like something that does what Acrobat does: it keeps the annotations, removes the content behind them, and preserves uncovered vector images. The Document Cloud piece with the new Acrobat really has me nervous using it for sensitive data though. Dec 14, 2019 at 17:39
  • 1
    This works for me now in that it flattens the PDF. Thanks. Jul 25, 2020 at 13:32
  • 1
    At least using Ubuntu 21.04 the convert command does not seem to work anymore: convert-im6.q16: attempt to perform an operation not allowed by the security policy PDF' @ error/constitute.c/IsCoderAuthorized/421. convert-im6.q16: no images defined out.pdf' @ error/convert.c/ConvertImageCommand/3229.
    – Prof.Chaos
    Aug 30, 2021 at 4:51
  • Does printing the document to PDF via, e.g., Preview work?
    – HappyFace
    Jun 20, 2022 at 20:07
11

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!

3
  • 4
    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. Oct 4, 2013 at 12:21
  • Actually, I had better luck with pdf2ps -q -sOutputFile=- file.pdf | ps2pdf - file_flat.pdf. Feb 21, 2014 at 21:01
  • 4
    Noting that on Mac you can install Ghostscript to make the above commands available via homebrew with brew install ghostscript.
    – Nick
    Nov 24, 2017 at 13:16
8

EDIT 2020-07-25 This used to work but doesn't anymore. Now these commands continue to preserve the PDF layers.


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>
2
  • The ps2pdf solutions failed for me. This worked, by apparently reindexing the fonts in the PDF.
    – raphink
    Oct 6, 2015 at 14:09
  • 1
    Neither of these commands flatten. You can copy the underlying layers using Preview and the original content will be present. Dec 13, 2019 at 4:03
1

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"
1
  • 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. Oct 4, 2013 at 12:22
0

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.

1
  • 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. Oct 4, 2013 at 12:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .