Skip to content

Instantly share code, notes, and snippets.

@knee-cola
Created October 10, 2024 07:10
Show Gist options
  • Save knee-cola/c3f69488b37ef34f9605e8826320fc2e to your computer and use it in GitHub Desktop.
Save knee-cola/c3f69488b37ef34f9605e8826320fc2e to your computer and use it in GitHub Desktop.
Splits a given a multi-page PDF document info multiple single-page files
#!/bin/bash
# pdfsplit.sh [input-file.pdf]
#
# Example: pdfsplit.sh input.pdf
#
# written by: ChatGPT under supervision of knee-cola
# Check if input PDF file is provided
if [ -z "$1" ]; then
echo "Usage: $0 <input-pdf-file>"
exit 1
fi
# Input PDF file
input_pdf="$1"
# Extract the base name without extension
base_name=$(basename "$input_pdf" .pdf)
# Get the total number of pages in the PDF
num_pages=$(pdfinfo "$input_pdf" | grep "Pages" | awk '{print $2}')
echo "Splitting PDF: $input_pdf"
# Split the PDF into individual pages
for ((i=1; i<=num_pages; i++)); do
output_pdf="${base_name}_page_$i.pdf"
gs -sDEVICE=pdfwrite -q -dNOPAUSE -dBATCH -dSAFER -dFirstPage=$i -dLastPage=$i -sOutputFile="$output_pdf" "$input_pdf"
echo " $output_pdf"
done
echo "PDF splitting complete. Total pages: $num_pages"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment