Skip to content

Instantly share code, notes, and snippets.

@lincetto
Created June 15, 2020 12:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lincetto/61df4a218456a8619208e4f50cb8b43e to your computer and use it in GitHub Desktop.
Save lincetto/61df4a218456a8619208e4f50cb8b43e to your computer and use it in GitHub Desktop.
Split multiple PDF files in linux using qpdf
#!/bin/bash
function process() {
echo "Processing $file"
totpages=`qpdf --show-npages "$file"`
echo "Processing $file : tot pages -> $totpages"
numsplit=$((totpages/2))
echo "Processing $file : tot splitted files -> $numsplit"
for currsplit in $(seq 1 $numsplit);
do
echo "Exporting pages $(((currsplit-1)*2+1))-$((currsplit*2)) from $file"
qpdf "$file" --pages "$file" $(((currsplit-1)*2+1))-$((currsplit*2)) -- ./OUTPUT/"${file%.*}"-$(((currsplit-1)*2+1))-$((currsplit*2)).pdf
done
}
find . -type f -name "*.pdf" -print0 | while IFS= read -r -d '' file;
do
process $file
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment