In GIMP, I can import a .pdf, and use the GUI to flatten it (if it was made with many layers) by selecting "Flatten Image" in the "Image" dropdown menu. I can then export the .pdf with a new filename.

I'd like to automate this. Is there some way to do it via the terminal?

  • 1
    install imagemagick and read man mogrify-im6 , montage-im6 , display-im6 , stream-im6 , identify-im6 , import-im6 , conjure-im6, composite-im6 , convert-im6 , animate-im6 and compare-im6 . – PersianGulf Oct 19 '14 at 0:29

I found these 2 method via Google, in this thread titled: Re: Flattening PDF Files at the UNIX Command Line.

Method #1 - using Imagemagick's convert:
$ convert orig.pdf flattened.pdf 

NOTE: The quality is reported to be so so with this approach.

Method #2 - Using pdf2ps -> ps2pdf:
$ pdf2ps orig.pdf - | ps2pdf - flattened.pdf 

NOTE: This method is reported to retain the image quality.

  • Better quality than GIMP on the second -- thanks! – generic_user Oct 19 '14 at 0:48
  • @ACD - that's good to know. Glad it solved your Q. – slm Oct 19 '14 at 0:49
  • 1
    Method 1 generated a very fuzzy image, while method 2 worked perfectly. – Severyn Kozak Jun 20 '15 at 1:06
  • Unfortunately, Method #2 does not rasterize the image, so if you are trying to block out sensitive portions, a user could still open the document and remove layers (with something like Inkscape). You can, however, modify the resolution of Method #1: $ convert -density 150 {original,flattened}.pdf If you need to preserve disk space, you can use -type Grayscale or -monochrome or things of that sort. – eacousineau Oct 8 '16 at 21:28
  • 1
    Both methods will rasterize the pdf, although the method #2 does it at a much higher resolution. So none of these methods are satisfying. – Ant Jan 29 '17 at 16:47

Ghostscript (gs) worked better than pdf2ps and convert for me. Quality was hardly degraded and file size is small.

-sColorConversionStrategy=/LeaveColorUnchanged  \
-dAutoFilterColorImages=true \
-dAutoFilterGrayImages=true \
-dDownsampleMonoImages=true \
-dDownsampleGrayImages=true \
-dDownsampleColorImages=true \
-sOutputFile=document_flat.pdf document_original.pdf

Found here: http://zeroset.mnim.org/2015/01/07/flatten-pdfs-with-ghostscript/

  • This method works great, the pdf is not rasterized and text is conserved as text – Ant Jan 29 '17 at 16:50
  • I tried using this to remove a transparency layer via flattening, but it did not work for me. – mikemtnbikes Nov 6 '18 at 21:13

Although convert will keep the same file size I've found it to be slow.

The pdf2ps ps2pdf method is faster but I noticed for me it was increasing the file size.

pdftk is nice because it is not only fast but also retains a similar file size.

This is what I use to bulk flatten a directory.

    function pdfflatten () {
        pdftk "$1" output "$2" flatten
    export pdfflatten
    alias pdfflattenDIR='mkdir flattenedPDFs; for i in `seq $(ls *.pdf | wc -l)`; do a=`ls *.pdf | head -$i | tail -1`; pdfflatten "$a" flattenedPDFs/"$a"; done'
  • pdftk flatten does not change the images within a PDF. From the docs it "merges an input PDF’s interactive form fields (and their data) with the PDF’s pages" – Aidan Kane Jul 31 '18 at 7:54
  • To be clear, this approach doesn't flatten transparent layers (as indicated above). – mikemtnbikes Nov 6 '18 at 21:15

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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