Skip to content

Instantly share code, notes, and snippets.

@KorigamiK
Created November 1, 2024 16:39
Show Gist options
  • Save KorigamiK/9169d3f5c99510908b10b5d51b0d3fc1 to your computer and use it in GitHub Desktop.
Save KorigamiK/9169d3f5c99510908b10b5d51b0d3fc1 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Loop through all week* directories
for dir in week*/; do
# Extract week number from directory name
week_num=$(echo "$dir" | grep -o '[0-9]\+')
# Enter directory
cd "$dir"
# For each PDF in current directory
for pdf in *.pdf; do
# Skip if no PDFs found
[ -f "$pdf" ] || continue
# Get total page count
pages=$(pdftk "$pdf" dump_data | grep "NumberOfPages" | awk '{print $2}')
# Calculate range to keep (skip first 3 and last 3)
start=4
end=$((pages - 3))
# Only process if enough pages exist
if [ $end -gt $start ]; then
pdftk "$pdf" cat $start-$end output "stripped_$pdf"
rm "$pdf"
mv "stripped_$pdf" "$pdf"
fi
done
# Run combine-pdfs command
combine-pdfs
# Rename combined.pdf to week*.pdf
mv combined.pdf "week${week_num}.pdf"
# Move back to parent directory
cd ..
# Move the week*.pdf to current directory
mv "${dir}week${week_num}.pdf" .
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment