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:
- Go to the Nautilus script folder
~/.local/share/nautilus/scripts/
- Create a new shell script file (eg
FlattenPDF.sh
)
- Make this file executable (eg Natiuls
Properties
→ Permissions
→ Allow executing file as program
)
- 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:
- Go to the Nautilus script folder
~/.local/share/nautilus/scripts/
- Create a new shell script file (eg
ImageOnlyPDF.sh
)
- Make this file executable (eg Natiuls
Properties
→ Permissions
→ Allow executing file as program
)
- 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.