Skip to content

Instantly share code, notes, and snippets.

@donbrae
Created August 5, 2019 10:51
Show Gist options
  • Save donbrae/2369abb83a0f3d53fbc3aba963e80f7c to your computer and use it in GitHub Desktop.
Save donbrae/2369abb83a0f3d53fbc3aba963e80f7c to your computer and use it in GitHub Desktop.
Bash script to add page numbers to PDFs. Liftit frae http://forums.debian.net/viewtopic.php?t=30598#p174267.
#!/bin/sh
# we'll hide the work in a temporary directory
mkdir tmp_num
cp numbers.pdf tmp_num/.
cp newbook.pdf tmp_num/.
cd tmp_num/
# burst newbook into its component pages and extract total pages
pdftk newbook.pdf burst output book_%04d.pdf
cat doc_data.txt | grep NumberOfPages > nu_pages.txt
nu_pages=`mawk '{print $2}' nu_pages.txt`
# burst the page number file into its component pages
pdftk numbers.pdf burst output nums_%04d.pdf
# no page number on the first page
cp book_0001.pdf fin_0001.pdf
# start converting pages from page 2
x=2 # initialize x
# place the page numbers on each page of newbook
while [ "$x" -le "$nu_pages" ]; do
if [ "$x" -lt 10 ]; then
pdftk book_000"$x".pdf background nums_000"$x".pdf output fin_000"$x".pdf
echo "Finished page $x of $nu_pages."
x=$(($x+1))
elif [ "$x" -lt 100 ]; then
pdftk book_00"$x".pdf background nums_00"$x".pdf output fin_00"$x".pdf
echo "Finished page $x of $nu_pages."
x=$(($x+1))
elif [ "$x" -lt 1000 ]; then
pdftk book_0"$x".pdf background nums_0"$x".pdf output fin_0"$x".pdf
echo "Finished page $x of $nu_pages."
x=$(($x+1))
fi
done
# create the new PDF file and move it to the original directory
pdftk fin_*.pdf cat output finbook.pdf
mv finbook.pdf ../.
# clean up the mess and exit
# cd ..
# rm tmp_num/*
# rmdir tmp_num
echo " "
echo "All done! Have fun!"
echo " "
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment