Cold War Spy Missions: Debriefing Files
Select a mission dossier from the archives above to view declassified information.
${mission.notable_figures}
` : ''} `; } downloadPdfBtn.addEventListener('click', () => { if (!currentMission) { alert("Please select a mission dossier first."); return; } if (typeof jsPDF === 'undefined' || typeof jsPDF.API === 'undefined') { // Check for jsPDF only alert("PDF generation library (jsPDF) not loaded. Please check your internet connection or ensure the CDN link is active."); console.error("jsPDF is not loaded."); return; } try { const { jsPDF: JSPDF } = window.jspdf; const doc = new JSPDF(); doc.setFontSize(18); doc.setFont('helvetica', 'bold'); doc.text("Cold War Spy Mission: Debrief", 105, 22, { align: 'center' }); doc.setFontSize(16); doc.setFont('helvetica', 'normal'); doc.text(currentMission.name, 105, 32, { align: 'center' }); let yPos = 45; const leftMargin = 14; const contentWidth = 180; const lineSpacingFactor = 4.5; // mm per line of text const sectionSpacing = 3; // mm extra space after a section's content const itemSpacing = 2; // mm space after a list item function addWrappedText(text, x, y, options = {}) { const maxWidth = options.maxWidth || contentWidth; const splitText = doc.splitTextToSize(String(text), maxWidth); doc.text(splitText, x, y, options); return y + (splitText.length * lineSpacingFactor); } function addSection(title, content, isList = false) { if (yPos > 255) { // Check before adding new section title doc.addPage(); yPos = 20; } doc.setFontSize(12); doc.setFont('helvetica', 'bold'); yPos = addWrappedText(title, leftMargin, yPos) - (lineSpacingFactor) + 2; // Add title, adjust yPos, add small space doc.setFontSize(10); doc.setFont('helvetica', 'normal'); if (isList && Array.isArray(content)) { content.forEach(item => { if (yPos > 270) { // Check before each list item doc.addPage(); yPos = 20; } yPos = addWrappedText(`• ${item}`, leftMargin + 5, yPos) + itemSpacing; }); } else { if (yPos > 270) { // Check before paragraph doc.addPage(); yPos = 20; } yPos = addWrappedText(String(content), leftMargin, yPos) + sectionSpacing; } yPos += sectionSpacing; } addSection("Dates of Operation:", currentMission.dates); addSection("Key Nations Involved:", currentMission.nations.join(', ')); addSection("Primary Intelligence Agencies:", currentMission.agencies.join(', ')); addSection("Objectives:", currentMission.objectives, true); addSection("Summary of Operation:", currentMission.summary); addSection("Outcome and Significance:", currentMission.outcome); if (currentMission.notable_figures) { addSection("Notable Figures:", currentMission.notable_figures); } doc.save(`${currentMission.name.replace(/[\s\W]+/g, '_')}_Debrief.pdf`); } catch (e) { console.error("Error during PDF generation: ", e); alert("An error occurred while generating the PDF. Please check the console for details."); } }); });