Skip to content

Instantly share code, notes, and snippets.

@ThomasG77
Created February 18, 2021 16:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ThomasG77/0a04ef4aae660292ca5e8486df576b2d to your computer and use it in GitHub Desktop.
Save ThomasG77/0a04ef4aae660292ca5e8486df576b2d to your computer and use it in GitHub Desktop.
Need to split PDF every n pages, do it with pdftk
# Recipe from https://unix.stackexchange.com/questions/66931/split-pdf-into-documents-with-several-pages-each
pagesper=2
file=layout_atlas_multipage.pdf
number=$(pdfinfo -- "$file" 2> /dev/null | awk '$1 == "Pages:" {print $2}')
count=$((number / pagesper))
filename=${file%.pdf}
counter=0
while [ "$count" -gt "$counter" ]; do
start=$((counter*pagesper + 1));
end=$((start + pagesper - 1));
counterstring=$(printf %04d "$counter")
pdftk "$file" cat "${start}-${end}" output "${filename}_${counterstring}.pdf"
counter=$((counter + 1))
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment