Created
January 31, 2025 17:03
-
-
Save cr3a7ure/5d0d3aae7375f164a4582da7da361c4e to your computer and use it in GitHub Desktop.
Split every PDF in a directory per X number of pages
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Split PDF files into multiple files with a specified number of pages. | |
# Usage: ./split_pdf.sh [increment] [directory] | |
# | |
increment=${1:-5} | |
directory=${2:-"."} | |
for file in "$directory"/*; do | |
max_page_num=$(pdftk "$file" dump_data | grep NumberOfPages | awk '{print $2}') | |
echo "> Total number of pages: $max_page_num" | |
echo "> Number of pages per output file(s): $increment" | |
if [ "$max_page_num" -lt "$increment" ]; then | |
echo "> Skip split" | |
continue | |
fi | |
for i in $(seq 1 "$increment" "$max_page_num"); do | |
num_first=$i | |
num_second=$(expr "$i" + "$increment" - 1) | |
echo "> Splitting pages: $num_first to $num_second" | |
if [ "$num_second" -gt "$max_page_num" ]; then | |
num_second=$max_page_num | |
fi | |
idx=$(expr "$i" / "$increment") | |
pdftk "$file" cat "$num_first"-"$num_second" output "${file%.pdf}-Part-${idx}.pdf" | |
done | |
echo "> PDF Split: ${file} Done." | |
done | |
echo "Done." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment