`;
const interestDiffValPdf = results.interestDifference;
const interestDiffTextPdf = `${bp_formatCurrency(Math.abs(interestDiffValPdf))} ${interestDiffValPdf > 0 ? "(Less with Balloon)" : (interestDiffValPdf < 0 ? "(More with Balloon)" : "(Same)")}`;
let interestDiffClassPdf = "";
if (interestDiffValPdf > 0) interestDiffClassPdf = "pdf-cost-savings";
else if (interestDiffValPdf < 0) interestDiffClassPdf = "pdf-cost-increase";
const resultsHtml = `
Loan & Payment Summary:
Calculated Monthly P&I (Initial Period): ${bp_formatCurrency(results.monthlyPI)}
At End of Balloon Term (${bp_formatYears(inputs.balloonTermYears)}):
Total Principal Paid (before balloon): ${bp_formatCurrency(results.principalPaidBeforeBalloon)}
Total Interest Paid (before balloon): ${bp_formatCurrency(results.interestPaidBeforeBalloon)}
Total Payments Made (before balloon): ${bp_formatCurrency(results.paymentsMadeBeforeBalloon)}
Balloon Payment Due: ${bp_formatCurrency(results.balloonPaymentDue)}
Overall Cost & Comparison:
Total Amount Paid (Balloon Scenario, Incl. Upfront): ${bp_formatCurrency(results.totalPaidWithBalloon)}
Total Interest Paid (Balloon Scenario): ${bp_formatCurrency(results.totalInterestPaidWithBalloonScenario)}
Comparison if fully amortized over ${bp_formatYears(inputs.amortizationPeriodYears)}:
Total Interest if Fully Amortized: ${bp_formatCurrency(results.totalInterestFullAmort)}
Difference in Total Interest Paid: ${interestDiffTextPdf}
`;
const interpretationNote_pdf = document.querySelector('#balloonMortgageAnalyzer .bp-interpretation-note').innerText.replace("Understanding Your Balloon Mortgage Estimate:", "").trim();
pdfContentEl.innerHTML = `
Home Loan Balloon Payment Analysis (USA Focus)
I. Input Summary
${inputsHtml}
II. Analysis Results
${resultsHtml}
Understanding Balloon Mortgages:${interpretationNote_pdf.replace(/\n/g, "
").replace(/
/g,"
`;
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('Balloon_Mortgage_Analysis.pdf');
if(document.body.contains(pdfContentEl)) document.body.removeChild(pdfContentEl);
}).catch(err => {
console.error("BPM PDF Error:", err); alert("Error generating PDF. See console.");
if(document.body.contains(pdfContentEl)) document.body.removeChild(pdfContentEl);
});
}
document.addEventListener('DOMContentLoaded', function() {
// Default some values for quicker testing
const principalEl = document.getElementById('bp_loanPrincipal');
if (principalEl && !principalEl.value) principalEl.value = '200000';
const rateEl = document.getElementById('bp_annualInterestRate');
if (rateEl && !rateEl.value) rateEl.value = '6.0';
const amortTermEl = document.getElementById('bp_amortizationPeriodYears');
if (amortTermEl && !amortTermEl.value) amortTermEl.value = '30';
const balloonTermEl = document.getElementById('bp_balloonTermYears');
if (balloonTermEl && !balloonTermEl.value) balloonTermEl.value = '7';
if (!document.getElementById('bp_loanPrincipal')) {
console.error("Critical input 'bp_loanPrincipal' not found.");
}
});