`;
} else {
sheetContent.innerHTML = html;
}
pdfDownloadBtn.style.display = 'block';
showPdsgTab(0);
}
async function downloadPdf() {
if (typeof jspdf === 'undefined' || typeof html2canvas === 'undefined') {
alert('PDF libraries are still loading...'); return;
}
const { jsPDF } = jspdf;
const content = container.querySelector('#pdsg-pdf-target');
container.classList.add('pdsg-pdf-view');
try {
const canvas = await html2canvas(content, { scale: 2, useCORS: true, logging: false });
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF({ orientation: 'portrait', unit: 'mm', format: 'a4' });
const pdfWidth = pdf.internal.pageSize.getWidth();
const imgProps = pdf.getImageProperties(imgData);
const pdfHeight = (imgProps.height * pdfWidth) / imgProps.width;
pdf.addImage(imgData, 'PNG', 0, 0, pdfWidth, pdfHeight);
pdf.save(`${sheetTitleInput.value.replace(/\s/g, '_') || 'portrait_practice'}.pdf`);
} catch (error) {
console.error("PDF Error:", error);
alert("An error occurred generating the PDF.");
} finally {
container.classList.remove('pdsg-pdf-view');
}
}
function escapeHTML(str) {
return str.replace(/[&<>"']/g, m => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[m]));
}
// --- 5. EVENT BINDING ---
tabButtons.forEach((btn, i) => btn.addEventListener('click', () => showPdsgTab(i)));
navNext.addEventListener('click', () => showPdsgTab(currentPdsgTab + 1));
navPrev.addEventListener('click', () => showPdsgTab(currentPdsgTab - 1));
generateBtn.addEventListener('click', generateSheet);
pdfDownloadBtn.addEventListener('click', downloadPdf);
// --- 6. INITIALIZATION ---
showPdsgTab(0);
});
