Estimated Student Loan Interest Deduction: ${ustd_formatCurrency(results.studentLoanInterestDeduction)}
`;
let notesPdfHtml = "
" + notes.map(note => {
let className = "";
if (note.includes("phased out") || note.includes("exceeds the acquisition debt limit")) className = "pdf-warning";
return `- ${note.replace(/|<\/strong>/g, "")}
`;
}).join('') + "
";
const disclaimer_pdf = `This estimator provides general information based on U.S. Federal tax rules for a recent tax year (e.g., 2023/2024 rules for limits/phase-outs). 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 970) and a qualified tax professional for advice specific to your situation and the current tax year.`;
pdfContentEl.innerHTML = `
Loan Tax Deduction Estimator (U.S. Federal Focus)
Estimates Only - Not Tax Advice - Consult IRS Pubs & Tax Professional
I. Your Input Summary
${inputsHtml}
II. Estimated Potential Deductions
${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_Loan_Tax_Deduction_Estimate.pdf');
if(document.body.contains(pdfContentEl)) document.body.removeChild(pdfContentEl);
}).catch(err => {
console.error("USTD 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 magiEl = document.getElementById('ustd_magi');
if (magiEl && !magiEl.value) magiEl.value = '70000';
const mortIntEl = document.getElementById('ustd_mortgageInterestPaid');
if (mortIntEl && !mortIntEl.value) mortIntEl.value = '10000';
const mortDebtEl = document.getElementById('ustd_totalMortgageDebt');
if (mortDebtEl && !mortDebtEl.value) mortDebtEl.value = '250000';
const studIntEl = document.getElementById('ustd_studentLoanInterestPaid');
if (studIntEl && !studIntEl.value) studIntEl.value = '1500';
if (!document.getElementById('ustd_filingStatus')) {
console.error("Critical input 'ustd_filingStatus' not found.");
}
});