有人可以告诉我如何使用 PDFtk 删除 PDF 文件的最后一页吗?
3 回答
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/
您需要找出页数,然后将其与 pdftk cat 函数一起使用,因为(AFAICT)pdftk 不允许指定“从最后一个偏移量”。
来自 Poppler ( http://poppler.freedesktop.org/ ) 的 'pdfinfo' 之类的工具可以提供此功能。
将其包装在一些 bash 脚本中可以轻松地自动化此过程:
page_count=`pdfinfo "$INFILE" | grep 'Pages:' | awk '{print $2}'`
page_count=$(( $page_count - 1 ))
pdftk A="$INFILE" cat A1-$page_count output "$OUTFILE"
显然,添加参数、错误检查和其他内容也可以放在所述脚本中:
#! /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