Skip to content

Instantly share code, notes, and snippets.

@drbr
Last active April 28, 2016 01:12
Show Gist options
  • Save drbr/23949fc921ead6b2917d to your computer and use it in GitHub Desktop.
Save drbr/23949fc921ead6b2917d to your computer and use it in GitHub Desktop.
A shell script that uses pdftk to break a PDF file into smaller files, each with an equal number of pages
#!/bin/zsh
if (($# != 2)); then
echo
echo "This script breaks the given PDF file into a set of PDf files,"
echo "each with block_size pages."
echo
echo "Usage:"
echo " extract_pages.sh filename block_size"
echo
exit 1
fi
filename=$1
block_size=$2
page_count=$(pdftk $filename dump_data &>/dev/null | grep NumberOfPages | awk '{print $2}')
echo; echo "Detected $page_count pages in the PDF file"; echo
num_blocks=$(($page_count / $block_size))
for i ({1..$num_blocks}); do
start_page=$((($i - 1) * $block_size + 1))
end_page=$(($i * $block_size))
echo "Running command:"
echo " pdftk $filename cat ${start_page}-${end_page} output ${i}.pdf"
pdftk $filename cat ${start_page}-${end_page} output ${i}.pdf
echo
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment