Student Loan EMI Calculator
Enter Your Loan Information
EMI & Loan Summary
Please enter your loan details in Tab 1 to calculate your EMI.
Please correct the errors in Tab 1 and try again.
';
slec_domElements.pdfDownloadBtn.classList.add('slec-hidden');
return;
}
const P = slec_loanData.principal;
const annualRate = slec_loanData.annualRate;
const tenureYears = slec_loanData.tenureYears;
const monthlyRate = (annualRate / 100) / 12;
const numberOfMonths = tenureYears * 12;
let emi = 0;
if (monthlyRate === 0) { // 0% interest rate
emi = P / numberOfMonths;
} else {
const powerTerm = Math.pow(1 + monthlyRate, numberOfMonths);
emi = (P * monthlyRate * powerTerm) / (powerTerm - 1);
}
if (!isFinite(emi) || isNaN(emi) || emi < 0) emi = 0; // Handle potential calculation issues
const totalAmountPayable = emi * numberOfMonths;
const totalInterestPayable = totalAmountPayable - P;
// Store for PDF
slec_loanData.emi = emi;
slec_loanData.totalInterestPayable = totalInterestPayable > 0 ? totalInterestPayable : 0; // Ensure non-negative
slec_loanData.totalAmountPayable = totalAmountPayable > P ? totalAmountPayable : P; // Ensure at least principal
slec_domElements.resultsContainer.innerHTML = `
EMI Calculation Results
Monthly EMI: ${slec_formatCurrency(slec_loanData.emi)}
Total Interest Payable: ${slec_formatCurrency(slec_loanData.totalInterestPayable)}
Total Amount Payable (Principal + Interest): ${slec_formatCurrency(slec_loanData.totalAmountPayable)}
`;
slec_domElements.pdfDownloadBtn.classList.remove('slec-hidden');
}
function slec_hexToRgb(hex) {
if (!hex || typeof hex !== 'string') return null;
const shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
hex = hex.replace(shorthandRegex, function(m, r, g, b) { return r + r + g + g + b + b; });
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? { r: parseInt(result[1], 16), g: parseInt(result[2], 16), b: parseInt(result[3], 16) } : null;
}
function slec_generatePdf() {
if (typeof window.jspdf === 'undefined' || typeof window.jspdf.jsPDF === 'undefined' || typeof window.jspdf.jsPDF.API === 'undefined' || typeof window.jspdf.jsPDF.API.autoTable === 'undefined') {
alert("PDF generation library is not loaded. Please try again later."); return;
}
// Ensure data is calculated and valid
if (!slec_validateInputs()) { // This also populates slec_loanData if valid
alert("Please ensure all inputs are correctly filled before generating PDF.");
slec_navigateToTab('slec-tab1'); // Navigate to input tab
return;
}
// Re-calculate to ensure slec_loanData is fresh for PDF if user somehow got to PDF without fresh calc
slec_calculateAndDisplayEMI();
if (slec_domElements.resultsContainer.innerHTML.includes("slec-error-message")) {
alert("Cannot generate PDF due to errors in input or calculation. Please correct them.");
return;
}
const ActualJsPDF = window.jspdf.jsPDF;
const doc = new ActualJsPDF();
const { principal, annualRate, tenureYears, emi, totalInterestPayable, totalAmountPayable } = slec_loanData;
const numberOfMonths = tenureYears * 12;
doc.setFontSize(18);
const primaryColorRGB = slec_hexToRgb(getComputedStyle(document.documentElement).getPropertyValue('--slec-primary-color').trim());
const secondaryColorRGB = slec_hexToRgb(getComputedStyle(document.documentElement).getPropertyValue('--slec-secondary-color').trim());
const accentColorRGB = slec_hexToRgb(getComputedStyle(document.documentElement).getPropertyValue('--slec-accent-color').trim());
if (primaryColorRGB) doc.setTextColor(primaryColorRGB.r, primaryColorRGB.g, primaryColorRGB.b); else doc.setTextColor(0,123,255);
doc.text("Student Loan EMI Calculation Summary", 105, 22, null, null, "center");
doc.setFontSize(12);
doc.setTextColor(52, 58, 64); // --slec-text-color
let startY = 35;
const tableHeadFillColor = secondaryColorRGB ? [secondaryColorRGB.r, secondaryColorRGB.g, secondaryColorRGB.b] : [0,86,179];
doc.setFontSize(14);
if (secondaryColorRGB) doc.setTextColor(secondaryColorRGB.r, secondaryColorRGB.g, secondaryColorRGB.b); else doc.setTextColor(0,86,179);
doc.text("Loan Input Parameters", 14, startY); startY += 8;
doc.autoTable({
startY: startY, theme: 'grid', headStyles: { fillColor: tableHeadFillColor, textColor: 255 },
body: [
["Loan Principal Amount:", slec_formatCurrency(principal)],
["Annual Interest Rate:", slec_formatPercent(annualRate)],
["Loan Tenure:", `${tenureYears} years (${numberOfMonths} months)`],
], margin: { left: 14, right: 14 },
styles: { fontSize: 10 }, headStyles: { fontSize: 11 }, bodyStyles: { cellPadding: 2.5 }
});
startY = doc.autoTable.previous.finalY + 12;
doc.setFontSize(14);
if (secondaryColorRGB) doc.setTextColor(secondaryColorRGB.r, secondaryColorRGB.g, secondaryColorRGB.b); else doc.setTextColor(0,86,179);
doc.text("EMI & Loan Summary", 14, startY); startY += 8;
doc.autoTable({
startY: startY, theme: 'grid', headStyles: { fillColor: tableHeadFillColor, textColor: 255 },
body: [
[{content: "Calculated Monthly EMI:", styles: {fontStyle: 'bold'}},
{content: slec_formatCurrency(emi), styles: {fontStyle: 'bold', fontSize: 11, textColor: accentColorRGB ? [accentColorRGB.r, accentColorRGB.g, accentColorRGB.b] : [40,167,69] }}],
["Total Interest Payable:", slec_formatCurrency(totalInterestPayable)],
["Total Amount Payable (Principal + Interest):", slec_formatCurrency(totalAmountPayable)],
], margin: { left: 14, right: 14 },
styles: { fontSize: 10 }, headStyles: { fontSize: 11 }, bodyStyles: { cellPadding: 2.5 }
});
startY = doc.autoTable.previous.finalY + 15;
doc.setFontSize(9);
doc.setTextColor(120);
doc.text(`Report generated on: ${new Date().toLocaleDateString()} ${new Date().toLocaleTimeString()}`, 14, doc.internal.pageSize.height - 10);
doc.save("Student_Loan_EMI_Summary.pdf");
}
window.slec_switchTab = slec_switchTab;
window.slec_navigateToTab = slec_navigateToTab;
window.slec_generatePdf = slec_generatePdf;