Skip to main content
4 of 9
added 6 characters in body
bu5hman
  • 4.9k
  • 2
  • 16
  • 29

There may be a simpler way, but basing one on this solution using enscript and pdftk simply copy /usr/share/enscript/simple.hdr to ~/.enscript/myheader.hdr just to play with it as you need to set up anything bespoke and then ...

#!/bin/bash

input="$1"    #the name of the source pdf
label="$2"    #any text you want to add to the footer
from=$3       #the page number you want to start from
output="$4"   #the name of your output file

#get the number of pages in the pdf
pagenum=$(pdftk "$input" dump_data | grep "NumberOfPages" | cut -d":" -f2)

#calculate the last page number
to=$(($from+$pagenum-1))

enscript -L1 --header-font=Helvetica-Bold14 --fancy-header=myheader 
  --header="||" --footer="||$label $%" --output - < <(
       for i in $(seq $to); do echo; done
    ) | 
    ps2pdf - |
    pdftk - cat $from-$to output - | 
    pdftk "$input" multistamp - output "$output.pdf"

Use enscript to produce a 100+pagenum-1 page ps file with the footer in place, pipe this to ps2pdf to convert to pdf, then to pdftk to remove the unwanted 99 pages and finaly pipe these pages back into pdftk stamping them over your input file with the multistamp option

Other than that just loop over your files and feed them to the script

for f in *.pdf; do thisScript "$f" "hello world" 100 "${f%.*}.numbered.pdf"; done

The precise formatting can be finessed in myheader.hdr

Things get more complex if page sizes and orientations change in the pdf to be stamped.

bu5hman
  • 4.9k
  • 2
  • 16
  • 29