${affordabilityGuidanceText}
`;
}
cblr_navigateToTab(2); // Show results tab
}
function cblr_generatePDF() {
if (typeof window.jspdf === 'undefined' || typeof window.jspdf.jsPDF === 'undefined') {
alert('PDF generation library (jsPDF) is not loaded.'); return;
}
const JSPDF_CONSTRUCTOR = window.jspdf.jsPDF;
const tempDoc = new JSPDF_CONSTRUCTOR();
if (typeof tempDoc.autoTable !== 'function') {
alert('jsPDF AutoTable plugin is not loaded.'); return;
}
if (Object.keys(cblr_dataForPdf).length === 0 || !cblr_dataForPdf.monthlyLoanPayment === undefined) {
alert('Please complete the analysis first to generate a PDF report.'); return;
}
const doc = new JSPDF_CONSTRUCTOR({ orientation: 'p' });
const data = cblr_dataForPdf;
let yPos = 15;
const pageMargin = 15;
const pageWidth = doc.internal.pageSize.getWidth();
const usableWidth = pageWidth - (2 * pageMargin);
const primaryColor = [23, 162, 184]; // #17a2b8 Teal
const headingColor = [17, 122, 139]; // #117a8b Darker Teal
const textColor = [52, 58, 64]; // #343a40
function addMainHeader(text) {
doc.setFontSize(16); doc.setFont(undefined, 'bold');
doc.setTextColor(headingColor[0], headingColor[1], headingColor[2]);
doc.text(text, pageWidth / 2, yPos, { align: 'center' });
yPos += 12;
}
function addSectionHeader(text) {
if (yPos > doc.internal.pageSize.getHeight() - 30) { doc.addPage(); yPos = pageMargin; }
doc.setFontSize(12); doc.setFont(undefined, 'bold');
doc.setTextColor(primaryColor[0], primaryColor[1], primaryColor[2]);
doc.text(text, pageMargin, yPos);
yPos += 8; doc.setFont(undefined, 'normal');
}
function addLineItem(label, value, valueIsBold = false, labelIsBold = false) {
if (yPos > doc.internal.pageSize.getHeight() - 12) { doc.addPage(); yPos = pageMargin; }
doc.setFontSize(9.5);
doc.setTextColor(textColor[0],textColor[1],textColor[2]);
if(labelIsBold) doc.setFont(undefined, 'bold');
doc.text(label, pageMargin, yPos);
doc.setFont(undefined, valueIsBold ? 'bold' : 'normal');
doc.text(value, pageWidth - pageMargin, yPos, {align: 'right'});
doc.setFont(undefined, 'normal'); // Reset
yPos += 6.5;
}
function addParagraph(text, fontSize = 9.5, options = {}) {
doc.setFontSize(fontSize);
doc.setTextColor(options.color ? options.color[0] : textColor[0],
options.color ? options.color[1] : textColor[1],
options.color ? options.color[2] : textColor[2]);
if (options.fontStyle) doc.setFont(undefined, options.fontStyle);
const lines = doc.splitTextToSize(text, usableWidth - (options.indent || 0));
if (yPos + (lines.length * (fontSize * 0.4)) + (options.spacingAfter || 3) > doc.internal.pageSize.getHeight() - 20) {
doc.addPage(); yPos = pageMargin;
}
doc.text(lines, pageMargin + (options.indent || 0), yPos);
yPos += (lines.length * (fontSize * 0.4)) + (options.spacingAfter || 3);
if (options.fontStyle) doc.setFont(undefined, 'normal'); // Reset font style
}
addMainHeader("Career-Based Loan Repayment Analysis");
addSectionHeader("Your Inputs:");
const inputsForPdf = [
["Intended Career:", data.careerName],
["Estimated Annual Gross Salary:", cblr_formatCurrency(data.annualSalary)],
["Est. % for Taxes & Deductions:", `${data.taxDeductionPercentage.toFixed(1)}%`],
["Total Estimated Student Loan Debt:", cblr_formatCurrency(data.totalDebt)],
["Average Loan Interest Rate:", `${data.averageRate.toFixed(2)}%`],
["Loan Repayment Term:", `${data.repaymentTermYears} years`],
];
doc.autoTable({
startY: yPos, body: inputsForPdf, theme: 'grid',
styles: { fontSize: 9, cellPadding: 2.5, textColor: textColor },
columnStyles: { 0: { fontStyle: 'bold', cellWidth: usableWidth * 0.5 }, 1 : { halign: 'right', cellWidth: usableWidth * 0.5} },
didDrawPage: function (hookData){ yPos = hookData.cursor.y ? hookData.cursor.y +10 : pageMargin; }
});
yPos = doc.lastAutoTable.finalY ? doc.lastAutoTable.finalY + 10 : yPos;
if (yPos > doc.internal.pageSize.getHeight() - 80) { doc.addPage(); yPos = pageMargin; }
addSectionHeader("Estimated Repayment & Affordability:");
addLineItem("Est. Monthly Gross Income:", cblr_formatCurrency(data.monthlyGrossIncome));
addLineItem("Est. Monthly Taxes & Deductions:", cblr_formatCurrency(data.estimatedMonthlyTaxesDeductions));
addLineItem("Est. Monthly Take-Home Pay:", cblr_formatCurrency(data.estimatedMonthlyTakeHomePay), true, true);
yPos += 2; // Extra space
addLineItem("Est. Monthly Student Loan Payment:", cblr_formatCurrency(data.monthlyLoanPayment), true, true);
yPos += 2;
addLineItem("Loan Payment as % of Gross Income:", `${data.paymentAsPercentOfGross.toFixed(1)}%`);
addLineItem("Loan Payment as % of Take-Home Pay:", `${isFinite(data.paymentAsPercentOfTakeHome) ? data.paymentAsPercentOfTakeHome.toFixed(1) + '%' : 'N/A'}`, true, true);
yPos += 5;
// Affordability Guidance
if (yPos > doc.internal.pageSize.getHeight() - 40) { doc.addPage(); yPos = pageMargin; }
addSectionHeader("Affordability Guidance:", 6);
let guidanceColor = textColor;
if(data.guidanceClass === "cblr-guidance-high") guidanceColor = [114, 28, 36]; // Red
else if(data.guidanceClass === "cblr-guidance-moderate") guidanceColor = [154,52,18]; // Orange
else if(data.guidanceClass === "cblr-guidance-medium") guidanceColor = [133,100,4]; // Yellow
else if(data.guidanceClass === "cblr-guidance-low") guidanceColor = [21,87,36]; // Green
// Remove HTML tags from guidance text for PDF
const plainGuidanceText = data.affordabilityGuidanceText.replace(/<[^>]*>/g, "");
addParagraph(plainGuidanceText, 9.5, {color: guidanceColor, spacingAfter: 10});
// Strategies & Resources (abbreviated)
if (yPos > doc.internal.pageSize.getHeight() - 50) { doc.addPage(); yPos = pageMargin; }
addSectionHeader("Key Strategies & Resources (U.S. Focused):", 6);
const strategiesForPdf = [
"Research salaries thoroughly (BLS.gov OOH).",
"Minimize borrowing; prioritize free aid.",
"Understand your loans (StudentAid.gov for federal loans).",
"Explore Income-Driven Repayment (IDR) plans for federal loans if payments are high.",
"Create a post-graduation budget.",
"Consider loan forgiveness programs if eligible (PSLF, TLF)."
];
strategiesForPdf.forEach(s => addParagraph("• " + s, 9, {indent: 2, spacingAfter:1}));
yPos += 5;
// Disclaimer
if (yPos > doc.internal.pageSize.getHeight() - 35) { doc.addPage(); yPos = pageMargin; }
const disclaimerText = document.querySelector('#cblr-container .cblr-disclaimer-main p').textContent;
doc.setFontSize(8);
doc.setTextColor(133, 100, 4); // #856404 (text color from HTML disclaimer)
const lines = doc.splitTextToSize("Disclaimer: " + disclaimerText, usableWidth);
doc.setFillColor(255,243,205); // #fff3cd (bg from HTML disclaimer)
doc.rect(pageMargin - 2, yPos -2, usableWidth + 4, (lines.length * 3.5) + 4, 'F');
doc.text(lines, pageMargin, yPos);
doc.save("Career_Loan_Repayment_Estimate.pdf");
}
