Visual Analysis for "${word}"
${syllableBlocksHtml}
Explanation
${explanationHtml}
${noteHtml}
`;
};
// --- PDF Generation Function ---
const generatePdf = () => {
if (!analysisResult || !analysisResult.syllables) {
alert("Please analyze a valid word first.");
return;
}
const { jsPDF } = window.jspdf;
const originalButtonText = downloadPdfBtn.innerHTML;
downloadPdfBtn.innerHTML = 'Generating PDF...';
downloadPdfBtn.disabled = true;
pdfRenderContainer.innerHTML = buildPdfReportHtml(analysisResult);
html2canvas(pdfRenderContainer, { scale: 2, useCORS: true })
.then(canvas => {
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF({ orientation: 'portrait', unit: 'px', format: 'a4' });
const pdfWidth = pdf.internal.pageSize.getWidth();
const pdfHeight = pdf.internal.pageSize.getHeight();
const imgProps = pdf.getImageProperties(imgData);
const imgAspectRatio = imgProps.width / imgProps.height;
let finalWidth = pdfWidth;
let finalHeight = finalWidth / imgAspectRatio;
if (finalHeight > pdfHeight) {
finalHeight = pdfHeight;
finalWidth = finalHeight * imgAspectRatio;
}
pdf.addImage(imgData, 'PNG', 0, 0, finalWidth, finalHeight);
pdf.save(`Word_Stress_Report_${analysisResult.word}.pdf`);
})
.catch(err => {
console.error("Error generating PDF:", err);
alert("Sorry, there was an error creating the PDF.");
})
.finally(() => {
downloadPdfBtn.innerHTML = originalButtonText;
downloadPdfBtn.disabled = false;
pdfRenderContainer.innerHTML = '';
});
};
// --- Event Listeners ---
analyzeBtn.addEventListener('click', analyzeAndDisplay);
wordInput.addEventListener('keyup', (event) => {
if (event.key === 'Enter') {
analyzeAndDisplay();
}
});
downloadPdfBtn.addEventListener('click', generatePdf);
});