feat: accept PDF filename as input argument - Allow PDF file to be specified on the command line - Derive output JPEG name from input PDF basename - Generate files using IMG_<pdf-name>.jpg naming convention - Retain first-page conversion behavior - Improve script reusability for multiple PDFs
28 lines
583 B
Bash
Executable File
28 lines
583 B
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
# Convert the first page of a PDF to a JPEG image.
|
|
# Usage: ./pdf_to_jpg.sh <file.pdf>
|
|
|
|
set -euo pipefail
|
|
|
|
if [[ $# -ne 1 ]]; then
|
|
echo "Usage: $0 <file.pdf>"
|
|
exit 1
|
|
fi
|
|
|
|
INPUT_PDF="$1"
|
|
|
|
if [[ ! -f "$INPUT_PDF" ]]; then
|
|
echo "Error: '$INPUT_PDF' not found."
|
|
exit 1
|
|
fi
|
|
|
|
# Remove .pdf extension and prepend IMG_
|
|
BASE_NAME="$(basename "$INPUT_PDF" .pdf)"
|
|
OUTPUT_JPG="IMG_${BASE_NAME}.jpg"
|
|
|
|
echo "Converting first page of '$INPUT_PDF' to '$OUTPUT_JPG'..."
|
|
|
|
pdftoppm -f 1 -l 1 "$INPUT_PDF" | pnmtojpeg > "$OUTPUT_JPG"
|
|
|
|
echo "Done: $OUTPUT_JPG created." |