#!/bin/bash # Get all non-dot directories in current directory dirs=() while IFS= read -r -d $'\0' dir; do dirs+=("$dir") done < <(find . -maxdepth 1 -type d ! -name ".*" -printf "%P\0") # Check if any directories found if [ ${#dirs[@]} -eq 0 ]; then echo "No non-dot directories found in $(pwd)." exit 1 fi # List directories numbered echo "Select a directory to generate PDF and PNG from index.html:" for i in "${!dirs[@]}"; do echo "$((i+1)). ${dirs[i]}" done # Prompt user for choice read -rp "Enter number (1-${#dirs[@]}): " choice # Validate choice: must be number and within range if ! [[ "$choice" =~ ^[0-9]+$ ]] || (( choice < 1 || choice > ${#dirs[@]} )); then echo "Invalid choice." exit 1 fi selected_dir="${dirs[choice-1]}" echo "You selected: $selected_dir" # Construct file URL abs_path="$(realpath "$selected_dir/index.html")" if [ ! -f "$abs_path" ]; then echo "index.html not found in $selected_dir" exit 1 fi file_url="file://$abs_path" output_pdf="$selected_dir/output.pdf" output_png="$selected_dir/output.png" # Run chromium headless to generate PDF chromium --headless --disable-gpu --allow-file-access-from-files --print-to-pdf="$output_pdf" "$file_url" if [ $? -ne 0 ]; then echo "Chromium PDF generation failed." exit 1 fi echo "PDF generated at $output_pdf" # Convert PDF to PNG (first page only) pdftoppm -png -singlefile "$output_pdf" "${selected_dir}/output" if [ $? -ne 0 ]; then echo "PDF to PNG conversion failed." exit 1 fi echo "PNG generated at $output_png"