${title}
Name: _________________________
Date: _________________________
${problemsHtml}
${answersHtml}
`;
pdfContentArea.style.display = 'block';
pdfContentArea.style.position = 'absolute';
pdfContentArea.style.left = '-9999px';
pdfContentArea.style.width = '800px';
try {
const canvas = await html2canvas(pdfContentArea, { scale: 2 });
const imgData = canvas.toDataURL('image/png');
const { jsPDF } = window.jspdf;
const pdf = new jsPDF({ orientation: 'portrait', unit: 'px', format: 'a4' });
const pdfWidth = pdf.internal.pageSize.getWidth();
const imgHeight = canvas.height * pdfWidth / canvas.width;
pdf.addImage(imgData, 'PNG', 0, 0, pdfWidth, imgHeight);
pdf.save(`${title.replace(/[^a-z0-9]/gi, '-')}.pdf`);
} catch (error) {
console.error("PDF generation failed:", error);
alert("Could not generate PDF.");
} finally {
pdfContentArea.style.display = 'none';
}
};
const showError = (msg) => {
setupError.textContent = msg;
setupError.style.display = 'block';
};
const hideError = () => {
setupError.style.display = 'none';
};
// --- Event Listeners ---
generateInteractiveBtn.addEventListener('click', renderInteractiveWorksheet);
downloadPdfBtn.addEventListener('click', generatePdf);
submitWorksheetBtn.addEventListener('click', checkAnswers);
startOverBtn.addEventListener('click', () => switchView('setup'));
// --- Initial State ---
switchView('setup');
});
