${data.statement_of_need.replace(/\n/g, '
')}
Project Description
${data.project_description.replace(/\n/g, '
')}
Budget Narrative
${data.budget_narrative.replace(/\n/g, '
')}
Evaluation Plan
${data.evaluation_plan.replace(/\n/g, '
')}
`;
}
function generatePdf() {
if (!window.jspdf || !latestGeneratedData) {
alert("Please generate a proposal before downloading.");
return;
}
const { jsPDF } = window.jspdf;
const doc = new jsPDF({ orientation: 'portrait', unit: 'pt', format: 'letter' });
const pageHeight = doc.internal.pageSize.getHeight();
const pageWidth = doc.internal.pageSize.getWidth();
const margin = 72;
let cursorY = 0;
let pageCount = 1;
const addHeaderFooter = () => {
doc.setFont("Helvetica", "normal");
doc.setFontSize(8);
doc.setTextColor(150);
doc.text(document.getElementById('projectTitle').value, margin, pageHeight - 40);
doc.text(`Page ${pageCount}`, pageWidth - margin, pageHeight - 40, { align: 'right' });
};
// --- Cover Page ---
doc.setDrawColor(204, 251, 241); // Teal-100
doc.setLineWidth(10);
doc.rect(0, 0, pageWidth, pageHeight);
doc.setFont("Lora", "bold");
doc.setFontSize(26);
doc.setTextColor(15, 74, 68); // Teal-900
const titleLines = doc.splitTextToSize(document.getElementById('projectTitle').value, pageWidth - margin*4);
doc.text(titleLines, pageWidth / 2, pageHeight / 2 - 60, { align: 'center' });
doc.setFont("Helvetica", "normal");
doc.setFontSize(12);
doc.setTextColor(17, 94, 89); // Teal-800
doc.text(`A Grant Proposal Submitted By:`, pageWidth / 2, pageHeight / 2, { align: 'center' });
doc.setFontSize(16);
doc.text(document.getElementById('orgName').value, pageWidth / 2, pageHeight / 2 + 20, { align: 'center' });
doc.setFontSize(10);
doc.text(new Date().toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' }), pageWidth / 2, pageHeight / 2 + 50, { align: 'center' });
// --- Proposal Content ---
doc.addPage();
addHeaderFooter();
cursorY = margin;
const drawSection = (title, content) => {
if (!content) return;
doc.setFont("Lora", "bold");
doc.setFontSize(14);
doc.setTextColor(19, 78, 74); // Teal-800
const titleLines = doc.splitTextToSize(title, pageWidth - margin*2);
if (cursorY + (titleLines.length * 16) + 20 > pageHeight - 60) { doc.addPage(); pageCount++; addHeaderFooter(); cursorY = margin; }
doc.text(title, margin, cursorY);
cursorY += titleLines.length * 16 + 5;
doc.setFont("Helvetica", "normal");
doc.setFontSize(11);
doc.setTextColor(51, 65, 85);
const textLines = doc.splitTextToSize(content, pageWidth - margin * 2);
if (cursorY + (textLines.length * 12 * 1.5) > pageHeight - 60) { doc.addPage(); pageCount++; addHeaderFooter(); cursorY = margin; }
doc.text(textLines, margin, cursorY, { lineHeightFactor: 1.5 });
cursorY += textLines.length * 12 * 1.5 + 25;
};
drawSection("Executive Summary", latestGeneratedData.executive_summary);
drawSection("Statement of Need", latestGeneratedData.statement_of_need);
drawSection("Project Description", latestGeneratedData.project_description);
drawSection("Budget Narrative", latestGeneratedData.budget_narrative);
drawSection("Evaluation Plan", latestGeneratedData.evaluation_plan);
const safeFileName = (document.getElementById('projectTitle').value || 'grant_proposal').replace(/[^a-z0-9]/gi, '_').toLowerCase();
doc.save(`${safeFileName}.pdf`);
}
// Initial setup
showTab(1);
createDynamicField(budgetContainer, 'Item (e.g., Supplies)', 'Cost ($)', ()=>{});
createDynamicField(budgetContainer, 'Item (e.g., Staff Time)', 'Cost ($)', ()=>{});
});