#!/bin/bash # find_optimal_scale_fixed.sh # This script helps find the optimal scaling factors for a PDF page with improved error handling # Default values PDF_FILE="" PAGE=1 MIN_X=0.1 MAX_X=1.5 MIN_Y=0.1 MAX_Y=1.5 STEPS=5 # Function to display usage information function show_usage { echo "Usage: $0 -f PDF_FILE [options]" echo "" echo "Required:" echo " -f, --file FILE Path to the PDF file" echo "" echo "Options:" echo " -p, --page PAGE Page number to analyze (default: 1)" echo " --min-x VALUE Minimum X scaling factor (default: 0.1)" echo " --max-x VALUE Maximum X scaling factor (default: 1.5)" echo " --min-y VALUE Minimum Y scaling factor (default: 0.1)" echo " --max-y VALUE Maximum Y scaling factor (default: 1.5)" echo " --steps NUMBER Number of steps between min and max (default: 5)" echo " -h, --help Display this help message" echo "" echo "Example:" echo " $0 -f document.pdf -p 3 --min-x 0.5 --max-x 1.0 --steps 6" } # Parse command line arguments while [[ $# -gt 0 ]]; do case "$1" in -f|--file) PDF_FILE="$2" shift 2 ;; -p|--page) PAGE="$2" shift 2 ;; --min-x) MIN_X="$2" shift 2 ;; --max-x) MAX_X="$2" shift 2 ;; --min-y) MIN_Y="$2" shift 2 ;; --max-y) MAX_Y="$2" shift 2 ;; --steps) STEPS="$2" shift 2 ;; -h|--help) show_usage exit 0 ;; *) echo "Error: Unknown option $1" show_usage exit 1 ;; esac done # Check if PDF file is provided if [ -z "$PDF_FILE" ]; then echo "Error: PDF file is required" show_usage exit 1 fi # Check if PDF file exists if [ ! -f "$PDF_FILE" ]; then echo "Error: PDF file '$PDF_FILE' not found" exit 1 fi # Get PDF basename for output files PDF_BASENAME=$(basename "$PDF_FILE" .pdf) OUTPUT_DIR="${PDF_BASENAME}_scale_test" mkdir -p "$OUTPUT_DIR" # Step 1: Run analyzer once to get the DocTags echo "Step 1: Analyzing page $PAGE to generate DocTags..." DOCTAGS_FILE="$OUTPUT_DIR/page_${PAGE}.doctags.txt" python analyzer.py --image "$PDF_FILE" --page "$PAGE" --output "$DOCTAGS_FILE" # Check for DocTags file with different extensions if [ ! -f "$DOCTAGS_FILE" ]; then # Try alternative extensions ALTERNATIVE_FILE="$OUTPUT_DIR/page_${PAGE}.doctags.doctags.txt" if [ -f "$ALTERNATIVE_FILE" ]; then echo "Found DocTags at alternative location: $ALTERNATIVE_FILE" DOCTAGS_FILE="$ALTERNATIVE_FILE" else # Try to find any doctags file in the output directory FOUND_FILE=$(find "$OUTPUT_DIR" -name "*.doctags.txt" -print -quit) if [ -n "$FOUND_FILE" ]; then echo "Found DocTags at location: $FOUND_FILE" DOCTAGS_FILE="$FOUND_FILE" else echo "Error: Failed to generate DocTags for page $PAGE" echo "Could not find any doctags files in $OUTPUT_DIR" exit 1 fi fi fi echo "Using DocTags file: $DOCTAGS_FILE" # Create HTML index to easily compare results INDEX_FILE="$OUTPUT_DIR/index.html" echo "
Click on any thumbnail to open the full visualization.