Created
June 8, 2025 23:20
-
-
Save mgoellnitz/9ce584761398642dc7c7cb32addf6123 to your computer and use it in GitHub Desktop.
Remove one page from a PDF file - defaults to last one
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# Copyright 2021-2025 Martin Goellnitz | |
# | |
# This program is free software: you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation, either version 3 of the License, or | |
# (at your option) any later version. | |
# | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with this program. If not, see <http://www.gnu.org/licenses/>. | |
# | |
MODIFY=true | |
usage() { | |
echo "Usage: $0 [-m] [-p p] file1 [file2...]" 1>&2 | |
echo "" 1>&2 | |
echo " -p remove page p instead of last page" 1>&2 | |
echo " -m Don't modify file time of target file by a few seconds to slightly mark change" 1>&2 | |
exit 1 | |
} | |
if [ -z "$(which pdfinfo)" ] ; then | |
echo "To use this functionality, poppler-utils must be installed." | |
exit 1 | |
fi | |
if [ -z "$(which pdfjam)" ] ; then | |
echo "To use this functionality, texlive-extra-utils must be installed." | |
exit 1 | |
fi | |
SELECTED_PAGE="l" | |
while getopts "mp:" opt ; do | |
case "${opt}" in | |
m) | |
MODIFY= | |
;; | |
p) | |
SELECTED_PAGE=$OPTARG | |
;; | |
*) | |
usage | |
;; | |
esac | |
done | |
shift $((OPTIND-1)) | |
if [ -z "$*" ] ; then | |
echo "Input filenames missing as parameters" | |
echo "" | |
usage | |
exit 1 | |
fi | |
while [ ! -z "$*" ] ; do | |
INFILE="$1" | |
shift | |
if [ -f "$INFILE" ] ; then | |
PAGE=$SELECTED_PAGE | |
SELECT="" | |
# discover filenames | |
BASE=$(basename "$INFILE" .pdf) | |
BASE=$(basename "$BASE" .PDF) | |
ORIGINAL=$BASE-orig.pdf | |
if [ -z "$(uname -v|grep Darwin)" ] ; then | |
FILETIME=$(stat -c %Y -- "$INFILE") | |
else | |
FILETIME=$(stat -t %s -f %m -- "$INFILE") | |
fi | |
if [ ! -z "$MODIFY" ] ; then | |
FILETIME=$(expr 4 + $FILETIME) | |
fi | |
# enumerate pages except one | |
PAGES=$(pdfinfo "$INFILE"|grep ^Pages|tail -1|cut -d ':' -f 2|sed -e 's/\ //g') | |
# echo $PAGES | |
# Move original file | |
mv "$INFILE" "$ORIGINAL" | |
if [ "$PAGE" = "l" ] ; then | |
echo "Removing last page $PAGES" | |
PAGE=$PAGES | |
fi | |
P=1 | |
while [ $P -le $PAGES ] ; do | |
if [ "$P" -ne "$PAGE" ] ; then | |
if [ ! -z $SELECT ] ; then | |
SELECT="$SELECT," | |
fi | |
SELECT="$SELECT$P" | |
fi | |
# TODO: Maybe bug $P instead of P? | |
P=$(expr $P + 1) | |
done | |
# echo $ORIGINAL $SELECT | |
# transform file | |
if [ "$SELECT" = "1" ] ; then | |
pdfseparate -l 1 "$ORIGINAL" "$INFILE" | |
else | |
pdfjam --fitpaper true --rotateoversize true --suffix joined -o $INFILE $ORIGINAL $SELECT 2> /dev/null | |
fi | |
touch --date="$(date -R --date='@'$FILETIME)" "$INFILE" | |
ORIGSIZE=$(stat -c %s -- "$ORIGINAL") | |
NEWSIZE=$(stat -c %s -- "$INFILE") | |
# echo $INFILE: $ORIGSIZE $NEWSIZE | |
if [ "$NEWSIZE" = "0" ] ; then | |
echo "Problem processing $INFILE - ignoring." | |
mv "$ORIGINAL" "$INFILE" | |
fi | |
else | |
echo "$INFILE is not a file. Ignoring." | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment