`;
if (fspvpl_domElements.pdfDownloadBtn) {
fspvpl_domElements.pdfDownloadBtn.classList.remove('fspvpl-hidden');
}
}
function fspvpl_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 fspvpl_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."); return;
}
if (!fspvpl_validateTab1() || !fspvpl_validateTab2() || !fspvpl_validateTab3()) {
alert("Please ensure all inputs are correctly filled before generating PDF.");
if (!fspvpl_validateTab1()) fspvpl_navigateToTab('fspvpl-tab1');
else if (!fspvpl_validateTab2()) fspvpl_navigateToTab('fspvpl-tab2');
else if (!fspvpl_validateTab3()) fspvpl_navigateToTab('fspvpl-tab3');
return;
}
fspvpl_calculateAndDisplayComparison();
if (fspvpl_domElements.resultsContainer && fspvpl_domElements.resultsContainer.innerHTML.includes("fspvpl-error-message")) {
alert("Cannot generate PDF due to errors in input or calculation."); return;
}
const ActualJsPDF = window.jspdf.jsPDF;
const doc = new ActualJsPDF();
const { loanAmount, loanTermYears, federalLoan, privateLoan } = fspvpl_loanData;
const fed = federalLoan; const pvt = privateLoan; // Shorthands
doc.setFontSize(18);
const primaryColorRGB = fspvpl_hexToRgb(getComputedStyle(document.documentElement).getPropertyValue('--fspvpl-primary-color').trim()) || {r:63,g:81,b:181};
const secondaryColorRGB = fspvpl_hexToRgb(getComputedStyle(document.documentElement).getPropertyValue('--fspvpl-secondary-color').trim()) || {r:48,g:63,b:159};
doc.setTextColor(primaryColorRGB.r, primaryColorRGB.g, primaryColorRGB.b);
doc.text("Federal vs. Private Student Loan Comparison", 105, 20, null, null, "center");
doc.setFontSize(11);
doc.setTextColor(33,33,33);
let startY = 30;
doc.text(`Loan Amount: ${fspvpl_formatCurrency(loanAmount)} | Loan Term: ${loanTermYears} years`, 14, startY);
startY += 10;
const tableHeadFillColor = [secondaryColorRGB.r, secondaryColorRGB.g, secondaryColorRGB.b];
const federalColorArr = fspvpl_hexToRgb(getComputedStyle(document.documentElement).getPropertyValue('--fspvpl-federal-color').trim()) || {r:0,g:123,b:255};
const privateColorArr = fspvpl_hexToRgb(getComputedStyle(document.documentElement).getPropertyValue('--fspvpl-private-color').trim()) || {r:255,g:152,b:0};
const tableBody = [
["Loan Type", fed.typeText || 'N/A', "N/A (General Private)"],
["Est. Annual Interest Rate", `${fspvpl_formatPercent(fed.rate)} (Fixed)`, `${fspvpl_formatPercent(pvt.rate)} (${pvt.rateTypeText || 'N/A'})`],
["Total Loan Fees", fspvpl_formatCurrency(fed.totalFees), fspvpl_formatCurrency(pvt.totalFees)],
["Est. Monthly Payment (EMI)", fspvpl_formatCurrency(fed.calculated.emi), fspvpl_formatCurrency(pvt.calculated.emi)],
["Total Interest Paid", fspvpl_formatCurrency(fed.calculated.totalInterest), fspvpl_formatCurrency(pvt.calculated.totalInterest)],
["Total Finance Charge", fspvpl_formatCurrency(fed.calculated.totalFinanceCharge), fspvpl_formatCurrency(pvt.calculated.totalFinanceCharge)],
[{content:"Total Amount Repaid", styles:{fontStyle:'bold'}},
{content:fspvpl_formatCurrency(fed.calculated.totalRepayment), styles:{fontStyle:'bold'}},
{content:fspvpl_formatCurrency(pvt.calculated.totalRepayment), styles:{fontStyle:'bold'}}]
];
doc.autoTable({
startY: startY,
head: [['Feature', 'Federal Student Loan', 'Private Student Loan']],
body: tableBody,
theme: 'grid',
headStyles: { fillColor: tableHeadFillColor, textColor: 255, fontSize:10, fontStyle:'bold' },
styles: { fontSize: 9, cellPadding: 2.5 },
margin: { left: 14, right: 14 },
columnStyles: {
0: { fontStyle: 'bold', cellWidth: 55 },
1: { textColor: [federalColorArr.r, federalColorArr.g, federalColorArr.b], cellWidth: 'auto' },
2: { textColor: [privateColorArr.r, privateColorArr.g, privateColorArr.b], cellWidth: 'auto' }
}
});
startY = doc.autoTable.previous.finalY + 12;
doc.setFontSize(11);
doc.setTextColor(secondaryColorRGB.r, secondaryColorRGB.g, secondaryColorRGB.b);
doc.text("Key Differences to Consider:", 14, startY); startY += 7;
doc.setFontSize(8.5);
doc.setTextColor(33,33,33);
const keyDifferencesPDF = [
"- Repayment Protections: Federal loans offer IDR plans, deferment, forbearance, and potential forgiveness. Private loans offer fewer protections.",
"- Interest Rates: Federal rates are fixed. Private rates can be fixed or variable (variable rates may increase) and are credit-based.",
"- Credit Requirements: Most federal student loans for students do not require a credit check. Federal PLUS and private loans do.",
"- Tax Benefits: Interest on qualified student loans (both federal and private) may be tax-deductible.",
"- Discharge Options: Federal loans offer discharge for death/disability. Private loan options vary."
];
keyDifferencesPDF.forEach(item => {
const splitItem = doc.splitTextToSize(item, doc.internal.pageSize.width - 28);
doc.text(splitItem, 14, startY);
startY += (splitItem.length * (doc.getLineHeight() / doc.internal.scaleFactor * 0.8)) + 1.5;
});
startY += 5;
doc.setFontSize(10);
doc.setTextColor(secondaryColorRGB.r, secondaryColorRGB.g, secondaryColorRGB.b);
doc.text("Guidance:", 14, startY); startY += 6;
doc.setFontSize(8.5);
doc.setTextColor(33,33,33);
const guidanceTextPDF = "Federal student loans generally provide more borrower benefits and protections. It's often recommended to exhaust federal loan options (especially Direct Subsidized/Unsubsidized loans) before considering private loans. Compare all loan terms carefully.";
const splitGuidance = doc.splitTextToSize(guidanceTextPDF, doc.internal.pageSize.width - 28);
doc.text(splitGuidance, 14, startY);
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("Federal_vs_Private_Loan_Analyzer.pdf");
}
window.fspvpl_switchTab = fspvpl_switchTab;
window.fspvpl_navigateToTab = fspvpl_navigateToTab;
window.fspvpl_generatePdf = fspvpl_generatePdf;
window.fspvpl_updateTotalFederalFeesDisplay = fspvpl_updateTotalFederalFeesDisplay;
window.fspvpl_updateTotalPrivateFeesDisplay = fspvpl_updateTotalPrivateFeesDisplay;