39 lines
951 B
Bash
39 lines
951 B
Bash
#!/bin/bash
|
|
|
|
# Check if a PDF file is provided as an argument
|
|
if [ -z "$1" ]; then
|
|
echo "Usage: $0 <input_pdf_file>"
|
|
exit 1
|
|
fi
|
|
|
|
# Set input PDF filename from the argument
|
|
input_pdf="$1"
|
|
b=$(basename $input_pdf .pdf)
|
|
|
|
# Output directory for PNG files
|
|
output_dir="./output/"
|
|
|
|
# Ensure the output directory exists
|
|
mkdir -p "$output_dir"
|
|
|
|
echo "Converting $input_pdf to PNG files..."
|
|
|
|
# Use ImageMagick's convert command to loop through PDF pages and save each as a separate PNG file
|
|
string=$(pdfinfo "$input_pdf" | grep "Pages")
|
|
echo $string
|
|
number_regex="[0-9]+"
|
|
if [[ $string =~ $number_regex ]]; then
|
|
# echo ${BASH_REMATCH[0]}
|
|
number=${BASH_REMATCH[0]}
|
|
echo "number of pages $number"
|
|
fi
|
|
|
|
for page in $(seq -w 1 $number ); do
|
|
echo "Processing page $page"
|
|
proc_page=$(($page - 1))
|
|
echo "$proc_page"
|
|
magick convert -density 150 "$input_pdf[${proc_page}]" "$output_dir/${b}_page_${page}.png"
|
|
done
|
|
|
|
echo "Conversion complete."
|