- " + notes.map(note => {
let cleanNote = note.replace(/|<\/strong>/g, "");
if (note.includes("exceeds the illustrative debt limit") || note.includes("SALT deduction") || note.includes("must exceed your Standard Deduction")) {
cleanNote = `${cleanNote}`;
}
return `
- ${cleanNote} `; }).join('') + "
`;
let notesPdfHtml = "
Mortgage Tax Deduction Savings Estimator (U.S. Federal)
Estimates Based on Illustrative ${MTDS_TAX_YEAR_CONTEXT} Rules - Not Tax Advice
I. Your Input Summary
${inputsHtml}
II. Estimated Potential Deductions & Savings
${resultsHtml}
III. Important Notes & Considerations
${notesPdfHtml}
Overall Disclaimer: ${disclaimer_pdf}
`;
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('US_Mortgage_Tax_Savings_Estimate.pdf');
if(document.body.contains(pdfContentEl)) document.body.removeChild(pdfContentEl);
}).catch(err => {
console.error("MTDS 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 filingStatusEl = document.getElementById('mtds_filingStatus');
if (filingStatusEl) filingStatusEl.value = 'mfj';
const marginalRateEl = document.getElementById('mtds_marginalTaxRate');
if (marginalRateEl && !marginalRateEl.value) marginalRateEl.value = '22';
const mortIntEl = document.getElementById('mtds_mortgageInterestPaid');
if (mortIntEl && !mortIntEl.value) mortIntEl.value = '12000';
const mortDebtEl = document.getElementById('mtds_totalMortgageDebt');
if (mortDebtEl && !mortDebtEl.value) mortDebtEl.value = '250000';
const propTaxEl = document.getElementById('mtds_propertyTaxesPaid');
if (propTaxEl && !propTaxEl.value) propTaxEl.value = '4000';
if (!document.getElementById('mtds_filingStatus')) {
console.error("Critical input 'mtds_filingStatus' not found.");
}
});
