`;
let userResultsHtml = `
Illustrative Interest Rate for Your Score: ${mrics_formatPercentage(results.sampleRate)}
Estimated Monthly P&I: ${mrics_formatCurrency(results.monthlyPayment)}
Estimated Total Interest Paid: ${mrics_formatCurrency(results.totalInterest)}
Estimated Total Repayment: ${mrics_formatCurrency(results.totalPayment)}
`;
let comparisonTableHtml = `
| Credit Score Tier | Illustrative Rate | Est. Monthly P&I | Est. Total Interest | Difference vs. Your Score |
`;
comparison.forEach(tier => {
comparisonTableHtml += `
| ${tier.category} |
${mrics_formatPercentage(tier.sampleRate)} |
${mrics_formatCurrency(tier.monthlyPayment)} |
${mrics_formatCurrency(tier.totalInterest)} |
${tier.differenceVsUserText} |
`;
});
comparisonTableHtml += ``;
const disclaimer_pdf_subtitle = document.querySelector('#mortgageRateCreditScoreTool .mrics-tool-subtitle').innerText;
const disclaimer_pdf_main = document.querySelector('#mortgageRateCreditScoreTool .mrics-interpretation-note').innerText.replace("Understanding the Illustration:", "").replace("CRUCIAL DISCLAIMER:", "Further Notes:");
pdfContentEl.innerHTML = `
Lender Credit Score Impact on Mortgage Rates
${disclaimer_pdf_subtitle}
I. Your Input Summary
${inputsHtml}
II. Analysis for Your Estimated Credit Score
${userResultsHtml}
III. Comparative Scenario Analysis (Illustrative)
Important Disclaimers & Notes:${disclaimer_pdf_main.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_Rate_Credit_Score_Impact.pdf');
if(document.body.contains(pdfContentEl)) document.body.removeChild(pdfContentEl);
}).catch(err => {
console.error("MRICS PDF Error:", err); alert("Error generating PDF. See console.");
if(document.body.contains(pdfContentEl)) document.body.removeChild(pdfContentEl);
});
}
document.addEventListener('DOMContentLoaded', function() {
mrics_updateLoanTerm();
const scoreEl = document.getElementById('mrics_creditScore');
if(scoreEl && !scoreEl.value) scoreEl.value = '720';
const amountEl = document.getElementById('mrics_loanAmount');
if(amountEl && !amountEl.value) amountEl.value = '300000'; // Common mortgage amount
if (!document.getElementById('mrics_loanType')) {
console.error("Critical input 'mrics_loanType' not found.");
}
});