Files
website/scripts/first_page_image.sh
Steffen Illium 3787dd2607
Some checks failed
Next.js App CI / docker (push) Failing after 34m41s
corrected shebang to ash
2025-09-15 09:29:14 +02:00

50 lines
1.3 KiB
Bash
Executable File

#!/bin/ash
# Converts the first page of each PDF in /public/publications to a PNG.
PDF_DIR="./public/publications"
OUTPUT_DPI=150
PNG_QUALITY=90
# Check if the directory exists
if [ ! -d "$PDF_DIR" ]; then
echo "Warning: Directory $PDF_DIR not found. Skipping PDF to PNG conversion."
exit 0
fi
# Check if magick command is available [1]
if ! command -v magick &> /dev/null
then
echo "Error: 'magick' command not found. Please install ImageMagick." [1]
exit 1
fi
echo "Searching for PDF files in $PDF_DIR..."
# Loop through all PDF files in the target directory [1]
find "$PDF_DIR" -type f -name "*.pdf" | while read -r pdf_file; do
base_name=$(basename "$pdf_file" .pdf)
output_png="${pdf_file%.pdf}.png"
# Only create the image if it doesn't already exist
if [ ! -f "$output_png" ]; then
echo "Processing: $pdf_file"
# Use magick to convert the first page ([0]) of the PDF to PNG [1]
magick \
-density "$OUTPUT_DPI" \
"$pdf_file[0]" \
-background white \
-flatten\
-quality "$PNG_QUALITY" \
"$output_png"
if [ $? -eq 0 ]; then
echo " -> Created: $output_png" [1]
else
echo " -> Error: Failed to create PNG for $pdf_file." [1]
fi
else
echo "Skipping: $output_png already exists."
fi
done
echo "PDF to JPG conversion finished."