Can someone please tell me how to remove the last page of a PDF file, using PDFtk?
3 Answers
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 filecat
is the operation1-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 fileoutput.pdf
is the output pdf file
More examples are here: https://www.pdflabs.com/docs/pdftk-cli-examples/

- 5,414
- 6
- 40
- 72

- 4,869
- 3
- 17
- 30
-
6If you want to remove more than one page, you can change the range, for example `1-r3` does all but the last two pages. – mlissner Oct 14 '15 at 17:27
-
10One can also specify multiple ranes, divided by spaces. For example: 1-5 7-10 14-r2 – MakisH Nov 19 '19 at 23:24
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 help
if [ "$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 1
fi
### Check we have pdftk available
if [ ! -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 2
fi
### Check our input is OK
INFILE="$1"
if [ ! -r "$INFILE" ]; then
echo "$script: Failed to read [$INFILE]"
exit 2
fi
OUTFILE="$2"
if [ "$OUTFILE" = "" ]; then
echo "$script: Will overwrite [$INFILE] if processing is ok"
fi
timestamp=`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"
fi
fi
### Clean Up
if [ -f "$tmpfile" ]; then
rm -f "$tmpfile"
fi

- 14,398
- 5
- 31
- 53
-
1That awk command could be modified to do the decrement, somewhat simplifying the script: grep Pages | awk '{ printf("%d",$2 - 1); }' This makes the $(( $page_count - 1 )) redundant. – Kingsley Oct 06 '13 at 22:06
-
No need for `pdfinfo` actually, `pdftk` provides the number of pages with the `dump_data` sub-command: it produces a line `NumberOfPages: 8` – kebs Jul 14 '14 at 09:18
-
2Also keep in mind that you don't need to know the page count in order to remove the last page (at least for pdftk versions 1.45 and greater). You can use `1-r2` to indicate first to second to last pages. See `pdftk` documentation which says the following: 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. – Six Oct 13 '15 at 14:12
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

- 2,308
- 1
- 16
- 18
-
Thank you for creating such a tool. Works like magic. The only thing I would wish: of the output file is not specified, the "in.pdf" file is renamed automatically (like, for example, "in_1--2.pdf") – lanenok Oct 06 '15 at 06:59
-
I also want to reiterate what an incredible piece of kit this is! It is exceptionally easy to trim the first or last few pages (or anything else). Even the distribution of the software is perfection, which consists of a single static binary (for Linux 32, Linux 64, OSX, and Windows). It includes great PDF documentation as well. I am surprised to have never encountered the software before, perhaps it is because it is not open source (only free for non-commercial use from what I understand). – Six Oct 13 '15 at 13:59