`;
const pdfTemplate = document.getElementById('pdf-template');
pdfTemplate.innerHTML = reportHtml;
pdfTemplate.classList.remove('invisible');
try {
const { jsPDF } = window.jspdf;
const pdf = new jsPDF({ orientation: 'p', unit: 'pt', format: 'a4' });
const pages = pdfTemplate.querySelectorAll('.pdf-page');
for (let i = 0; i < pages.length; i++) {
const canvas = await html2canvas(pages[i], { scale: 2 });
if (i > 0) pdf.addPage();
const imgData = canvas.toDataURL('image/png');
const pdfWidth = pdf.internal.pageSize.getWidth(), pdfHeight = (canvas.height * pdfWidth) / canvas.width;
pdf.addImage(imgData, 'PNG', 0, 0, pdfWidth, pdfHeight);
}
pdf.save('Document_with_Notes.pdf');
} catch (e) { console.error('PDF Generation Error:', e); } finally {
downloadPdfBtn.disabled = false;
downloadPdfBtn.textContent = 'Download PDF';
pdfTemplate.classList.add('invisible');
pdfTemplate.innerHTML = '';
}
}
// --- UTILITY ---
function showNotification(message) {
notification.textContent = message;
notification.classList.remove('opacity-0');
setTimeout(() => notification.classList.add('opacity-0'), 2000);
}
// --- EVENT LISTENERS ---
mainTextInput.addEventListener('input', updateStateAndRender);
addNoteBtn.addEventListener('click', addNote);
downloadPdfBtn.addEventListener('click', generatePdfReport);
// --- INITIALIZATION ---
addNote();
document.getElementById('note-1').value = "See Johnson, C. (2022). A History of Primary Sources. University Press.";
addNote();
document.getElementById('note-2').value = "Fernandez, M. (2023). Contextual Analysis in Historical Research. Journal of Historical Methods, 45(2), 112-128.";
updateStateAndRender();
});