Complex Sentences
${analysisResults.complexCount}
% Complex
${complexPercent}%
Readability
${analysisResults.readability}
`;
document.getElementById('analyzed-text-output').innerHTML = analysisResults.sentenceDetails.map(s =>
s.isComplex ? `
${s.text}` : `
${s.text}`
).join(' ');
const complexListOutput = document.getElementById('complex-list-output');
if (analysisResults.complexSentences.length > 0) {
complexListOutput.innerHTML = analysisResults.complexSentences.map(s =>
`
${s.wordCount} words
${s.text}
`
).join('');
} else {
complexListOutput.innerHTML = `
No complex sentences found!
`;
}
}
// --- PDF GENERATION ---
async function generatePdfReport() {
downloadPdfBtn.disabled = true;
downloadPdfBtn.textContent = 'Generating...';
const complexSnippets = analysisResults.complexSentences.map(s =>
`
${s.wordCount} words
${s.text}
`).join('');
const reportHtml = `
COMPLEX SENTENCES
${analysisResults.complexCount}
AVG. SENTENCE LENGTH
${analysisResults.avgSentenceLength}
READABILITY SCORE
${analysisResults.readability}
Identified Complex Sentences
${complexSnippets || '
No sentences were flagged as overly complex based on the criteria (>= 25 words).
'}
`;
const pdfTemplate = document.getElementById('pdf-template');
pdfTemplate.innerHTML = reportHtml;
pdfTemplate.classList.remove('invisible');
try {
const { jsPDF } = window.jspdf;
const canvas = await html2canvas(pdfTemplate.querySelector('.pdf-page'), { scale: 2 });
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('Complex_Sentence_Report.pdf');
} catch (e) { console.error('PDF Generation Error:', e); } finally {
downloadPdfBtn.disabled = false;
downloadPdfBtn.textContent = 'Download Analysis Report';
pdfTemplate.classList.add('invisible');
pdfTemplate.innerHTML = '';
}
}
// --- EVENT LISTENERS ---
analyzeBtn.addEventListener('click', runAnalysis);
downloadPdfBtn.addEventListener('click', generatePdfReport);
});