Workaround for broken pdftk burst

Alec Jacobson

August 23, 2013

weblog/

The macports version of pdftk seems to be broken when using the burst command:

pdftk input.pdf burst output page-%02d.pdf

Is supposed to split apart a multipage pdf into single pages but instead produces a Java runtime exception:

Unhandled Java Exception:
java.lang.NullPointerException
   at com.lowagie.text.pdf.PdfCopy.copyIndirect(pdftk)
   at com.lowagie.text.pdf.PdfCopy.copyObject(pdftk)
   at com.lowagie.text.pdf.PdfCopy.copyDictionary(pdftk)

To get around this I wrote a short script. Save the following in a file called pdftkburst.sh:

#!/bin/bash
if [ $# -eq 0 ]
  then
    echo "Usage:"
    echo "  pdftkburst input.pdf output-%04d.pdf"
    return 1
fi

NUM_PAGES=`pdfinfo $1 | grep Pages: | sed -e "s/ *Pages: *//g"`
#echo "NUM_PAGES: $NUM_PAGES."
for i in $(seq 1 ${NUM_PAGES})
do
  #echo "printf \"$2\" $i"
  PAGE_NAME=`printf "$2" $i`
  pdftk $1 cat $i output $PAGE_NAME
  #echo "Creating $PAGE_NAME"
done

Then you can burst your pdfs using:

./pdftkburst.sh input.pdf page-%02d.pdf

Which indeed creates page-01.pdf, pages-02.pdf etc.