`;
breakdownDiv.innerHTML = `
Estimated Monthly Property Costs
Principal & Interest (P&I): ${cpla_formatCurrency(data.monthlyPI)}
Property Taxes (Monthly): ${cpla_formatCurrency(data.monthlyTaxes)}
Property Insurance (Monthly): ${cpla_formatCurrency(data.monthlyInsurance)}
Other Expenses (Monthly): ${cpla_formatCurrency(data.monthlyOtherPropEx)}
Total Estimated Monthly Outlay: ${cpla_formatCurrency(data.totalMonthlyOutlay)}
`;
}
function cpla_generatePDF() {
if (typeof window.jspdf === 'undefined' || typeof window.jspdf.jsPDF === 'undefined') {
alert('PDF generation library (jsPDF) is not loaded.');
console.error('CPLA Error: window.jspdf or window.jspdf.jsPDF is not defined.');
document.getElementById('cpla-tab2-error').textContent = 'Error: PDF library (jsPDF) not loaded.';
return;
}
const JSPDF_CONSTRUCTOR = window.jspdf.jsPDF;
const tempDoc = new JSPDF_CONSTRUCTOR();
if (typeof tempDoc.autoTable !== 'function') {
alert('jsPDF AutoTable plugin is not loaded.');
console.error('CPLA Error: autoTable function not found on jsPDF instance.');
document.getElementById('cpla-tab2-error').textContent = 'Error: PDF AutoTable plugin not loaded.';
return;
}
if (Object.keys(cpla_affordabilityData).length === 0 || cpla_affordabilityData.error) {
alert('Please calculate valid affordability details first or correct inputs.');
document.getElementById('cpla-tab2-error').textContent = 'Calculate valid results first to generate PDF.';
return;
}
document.getElementById('cpla-tab2-error').textContent = '';
const doc = new JSPDF_CONSTRUCTOR();
const data = cpla_affordabilityData;
const cellColor = '#007bff';
const textColor = '#ffffff';
const headingColor = '#0056b3';
let yPos = 15;
doc.setFontSize(18);
doc.setTextColor(headingColor);
doc.text("Commercial Property Loan Affordability Report", 105, yPos, { align: 'center' });
yPos += 12;
// Inputs Section
doc.setFontSize(14);
doc.setTextColor(headingColor);
doc.text("Your Inputs", 14, yPos);
yPos += 7;
doc.setFontSize(10);
const inputDataForPdf = [
["Annual Gross Income:", cpla_formatCurrency(data.annualGrossIncome)],
["Annual Operating Expenses (excl. prop specific):", cpla_formatCurrency(data.annualOpEx)],
["Annual Property Taxes:", cpla_formatCurrency(data.annualTaxes)],
["Annual Property Insurance:", cpla_formatCurrency(data.annualInsurance)],
["Other Annual Property Expenses:", cpla_formatCurrency(data.annualOtherPropEx)],
["Down Payment Amount:", cpla_formatCurrency(data.downPayment)],
["Annual Interest Rate:", `${data.annualInterestRate.toFixed(2)}%`],
["Loan Term (Years):", `${data.loanTermYears} years`],
["Desired DSCR:", `${data.dscr.toFixed(2)}x`]
];
doc.autoTable({
startY: yPos,
body: inputDataForPdf,
theme: 'grid',
styles: { fontSize: 9, cellPadding: 2 },
headStyles: { fillColor: cellColor, textColor: textColor, fontStyle: 'bold' },
columnStyles: { 0: { fontStyle: 'bold' } }
});
yPos = doc.lastAutoTable.finalY + 10;
// Affordability Summary Section
doc.setFontSize(14);
doc.setTextColor(headingColor);
doc.text("Affordability Summary", 14, yPos);
yPos += 7;
const summaryDataForPdf = [
["Net Operating Income (NOI):", cpla_formatCurrency(data.netOperatingIncome)],
["Max. Affordable Annual Debt Service:", cpla_formatCurrency(data.maxAnnualDebtService)],
["Max. Affordable Monthly P&I Payment:", cpla_formatCurrency(data.maxMonthlyDebtService)],
["Estimated Maximum Loan Amount:", cpla_formatCurrency(data.maxLoanAmount)],
[{content: "Estimated Affordable Property Price:", styles: {fontStyle: 'bold', fontSize: 11, halign: 'center'}}, {content: cpla_formatCurrency(data.affordablePropertyPrice), styles: {fontStyle: 'bold', fontSize: 11, halign: 'center', textColor: [40, 167, 69]}}], // Green highlight
];
doc.autoTable({
startY: yPos,
body: summaryDataForPdf,
theme: 'grid',
styles: { fontSize: 9, cellPadding: 2 },
columnStyles: { 0: { fontStyle: 'bold'} },
didParseCell: function (hookData) {
if (hookData.row.index === 4) { // Special styling for the 'Estimated Affordable Property Price' row
hookData.cell.styles.fontStyle = 'bold';
hookData.cell.styles.fontSize = 11;
hookData.cell.styles.halign = 'center';
if(hookData.column.index === 1) hookData.cell.styles.textColor = [40, 167, 69]; // Green
}
}
});
yPos = doc.lastAutoTable.finalY + 10;
// Monthly Breakdown Section
doc.setFontSize(14);
doc.setTextColor(headingColor);
doc.text("Estimated Monthly Property Costs", 14, yPos);
yPos += 7;
const breakdownDataForPdf = [
["Principal & Interest (P&I):", cpla_formatCurrency(data.monthlyPI)],
["Property Taxes (Monthly):", cpla_formatCurrency(data.monthlyTaxes)],
["Property Insurance (Monthly):", cpla_formatCurrency(data.monthlyInsurance)],
["Other Property Expenses (Monthly):", cpla_formatCurrency(data.monthlyOtherPropEx)],
[{content: "Total Estimated Monthly Outlay:", styles: {fontStyle: 'bold'}}, {content: cpla_formatCurrency(data.totalMonthlyOutlay), styles: {fontStyle: 'bold'}}],
];
doc.autoTable({
startY: yPos,
body: breakdownDataForPdf,
theme: 'grid',
styles: { fontSize: 9, cellPadding: 2 },
columnStyles: { 0: { fontStyle: 'bold'} },
didParseCell: function (hookData) {
if (hookData.row.index === 4) { // Total row
hookData.cell.styles.fontStyle = 'bold';
hookData.cell.styles.fillColor = [233, 236, 239]; // Light gray background like .cpla-result-item
}
}
});
doc.save("Commercial_Property_Loan_Affordability_Report.pdf");
}