diff --git a/README.md b/README.md index 9ece07b..47f0157 100644 --- a/README.md +++ b/README.md @@ -2,12 +2,12 @@ ### Generating existing fliers -Make sure chromium is installed, in my case (on debian 13) I ran: +Make sure the deps are installed, in my case (on debian 13) I ran: ``` -sudo apt intall chromium +sudo apt intall chromium imagemagick ``` -Once you've verified the installation completed successfully, run a command like the one below to generate a pdf from the html files: +Once you've verified the installation completed successfully, run the provided script to generate a pdf/png from the html files: ``` -chromium --headless --disable-gpu --print-to-pdf="output.pdf" file:////home//git/html-fliers//index.html +./gen ``` \ No newline at end of file diff --git a/gen b/gen new file mode 100755 index 0000000..da238e8 --- /dev/null +++ b/gen @@ -0,0 +1,63 @@ +#!/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"