Other Monthly Debts:
Car Loans: ${dti_formatCurrency(inputs.carLoansMonthly)}
Student Loans: ${dti_formatCurrency(inputs.studentLoansMonthly)}
Credit Card Minimums: ${dti_formatCurrency(inputs.creditCardMinsMonthly)}
Other Debts: ${dti_formatCurrency(inputs.otherDebtsMonthly)}
`;
let resultsSummaryHtml = `
Gross Monthly Income: ${dti_formatCurrency(results.grossMonthlyIncome)}
Total Monthly Housing Payment: ${dti_formatCurrency(results.totalMonthlyHousingPayment)}
Total Other Monthly Debts: ${dti_formatCurrency(results.totalOtherMonthlyDebts)}
Total All Monthly Debt Payments: ${dti_formatCurrency(results.totalAllMonthlyDebtPayments)}
`;
const frontEndClassPdf = dti_getRatioClass(results.frontEndDti).replace('dti-', 'pdf-');
const backEndClassPdf = dti_getRatioClass(results.backEndDti).replace('dti-', 'pdf-');
const dtiRatiosHtml = `
Front-End DTI Ratio (Housing): ${dti_formatPercentage(results.frontEndDti)}
Back-End DTI Ratio (Total Debt): ${dti_formatPercentage(results.backEndDti)}
`;
const interpretationNote_pdf = document.querySelector('#dti_resultsSection .dti-interpretation-note').innerText.replace("Understanding Your DTI Ratios:", "").trim();
pdfContentEl.innerHTML = `
Loan-to-Income (DTI) Ratio Analysis
I. Input Summary
${inputsHtml}
II. Calculated Monthly Values
${resultsSummaryHtml}
III. Debt-to-Income Ratios
${dtiRatiosHtml}
Interpretation Guidelines:${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('DTI_Ratio_Analysis.pdf');
if(document.body.contains(pdfContentEl)) document.body.removeChild(pdfContentEl);
}).catch(err => {
console.error("DTI PDF Error:", err); alert("Error generating DTI PDF. See console.");
if(document.body.contains(pdfContentEl)) document.body.removeChild(pdfContentEl);
});
}
document.addEventListener('DOMContentLoaded', function() {
// Set default values for quicker testing
const annualIncomeEl = document.getElementById('dti_grossAnnualIncome');
if (annualIncomeEl && !annualIncomeEl.value) annualIncomeEl.value = '60000';
const piEl = document.getElementById('dti_p_i');
if (piEl && !piEl.value) piEl.value = '1000';
const taxesEl = document.getElementById('dti_propertyTaxes_monthly');
if (taxesEl && !taxesEl.value) taxesEl.value = '200';
const insuranceEl = document.getElementById('dti_homeInsurance_monthly');
if (insuranceEl && !insuranceEl.value) insuranceEl.value = '80';
if (!document.getElementById('dti_grossAnnualIncome')) {
console.error("Critical input 'dti_grossAnnualIncome' not found.");
}
});