35

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?

fpmurphy
  • 4,556
  • 3
  • 23
  • 26
generic_user
  • 597
  • 2
  • 5
  • 10
  • 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 00:29

5 Answers5

48

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.

slm
  • 363,520
  • 117
  • 767
  • 871
  • 2
    Better quality than GIMP on the second -- thanks! – generic_user Oct 19 '14 at 00:48
  • @ACD - that's good to know. Glad it solved your Q. – slm Oct 19 '14 at 00:49
  • 5
    Method 1 generated a very fuzzy image, while method 2 worked perfectly. – Severyn Kozak Jun 20 '15 at 01:06
  • 4
    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 08 '16 at 21:28
  • 4
    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 https://stackoverflow.com/questions/6605006/convert-pdf-to-image-with-high-resolution – equaeghe Feb 20 '17 at 22:21
  • 1
    I'm using this for resizing notes. Method 2 took ages to convert 12 pages from 2 MB to 70 MB (pdf->ps) and back (ps -> pdf). Ghostscript did it faster. – Just_Alex Apr 01 '20 at 04:24
  • When I run the `convert` command I get an error. My PDF is only text, no images. error: ```convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/562. convert: no images defined `no_protect_raster.pdf' @ error/convert.c/ConvertImageCommand/3285.``` – carter Sep 01 '20 at 01:39
  • 1
    The second method increase the size of my PDF more than 10x, and lost text highlighting annotation created with Evince. – Alexey Nov 08 '20 at 09:51
23

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. Note the comment about removing the / before LeaveColorUnchanged.

AdminBee
  • 21,637
  • 21
  • 47
  • 71
Stan Bondi
  • 331
  • 2
  • 3
12

ghostscript changed their default for flattening annotations in late 2016.

To simply flatten a PDF now, you need -dPreserveAnnots=false

So a simple command line is now

gs -dSAFER -dBATCH -dNOPAUSE -dNOCACHE -sDEVICE=pdfwrite \
-dPreserveAnnots=false \
-sOutputFile=document_flat.pdf document_original.pdf
Lalo
  • 121
  • 1
  • 3
  • 1
    Thanks, worked well for me. This command seems to give the same output as Stan Bondi's answer while being simpler. Both were much quicker and gave a smaller file than using `convert`. – Jaydin Nov 19 '20 at 00:16
  • Ya, ditto. Worked for me too. – Diagon May 11 '23 at 22:03
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'
Jgar
  • 39
  • 1
0

I frequently need to flatten PDFs or make them image-only. I wanted to do this quickly from within Nautilus instead of going to a terminal, so I created two Nautilus scripts based on the previous answers. I am sharing the instructions in case they are useful to others.

Create script to flatten PDFs:

  1. Go to the Nautilus script folder ~/.local/share/nautilus/scripts/
  2. Create a new shell script file (eg FlattenPDF.sh)
  3. Make this file executable (eg Natiuls PropertiesPermissionsAllow executing file as program)
  4. Insert below text into the file:

#!/bin/bash

# Flattend PDFs    

IFS='
'
for file in $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS
do
  if [ -f "$file" ]; then
    base=${file%.*}
    ext=${file##*.}
    newname=${base}_flat.pdf
    gs -dSAFER -dBATCH -dNOPAUSE -dNOCACHE -sDEVICE=pdfwrite -dPreserveAnnots=false -sOutputFile=$newname $file
  fi
done

Create script to create image-only (text not searchable or selectable) PDFs:

  1. Go to the Nautilus script folder ~/.local/share/nautilus/scripts/
  2. Create a new shell script file (eg ImageOnlyPDF.sh)
  3. Make this file executable (eg Natiuls PropertiesPermissionsAllow executing file as program)
  4. Insert below text into the file:

#!/bin/bash

# Image-only PDFs    

IFS='
'
for file in $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS
do
  if [ -f "$file" ]; then
    base=${file%.*}
    ext=${file##*.}
    newname=${base}_img.pdf
    gconvert -density 200 $file $newname
  fi
done

Restart nautilus (eg type nautilus -q to quit Nautilus and then open Nautilus from the Dash again). If you right-click on a file, you should see the Scripts menu with the new scripts.

Jaydin
  • 101
  • 2