Monday, January 24, 2011

CropPDF integrated in Nautilus

During my master thesis I had to perform the following task several times: crop a PDF, i.e. remove the outer white borders of the PDF.

A software called pdfcrop (I think it is in the Ubuntu repositories) can do this. From a console the syntax is the following:

pdfcrop input.pdf output.pdf

I wanted to use this feature from my file browser (Nautilus), by right-clicking the file-to-be-cropped and choosing "Scripts -> Crop PDF". Here is how to do this:
  • Create a file called CropPDF located at ~/.gnome2/nautilus-scripts with the following content
  • #!/bin/bash
    pdfcrop $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS \
            $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS
  • Make it executable (chmod +x CropPDF)
  •  
That's it.
Caution: this modifies the file, on which you execute the operation!

1 comment:

  1. Nice tip. Here is a small improvement if you want to be able to convert multiple files at a time:

    #!/bin/bash
    echo "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" | while read filename; do
    if [ "$filename" != "" ]; then
    pdfcrop "$filename" "$filename"
    fi
    done

    If you use cinnamon and nemo, replace $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS with $NEMO_SCRIPT_SELECTED_FILE_PATHS

    ReplyDelete