`;
issuesList.innerHTML += card;
});
}
resultsContainer.classList.remove('hidden');
document.getElementById('generate-pdf-btn').addEventListener('click', prepareAndGeneratePdf);
}
function clearAll() {
textInput.value = '';
resultsContainer.classList.add('hidden');
issuesList.innerHTML = '';
analysisResults = {};
updateStats();
}
async function prepareAndGeneratePdf(event) {
const button = event.target;
const originalButtonText = button.innerHTML;
button.disabled = true;
button.innerHTML = 'Generating...';
const { text, issues, stats } = analysisResults;
const spellingHtml = issues.filter(i => i.type === 'spelling').map(i => `
`).join('') || '
`).join('') || '
`).join('') || '
`;
pdfTemplate.innerHTML = reportHtml;
// Temporarily make the hidden element visible for rendering
pdfTemplate.style.position = 'absolute';
pdfTemplate.style.left = '-9999px';
pdfTemplate.classList.remove('invisible');
try {
const { jsPDF } = window.jspdf;
const canvas = await html2canvas(pdfTemplate.querySelector('.pdf-report-container'), {
scale: 3, // Increased scale for higher quality and larger file size
backgroundColor: '#ffffff',
useCORS: true
});
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF({ orientation: 'p', unit: 'pt', format: 'a4' });
const pdfWidth = pdf.internal.pageSize.getWidth();
const pdfHeight = (canvas.height * pdfWidth) / canvas.width;
pdf.addImage(imgData, 'PNG', 0, 0, pdfWidth, pdfHeight);
pdf.save('Document_Analysis_Report.pdf');
showNotification('PDF report downloaded successfully!', false);
} catch (error) {
console.error("PDF Generation Failed:", error);
showNotification('Sorry, there was an error generating the PDF.', true);
} finally {
// Restore hidden state and button
pdfTemplate.style.position = '';
pdfTemplate.style.left = '-left-full';
pdfTemplate.classList.add('invisible');
button.disabled = false;
button.innerHTML = originalButtonText;
}
}
function findContext(fullText, phrase) {
const index = fullText.toLowerCase().indexOf(phrase.toLowerCase());
if (index === -1) return `"${phrase}"`;
const start = Math.max(0, index - 25);
const end = Math.min(fullText.length, index + phrase.length + 25);
let context = fullText.substring(start, end);
// Highlight the phrase
context = context.replace(new RegExp(phrase.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'), 'i'), `${phrase}`);
return context;
}
// --- Initial State ---
updateStats();
});
...${findContext(text, i.error)}...
Suggestion: Replace "${i.error}" with "${i.suggestion}".
No spelling issues found.
'; const grammarHtml = issues.filter(i => i.type === 'grammar').map(i => `...${findContext(text, i.error)}...
Consider replacing "${i.error}" with "${i.suggestion}". Reason: ${i.reason}
No grammar issues found.
'; const styleHtml = issues.filter(i => i.type === 'style').map(i => `...${findContext(text, i.error)}...
Consider replacing or removing "${i.error}". Reason: ${i.reason}
No style issues found.
'; const reportHtml = `Document Analysis Report
Generated on ${new Date().toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' })}
Overall Score
${100 - (issues.length * 5) > 0 ? 100 - (issues.length * 5) : 'N/A'}%
Readability
${stats.readability}
Word Count
${stats.words}
