#!/bin/bash # 1. Check if both arguments were provided if [ -z "$1" ]; then echo "Error: Source folder not specified." echo "Usage: $0 " exit 1 fi if [ -z "$2" ]; then echo "Error: Output folder not specified." echo "Usage: $0 " exit 1 fi # 2. Assign arguments to variables and resolve absolute paths TARGET_DIR=$(cd "$1" 2>/dev/null && pwd) # 3. Check if the input directory actually exists if [ -z "$TARGET_DIR" ]; then echo "Error: Source directory '$1' does not exist." exit 1 fi # Set the final output path explicitly *before* creating or checking it OUTPUT_DIR="$2/pdfa" # 4. Create the output directory if it doesn't exist # This is the crucial fix for Ghostscript! if [ ! -d "$OUTPUT_DIR" ]; then echo "Creating output directory: $OUTPUT_DIR" mkdir -p "$OUTPUT_DIR" fi echo "Processing PDFs in: $TARGET_DIR" echo "Saving outputs to: $OUTPUT_DIR" echo "----------------------------------------" # 5. Loop through all PDF files (case-insensitive) shopt -s nullglob nocaseglob for pdf in "$TARGET_DIR"/*.pdf; do filename=$(basename "$pdf") echo "Processing: $filename..." # Run Ghostscript gs -sDEVICE=pdfwrite \ -dCompatibilityLevel=1.4 \ -dPDFSETTINGS=/ebook \ -dNOPAUSE \ -dQUIET \ -dBATCH \ -sOutputFile="$OUTPUT_DIR/$filename_converted" "$pdf" done shopt -u nullglob nocaseglob echo "----------------------------------------" echo "Done! Converted files are in: $OUTPUT_DIR"