Remove the last page of a pdf file using PDFtk? Remove the last page of a pdf file using PDFtk? linux linux

Remove the last page of a pdf file using PDFtk?


This will create the outfile.pdf with all but the last page in infile.pdf

pdftk infile.pdf cat 1-r2 output outfile.pdf

Explanation of parameters

  • infile.pdf is the original pdf file
  • cat is the operation
  • 1-r2 is the page range
    • You can reference page numbers in reverse order by prefixing them with the letter r. For example, page r1 is the last page of the document, r2 is the next-to-last page of the document, and rend is the first page of the document. You can use this prefix in ranges, too, for example r3-r1 is the last three pages of a PDF.

  • output will output it to a specific file
  • output.pdf is the output pdf file

More examples are here: https://www.pdflabs.com/docs/pdftk-cli-examples/


You need to find out the page count, then use this with the pdftk cat function, since (AFAICT) pdftk does not allow one to specify an "offset from last".

A tool like 'pdfinfo' from Poppler (http://poppler.freedesktop.org/) can provide this.

Wrapping this in a bit of bash scripting can easily automate this process:

page_count=`pdfinfo "$INFILE" | grep 'Pages:' | awk '{print $2}'`page_count=$(( $page_count - 1 ))pdftk A="$INFILE" cat A1-$page_count output "$OUTFILE"

Obviously adding parameters, error checking, and what-not also could be placed in said script:

#! /bin/sh### Path to the PDF Toolkit executable 'pdftk'pdftk='/usr/bin/pdftk'pdfinfo='/usr/bin/pdfinfo'####################################################################script=`basename "$0"`### Script helpif [ "$1" = "" ] || [ "$1" = "-h" ] || [ "$1" = "--help" ] || [ "$1" = "-?" ] || [ "$1" = "/?" ]; then    echo "$script: <input-file.PDF> [<output-file.PDF>]"    echo "    Removes the last page from the PDF, overwriting the source"    echo "    if no output filename is given"    exit 1fi### Check we have pdftk availableif [ ! -x "$pdftk" ] || [ ! -x "$pdfinfo" ]; then    echo "$script: The PDF Toolkit and/or Poppler doesn't seem to be installed"    echo "    (was looking for the [$pdftk] and [$pdfinfo] executables)"    exit 2fi### Check our input is OKINFILE="$1"if [ ! -r "$INFILE" ]; then    echo "$script: Failed to read [$INFILE]"    exit 2fiOUTFILE="$2"if [ "$OUTFILE" = "" ]; then    echo "$script: Will overwrite [$INFILE] if processing is ok"fitimestamp=`date +"%Y%m%d-%H%M%S"`tmpfile="/tmp/$script.$timestamp"page_count=`$pdfinfo "$INFILE" | grep 'Pages:' | awk '{print $2}'`page_count=$(( $page_count - 1 ))### Do the deed!$pdftk A="$INFILE" cat A1-$page_count output "$tmpfile"### Was it good for you?if [ $? -eq 0 ]; then    echo "$script: PDF Toolkit says all is good"    if [ "$OUTFILE" = "" ]; then        echo "$script: Overwriting [$INFILE]"        cp -f "$tmpfile" "$INFILE"    else        echo "$script: Creating [$OUTFILE]"        cp -f "$tmpfile" "$OUTFILE"    fifi### Clean Upif [ -f "$tmpfile" ]; then    rm -f "$tmpfile"fi


With cpdf, you can reference a page by how far it is from the end of the document, using a tilde, as well as the beginning.

So, we can do

cpdf in.pdf 1-~2 -o out.pdf