27

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 would like to automate this. Is there some way to do it via the terminal?

  • 2
    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
36

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 -density 300 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.

| improve this answer | |
  • 2
    Better quality than GIMP on the second -- thanks! – generic_user Oct 19 '14 at 0:48
  • 3
    Method 1 generated a very fuzzy image, while method 2 worked perfectly. – Severyn Kozak Jun 20 '15 at 1:06
  • 3
    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
  • 2
    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
  • 2
    Regarding Method #1, also see stackoverflow.com/questions/6605006/… – equaeghe Feb 20 '17 at 22:21
21

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

gs -dSAFER -dBATCH -dNOPAUSE -dNOCACHE -sDEVICE=pdfwrite \
-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/

| improve this answer | |
  • This method works great, the pdf is not rasterized and text is conserved as text – Ant Jan 29 '17 at 16:50
  • 2
    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
  • It changed a font for me but otherwise much better resolution than the other proposed methods. – ste Aug 16 '19 at 9:23
  • Is there a way to do this with batch files in place? – Clintus Apr 1 at 4:40
3

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'
| improve this answer | |
  • 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
  • 2
    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 agree to our terms of service, privacy policy and cookie policy

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