`;
if (results.stressRate && results.stressRate.length > 0) {
contentHtml += `
III. Interest Rate Stress Analysis
`;
results.stressRate.forEach(sr => {
contentHtml += `
If Rate Increases by +${sr.increase.toFixed(2)}% (to ${sr.newRate.toFixed(2)}%):
New Monthly P&I: ${mpst_formatCurrency(sr.newPI)}
New Total PITI (+HOA): ${mpst_formatCurrency(sr.newPITI)}
Change in PITI: ${mpst_formatCurrency(sr.changeInPITI)} ${sr.changeInPITI > 0 ? '(Higher)' : ''}
New Front-End DTI: ${mpst_formatPercentage(sr.newFrontEndDTI)}
`;
});
}
if (results.stressIncome) {
const si = results.stressIncome;
contentHtml += `
IV. Income Shock Analysis (Income Decreases by ${inputs.incomeDecreasePercent}%):
New Gross Monthly Income: ${mpst_formatCurrency(si.newIncome)}
New Front-End DTI: ${mpst_formatPercentage(si.newFrontEndDTI)}
${inputs.otherMonthlyDebts > 0 ? `
New Back-End DTI: ${mpst_formatPercentage(si.newBackEndDTI)}
`: ''}
New Disposable Income: ${mpst_formatCurrency(si.newDisposableIncome)}
`;
}
if (results.stressPropCosts) {
const spc = results.stressPropCosts;
contentHtml += `
V. Property Cost Increase Analysis:
${inputs.taxIncrease > 0 ? `
Additional Annual Taxes: ${mpst_formatCurrency(inputs.taxIncrease)}
` : ''}
${inputs.insuranceIncrease > 0 ? `
Additional Annual Insurance: ${mpst_formatCurrency(inputs.insuranceIncrease)}
` : ''}
New Total PITI (+HOA): ${mpst_formatCurrency(spc.newPITI)}
New Front-End DTI: ${mpst_formatPercentage(spc.newFrontEndDTI)}
`;
}
if (results.stressOtherExp) {
const soe = results.stressOtherExp;
contentHtml += `
VI. Other Essential Expense Increase Analysis:
Increase in Other Monthly Expenses: ${mpst_formatCurrency(inputs.otherMonthlyExpenseIncrease)}
New Disposable Income: ${mpst_formatCurrency(soe.newDisposableIncome)}
`;
}
const disclaimer_pdf = document.querySelector('#mortgageStressTestTool .mpst-interpretation-note').innerText.replace("Understanding Your Stress Test Results (USA Context):", "").replace("Disclaimer:", "Further Notes:").trim();
pdfContentEl.innerHTML = `
${contentHtml}
Important Notes & Disclaimer:${disclaimer_pdf.replace(/\n/g, "
").replace(/
`;
document.body.appendChild(pdfContentEl);
html2canvas(pdfContentEl, { scale: 2, useCORS: true, logging:true, windowWidth: pdfContentEl.scrollWidth, windowHeight: pdfContentEl.scrollHeight }).then(canvas => {
const imgData = canvas.toDataURL('image/png');
const pdfWidth = pdf.internal.pageSize.getWidth();
const pdfHeight = pdf.internal.pageSize.getHeight();
let numPages = Math.ceil(canvas.height / ( (pdfHeight - 40) * (canvas.width / (pdfWidth - 40)) ) );
let pageCanvasHeight = (pdfHeight - 40) * (canvas.width / (pdfWidth - 40));
for (let i = 0; i < numPages; i++) {
if (i > 0) pdf.addPage();
let sourceY = i * pageCanvasHeight;
let sourceHeight = Math.min(pageCanvasHeight, canvas.height - sourceY);
const tempCanvas = document.createElement('canvas');
tempCanvas.width = canvas.width;
tempCanvas.height = sourceHeight;
const ctx = tempCanvas.getContext('2d');
ctx.drawImage(canvas, 0, sourceY, canvas.width, sourceHeight, 0, 0, canvas.width, sourceHeight);
const pageImgData = tempCanvas.toDataURL('image/png');
const pageImgHeight = (sourceHeight * (pdfWidth - 40)) / canvas.width;
pdf.addImage(pageImgData, 'PNG', 20, 20, pdfWidth - 40, pageImgHeight, undefined, 'FAST');
}
pdf.save('Mortgage_Stress_Test_Report.pdf');
if(document.body.contains(pdfContentEl)) document.body.removeChild(pdfContentEl);
}).catch(err => {
console.error("MPST PDF Error:", err); alert("Error generating PDF. See console.");
if(document.body.contains(pdfContentEl)) document.body.removeChild(pdfContentEl);
});
}
document.addEventListener('DOMContentLoaded', function() {
mpst_openTab({}, 'mpst_tab_base');
// Default some values for quicker testing
const principalEl = document.getElementById('mpst_loanPrincipal');
if (principalEl && !principalEl.value) principalEl.value = '250000';
const rateEl = document.getElementById('mpst_baseInterestRate');
if (rateEl && !rateEl.value) rateEl.value = '6.0';
const incomeEl = document.getElementById('mpst_grossMonthlyIncome');
if (incomeEl && !incomeEl.value) incomeEl.value = '7000';
const taxesEl = document.getElementById('mpst_annualPropertyTaxes');
if (taxesEl && !taxesEl.value) taxesEl.value = '3000';
const insuranceEl = document.getElementById('mpst_annualHomeInsurance');
if (insuranceEl && !insuranceEl.value) insuranceEl.value = '1000';
if (!document.getElementById('mpst_loanPrincipal')) {
console.error("Critical input 'mpst_loanPrincipal' not found.");
}
});