`;
const interpretationNote_pdf = document.querySelector('#mortgageDeferralCalculator .mpd-interpretation-note').innerText.replace("Understanding the Impact of Deferral (with Capitalization):", "").trim();
pdfContentEl.innerHTML = `
Mortgage Payment Deferral Impact Report
Estimates Only - Lender Terms Vary - USA Focus
I. Input Summary
${inputsHtml}
II. Estimated Impact of Deferral
${resultsHtml}
Understanding the Impact (from tool):${interpretationNote_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_Deferral_Impact.pdf');
if(document.body.contains(pdfContentEl)) document.body.removeChild(pdfContentEl);
}).catch(err => {
console.error("MPD PDF Error:", err); alert("Error generating PDF. See console.");
if(document.body.contains(pdfContentEl)) document.body.removeChild(pdfContentEl);
});
}
document.addEventListener('DOMContentLoaded', function() {
// Set default values for quicker testing
const principalEl = document.getElementById('mpd_currentPrincipal');
if (principalEl && !principalEl.value) principalEl.value = '200000';
const rateEl = document.getElementById('mpd_currentAnnualRate');
if (rateEl && !rateEl.value) rateEl.value = '6.5';
const termEl = document.getElementById('mpd_remainingLoanTermYears');
if (termEl && !termEl.value) termEl.value = '25';
const deferEl = document.getElementById('mpd_deferralMonths');
if (deferEl && !deferEl.value) deferEl.value = '6';
if (!document.getElementById('mpd_currentPrincipal')) {
console.error("Critical input 'mpd_currentPrincipal' not found.");
}
});