Linux – How to extract and/or remove the last page of a bunch of PDFs

linuxpdf

One of our vendors started tacking on an unnecessarily huge image to the last page of PDFs we get from them. I need to trim this out. However, we have hundreds of these, so it's prohibitive to go in manually. What're the best ways to extract and then delete (Preferably first one, then the other; I still need to confirm via filesize that I'm not deleting one which doesn't have the image) the last page of a PDF automatically? OS is Linux.

I can extract it using ghostscript, with something along the lines of gs -dFirstPage=5 -dLastPage=5, but I need to automate this, I can't go through and manually find out what the number of the last page is.

Any ideas?

Edit: To clarify, I simply want to split out/delete the last page. Not the image in it, excise the last page period.

Best Answer

As @Daniel Andersson already commented, this can easily be done with pdftk:

pdftk input.pdf cat end-1 output temp.pdf
pdftk temp.pdf  cat end-2 output output.pdf
rm temp.pdf

I don't know if it can be done with one call to pdftk though...

Edit: you could combine it with thanosk's answer and use (in bash):

pdftk input.pdf cat 1-$((last-1)) output output.pdf

when you already extracted the last page to the variable $last.

Related Question