A nifty little script for splicing pages into your PDF's

Wed 27 June 2012

Today, I was in dire need of inserting a page into a PDF without having to regenerate the entire document. I know there are products out there that do that, even cloud services such as foxyutils, but the PDF's are kind of sensitive and I didn't want to go through the whole dance of purchasing commercial software. So I built my own basic script in bash, using pdftk to manipulate the files.

Very, very simple use. Let's assume the following:

  • The name of the script below is splicer.sh and it is marked as executable
  • The original file is original.pdf
  • The additional page is addendum.pdf
  • You're placing the contents of addendum after the first page
  • The final file is final.pdf
Then this is how you would call the script from your linux command line:
./splicer.sh original.pdf addendum.pdf 1 final.pdf

Easy, right? So without further ado, here is the script for splicing pages into your PDF's:

#!/bin/bash
export original=$1
export addendum=$2
export splice_at=$3
part_two=$(expr $splice_at + 1)
export result=$4

pdftk $original cat 1-$splice_at output tmp_$result
pdftk $original cat $part_two-end output tmp2_$result
pdftk tmp_$result $addendum tmp2_$result cat output $result

rm tmp_$result tmp2_$result

blogroll

social