`;
inputsHtml += `
Property Tax Details (Annual):
Property Taxes Paid: ${mtds_formatCurrency(inputs.propertyTaxesPaid)}
Illustrative SALT Cap Used: ${mtds_formatCurrency(inputs.saltCapUsed)}
`;
if (inputs.otherItemizedDeductions >= 0) { // Show even if 0 to confirm it was considered
inputsHtml += `
Other Itemized Deductions Entered:
Amount: ${mtds_formatCurrency(inputs.otherItemizedDeductions)}
`;
}
const resultsHtml = `
Est. Deductible Mortgage Interest: ${mtds_formatCurrency(results.deductibleMortgageInterest)}
Est. Deductible Property Taxes (SALT Context): ${mtds_formatCurrency(results.deductiblePropertyTaxes)}
Total Potential Homeownership Deductions: ${mtds_formatCurrency(results.totalHomeownershipDeductions)}
Estimated Annual Tax Savings from these Deductions: ${mtds_formatCurrency(results.estimatedTaxSavings)}
`;
let notesPdfHtml = "
" + 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('') + "
";
const disclaimer_pdf = `This estimator provides general information based on U.S. Federal tax rules for Tax Year ${MTDS_TAX_YEAR_CONTEXT}. Tax laws are complex and subject to annual changes by the IRS and Congress. This is NOT tax advice and does not cover state taxes. Always consult official IRS publications (e.g., Pub 936, Pub 17, current year's instructions) and a qualified tax professional for advice specific to your situation.`;
pdfContentEl.innerHTML = `
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.");
}
});