Remove the Last Page of a PDF File Using PDFtk

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/

Batch for removing last 2 pages from PDF files on a folder

The task can be done with a batch file with only following single command line:

@for %%I in ("C:\Desktop\long\*.pdf") do @"C:\Program Files (x86)\PDFtk\bin\pdftk.exe" "%%I" cat 1-r3 output "C:\Desktop\short\%%~nxI" && echo Successfully processed "%%~nxI" || echo ERROR: Failed to process "%%~nxI"

This command line uses the Windows command FOR to process all PDF files in the specified folder. For each PDF file is executed pdftk with the fully qualified file name of the current PDF file as input file name and the file name + extension with a different directory path as output file name. Run for /? in a command prompt window for help on this command.

There is output the success message on pdftk.exe exits with value 0. Otherwise the error message is output on pdftk.exe exits with value not equal 0.

The two @ are for suppressing the output of the FOR command line and of each executed pdftk command line on processing the PDF files in the specified folder.

Please see single line with multiple commands using Windows batch file for an explanation of the conditional operators && and ||.

Removing last page of PDF using Python or Bash

You can use pdfinfo in order to get the number of pages from the pdf and ghostscript to delete the last one:

#!/bin/bash

for file in *.pdf
do
page_nb=$(pdfinfo $file | awk '/^Pages/ { print $2 }')

file_name=$(echo $file | cut -d'.' -f 1)

gs -sDEVICE=pdfwrite -dNOPAUSE -dQUIET -dBATCH -dFirstPage=1 -dLastPage=$(expr $page_nb - 1) -sOutputFile=$file_name"_without_last_page.pdf" $file
done

Use ghostscript to delete a page (not extracting a range)

Use the right tool for the job!

For reasons outlined by KenS, Ghostscript is not the best tool for what you want to achieve. A better tool for this task is pdftk. To remove the 2nd page from input.pdf, you should run this command line:

pdftk  input.pdf  cat 1 3-end  output output.pdf

How to stamp only the last page of a PDF

If you want to work only with pdftk, it looks like you will have to extract the last page using the cat command of pdftk, stamp it, then join it with the first pages - or prepare a PDF with the appropriate number of blank pages and use the "multistamp" command.

How can I programmatically remove a page from a PDF document on a Mac?

Use pdftk.

To remove page 8:

pdftk in.pdf cat 1-7 9-end output out.pdf

Remove First Page from a Series of PDFs

Loop on all issues. Output is named after issue by replacing "issue" by "output". The first line extract page 1, the second line extract the other pages:

for issue in *_issue*.pdf
do
pdftk ${issue} cat 1 output page1_${issue/issue/output}
pdftk ${issue} cat 2-end output otherpages_${issue/issue/output}
done

PDFs - Delete all pages except the 1st page in a PDF in a folder

Thank Lit for your tips, worked with that to get the answer.
I created the following script

@echo off
set OutputFilePath=<output path>

pushd
for %%i in (*.pdf) do (
pdftk "%%i" cat 1 output "%OutputFilePath%%%~ni-1stpage%%~xi"
)
popd


Related Topics



Leave a reply



Submit