<?php
/**
 * Smart PDF Generation Router
 * Automatically detects if DOMPDF is available and routes to appropriate script
 */

// Check if DOMPDF is available
function isDompdfAvailable() {
    // Method 1: Check if composer autoload exists and DOMPDF is installed
    if (file_exists(__DIR__ . '/vendor/autoload.php')) {
        require_once __DIR__ . '/vendor/autoload.php';
        return class_exists('Dompdf\Dompdf');
    }
    
    // Method 2: Check if DOMPDF classes can be loaded directly
    return class_exists('Dompdf\Dompdf') || class_exists('Dompdf\Options');
}

// Get all parameters from the URL
$query_string = $_SERVER['QUERY_STRING'];

if (isDompdfAvailable()) {
    // DOMPDF is available, use the main PDF generation script
    header("Location: generate_pdf_reports.php?" . $query_string);
} else {
    // DOMPDF is not available, use the fallback script
    header("Location: generate_pdf_fallback.php?" . $query_string);
}

exit();
?>