// PDF Comparison Tool JavaScript document.addEventListener('DOMContentLoaded', function() { const uploadForm = document.getElementById('uploadForm'); const loadingSection = document.getElementById('loadingSection'); const resultsSection = document.getElementById('resultsSection'); const errorSection = document.getElementById('errorSection'); const errorMessage = document.getElementById('errorMessage'); // Handle form submission uploadForm.addEventListener('submit', function(e) { e.preventDefault(); const formData = new FormData(uploadForm); const pdf1 = document.getElementById('pdf1').files[0]; const pdf2 = document.getElementById('pdf2').files[0]; // Validate files if (!pdf1 || !pdf2) { showError('Please select both PDF files.'); return; } if (!pdf1.name.toLowerCase().endsWith('.pdf') || !pdf2.name.toLowerCase().endsWith('.pdf')) { showError('Please select valid PDF files.'); return; } // Show loading showLoading(); hideError(); // Submit form via AJAX fetch('/upload', { method: 'POST', body: formData }) .then(response => response.json()) .then(data => { hideLoading(); if (data.success) { displayResults(data.results); } else { showError(data.error || 'An error occurred during comparison.'); } }) .catch(error => { hideLoading(); showError('Network error: ' + error.message); }); }); function showLoading() { loadingSection.style.display = 'block'; resultsSection.style.display = 'none'; errorSection.style.display = 'none'; } function hideLoading() { loadingSection.style.display = 'none'; } function showError(message) { errorMessage.textContent = message; errorSection.style.display = 'block'; resultsSection.style.display = 'none'; } function hideError() { errorSection.style.display = 'none'; } function displayResults(results) { resultsSection.style.display = 'block'; // Display visual comparison displayVisualComparison(results); // Display spelling issues displaySpellingIssues(results); // Display barcodes and QR codes displayBarcodes(results); } function displayVisualComparison(results) { const visualContent = document.getElementById('visualComparisonContent'); let html = '
'; if (results.text_comparison && results.text_comparison.length > 0) { results.text_comparison.forEach((page, index) => { html += `
Page ${page.page}
PDF 1
${page.annotated_images && page.annotated_images.pdf1 ? `PDF 1 Page ${page.page}` : '

No differences detected

' }
PDF 2
${page.annotated_images && page.annotated_images.pdf2 ? `PDF 2 Page ${page.page}` : '

No differences detected

' }
${page.color_differences && page.color_differences.length > 0 ? `
${page.color_differences.length} color difference(s) detected
` : '
No color differences
' }
`; }); } else { html += '

No visual comparison data available.

'; } html += '
'; visualContent.innerHTML = html; } function displaySpellingIssues(results) { const spellingContent = document.getElementById('spellingIssuesContent'); let html = ''; if (results.spelling_issues && results.spelling_issues.length > 0) { html += `
`; results.spelling_issues.forEach(issue => { const englishSuggestions = issue.suggestions.english.join(', ') || 'None'; const frenchSuggestions = issue.suggestions.french.join(', ') || 'None'; html += ` `; }); html += `
Word Original English Suggestions French Suggestions
${issue.word} ${issue.original_word} ${englishSuggestions} ${frenchSuggestions}
${results.spelling_issues.length} spelling issue(s) found
`; } else { html = '
No spelling issues detected.
'; } spellingContent.innerHTML = html; } function displayBarcodes(results) { const barcodesContent = document.getElementById('barcodesContent'); let html = ''; if (results.barcodes_qr_codes && results.barcodes_qr_codes.length > 0) { html += `
`; results.barcodes_qr_codes.forEach(barcode => { const position = `(${barcode.rect.left}, ${barcode.rect.top}) - (${barcode.rect.left + barcode.rect.width}, ${barcode.rect.top + barcode.rect.height})`; html += ` `; }); html += `
Type Data Position
${barcode.type} ${barcode.data} ${position}
${results.barcodes_qr_codes.length} barcode/QR code(s) detected
`; } else { html = '
No barcodes or QR codes detected.
'; } barcodesContent.innerHTML = html; } // Add file input change handlers for better UX document.getElementById('pdf1').addEventListener('change', function(e) { const file = e.target.files[0]; if (file) { const label = e.target.nextElementSibling; if (label && label.classList.contains('form-text')) { label.textContent = `Selected: ${file.name}`; } } }); document.getElementById('pdf2').addEventListener('change', function(e) { const file = e.target.files[0]; if (file) { const label = e.target.nextElementSibling; if (label && label.classList.contains('form-text')) { label.textContent = `Selected: ${file.name}`; } } }); });