diff --git a/api.php b/api.php index e4b5698..9ef76c4 100644 --- a/api.php +++ b/api.php @@ -55,6 +55,12 @@ switch ($action) { case 'image': handleImage(); break; + case 'remediate': + handleRemediate(); + break; + case 'download': + handleDownload(); + break; default: error('Invalid action'); } @@ -389,6 +395,115 @@ function handleImage() { exit; } +/** + * Auto-remediate PDF accessibility issues + */ +function handleRemediate() { + $job_id = $_POST['job_id'] ?? ''; + + if (empty($job_id)) { + error('Job ID required'); + } + + $meta_file = RESULTS_DIR . '/' . $job_id . '.meta.json'; + $result_file = RESULTS_DIR . '/' . $job_id . '.result.json'; + + if (!file_exists($meta_file) || !file_exists($result_file)) { + error('Job not found'); + } + + $job_data = json_decode(file_get_contents($meta_file), true); + $result_data = json_decode(file_get_contents($result_file), true); + + // Check if there are fixable issues + if (!isset($result_data['auto_fixable_count']) || $result_data['auto_fixable_count'] == 0) { + error('No auto-fixable issues found'); + } + + $original_pdf = $job_data['filepath']; + $remediated_pdf = UPLOAD_DIR . '/' . $job_id . '_remediated.pdf'; + + // Use absolute venv path + $venv_python = '/Users/daveporter/Desktop/CODING-2024/PDF-Accessibility-checker/venv/bin/python3'; + $python_bin = file_exists($venv_python) ? $venv_python : 'python3'; + $remediation_script = __DIR__ . '/pdf_remediation.py'; + + // Build command - apply all safe fixes + $cmd = escapeshellcmd($python_bin . ' ' . $remediation_script) . ' ' . + escapeshellarg($original_pdf) . ' ' . + '--output ' . escapeshellarg($remediated_pdf) . ' ' . + '--all'; + + // Set PATH for poppler + $env_path = getenv('PATH'); + $poppler_paths = '/opt/homebrew/bin:/usr/local/bin'; + putenv("PATH={$poppler_paths}:{$env_path}"); + + // Run remediation + $error_log = RESULTS_DIR . '/' . $job_id . '.remediation.log'; + $cmd .= ' > ' . escapeshellarg($error_log) . ' 2>&1'; + + exec($cmd, $output, $return_code); + + // Check if remediation succeeded + if ($return_code !== 0 || !file_exists($remediated_pdf)) { + $log_content = file_exists($error_log) ? file_get_contents($error_log) : 'Unknown error'; + error('Remediation failed: ' . substr($log_content, -500)); + } + + // Store remediated file info + $job_data['remediated_pdf'] = $remediated_pdf; + $job_data['remediated_at'] = date('Y-m-d H:i:s'); + file_put_contents($meta_file, json_encode($job_data, JSON_PRETTY_PRINT)); + + success([ + 'job_id' => $job_id, + 'remediated_pdf' => basename($remediated_pdf), + 'original_filename' => $job_data['original_filename'], + 'fixes_applied' => $result_data['auto_fixable_count'], + 'download_url' => 'api.php?action=download&job_id=' . $job_id . '&type=remediated', + 'message' => 'PDF remediated successfully' + ]); +} + +/** + * Download original or remediated PDF + */ +function handleDownload() { + $job_id = $_GET['job_id'] ?? ''; + $type = $_GET['type'] ?? 'original'; // 'original' or 'remediated' + + if (empty($job_id)) { + error('Job ID required'); + } + + $meta_file = RESULTS_DIR . '/' . $job_id . '.meta.json'; + + if (!file_exists($meta_file)) { + error('Job not found'); + } + + $job_data = json_decode(file_get_contents($meta_file), true); + + if ($type === 'remediated') { + if (!isset($job_data['remediated_pdf']) || !file_exists($job_data['remediated_pdf'])) { + error('Remediated PDF not found'); + } + $file_path = $job_data['remediated_pdf']; + $filename = pathinfo($job_data['original_filename'], PATHINFO_FILENAME) . '_fixed.pdf'; + } else { + $file_path = $job_data['filepath']; + $filename = $job_data['original_filename']; + } + + // Serve the file + header('Content-Type: application/pdf'); + header('Content-Disposition: attachment; filename="' . $filename . '"'); + header('Content-Length: ' . filesize($file_path)); + readfile($file_path); + exit; +} + /** * Send success response */ diff --git a/index.html b/index.html index ae6c679..da15854 100644 --- a/index.html +++ b/index.html @@ -1084,12 +1084,74 @@ fixesList.innerHTML = fixesHTML; } - function applyFixes() { - // This would call the API to remediate the PDF - alert('Auto-remediation feature coming soon! This will:\n\nβ’ Add missing metadata\nβ’ Set document language\nβ’ Add bookmarks\nβ’ Generate a fixed PDF for download'); + async function applyFixes() { + const btn = document.getElementById('applyFixesBtn'); + const resultDiv = document.getElementById('fixResult'); - // TODO: Implement API call to remediation endpoint - // const response = await fetch('api.php?action=remediate&job_id=' + currentJobId); + // Disable button + btn.disabled = true; + btn.innerHTML = ' Applying fixes...'; + + resultDiv.style.display = 'block'; + resultDiv.innerHTML = '