Readability
${stats.readability}
Word Count
${stats.words}
Spelling Corrections (${issues.filter(i => i.type === 'spelling').length})
${spellingHtml}
Grammar Suggestions (${issues.filter(i => i.type === 'grammar').length})
${grammarHtml}
Style Improvements (${issues.filter(i => i.type === 'style').length})
${styleHtml}
`;
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();
});