Skip to content

Instantly share code, notes, and snippets.

@sven-bock
Created March 21, 2018 21:09
Show Gist options
  • Save sven-bock/8c18b57a2910cc0c96717b1e951e4975 to your computer and use it in GitHub Desktop.
Save sven-bock/8c18b57a2910cc0c96717b1e951e4975 to your computer and use it in GitHub Desktop.
Split PDF in single page PDFs
#!/bin/bash
# Splits the PDF in single pages PDFs which are saved in a subfolder called extracted and the page name.
# If the pages does already exist, it will increment the page number by one, until the filename is not occupied.
#sudo apt-get install pdftk
#chmod +x split_pdf.bash
#./split_pdf.bash source.pdf
PDFFILE=$1
echo "File: "$PDFFILE
if [ ! -f $PDFFILE ]; then
echo "File not found!"
exit 1
fi
pages=$(pdfinfo "${PDFFILE}" | grep Pages | sed 's/[^0-9]*//')
echo "Pages: "$pages
mkdir -p extracted
j=0
for i in $(seq $pages)
do
k=$((i+j))
while [ -f extracted/page$k.pdf ];
do
echo "File already exists: extracted/page"$k.pdf
j=$((j+1))
k=$((i+j))
done
echo "extracting page: "$i" to "$k
pdftk $PDFFILE cat $i output extracted/page$k.pdf
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment