Skip to content

Instantly share code, notes, and snippets.

@hadisfr
Last active September 10, 2023 10:39
Show Gist options
  • Save hadisfr/081e466e2e0c1edcd9874682b49ba186 to your computer and use it in GitHub Desktop.
Save hadisfr/081e466e2e0c1edcd9874682b49ba186 to your computer and use it in GitHub Desktop.
use pdftk to stamp a pdf on another one
#!/usr/bin/env bash
# PDF Stamper Scrtipt
#
# Author: H@di (info@hadisafari.ir)
#
# pdftk in.pdf multistamp stamp.pdf output out.pdf
# https://www.pdflabs.com/docs/how-to-add-headers-footers-watermarks-and-stamps-to-pdf/
#
if [ $# != 3 ]; then
echo -e "use:\t $0 source_file stamp_file dest_file"
exit 1;
fi
source_file="$1"
stamp_file="$2"
dest_file="$3"
t=$(pdftk "$source_file" dump_data | grep NumberOfPages)
source_num=${t:15}
temp_folder=".temp_fkasdnasxlaslnhgkjhjlhsa"
mkdir "$temp_folder"
for i in `seq 1 $source_num`; do pdftk "$source_file" cat $i output "$temp_folder"/$i.pdf; done
for i in `seq 1 $source_num`; do pdftk "$stamp_file" background "$temp_folder"/$i.pdf output "$temp_folder"/${i}e.pdf; done
for i in `seq 1 9`; do mv "$temp_folder"/${i}e.pdf "$temp_folder"/00${i}e.pdf &>/dev/null; done
for i in `seq 10 99`; do mv "$temp_folder"/${i}e.pdf "$temp_folder"/0${i}e.pdf &>/dev/null; done
pdftk "$temp_folder"/*e.pdf output "$dest_file"
rm -rf "$temp_folder"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment