Unsubsidized & PLUS Loans:
- Interest Accrued: $${dfccFormatCurrency(forbUnsubAccruedInterest)}
- Interest Capitalized (if not paid): $${dfccFormatCurrency(forbUnsubCapitalized)}
- New Unsubsidized/PLUS Loan Balance: $${dfccFormatCurrency(forbNewUnsubBalance)}
Totals for Forbearance:
- Total Interest Accrued (Borrower Responsibility): $${dfccFormatCurrency(forbTotalAccrued)}
- Total Interest Capitalized: $${dfccFormatCurrency(forbTotalCapitalized)}
- New Total Loan Balance: $${dfccFormatCurrency(forbNewTotalBalance)}
`;
resultsSectionEl.style.display = 'block';
resultsSectionEl.scrollIntoView({ behavior: 'smooth' });
}
function dfccDownloadPDF() {
if (typeof window.jspdf === 'undefined' || typeof window.jspdf.jsPDF !== 'function') {
alert('PDF library (jspdf) is not loaded correctly.'); return;
}
const JsPDFAPI = window.jspdf.jsPDF.API; // For AutoTable check
if (typeof JsPDFAPI?.autoTable !== 'function') {
console.warn('jsPDF-AutoTable plugin not found. Tables in PDF will be basic if used (current static table is simple).');
}
if (Object.keys(dfccCalcData).length === 0 || !dfccCalcData.inputs) {
alert("Please calculate the costs first before downloading the PDF.");
return;
}
const JsPDFConstructor = window.jspdf.jsPDF;
const pdf = new JsPDFConstructor('p', 'pt', 'a4');
let yPos = 40;
const pageMargin = 40;
const pageWidth = pdf.internal.pageSize.getWidth();
const contentWidth = pageWidth - (2 * pageMargin);
const lineH = 14; // Line height in points
const sectionGap = 10;
function addTitle(text) {
pdf.setFontSize(16); pdf.setFont("helvetica", "bold"); pdf.setTextColor(44, 62, 80);
pdf.text(text, pageWidth / 2, yPos, { align: 'center' });
yPos += 25;
}
function addSectionTitle(text) {
if (yPos > pdf.internal.pageSize.getHeight() - 70) { pdf.addPage(); yPos = pageMargin; }
pdf.setFontSize(13); pdf.setFont("helvetica", "bold"); pdf.setTextColor(52, 73, 94);
pdf.text(text, pageMargin, yPos);
yPos += lineH + 5;
pdf.setFont("helvetica", "normal"); pdf.setFontSize(10); pdf.setTextColor(51,51,51);
}
function addLinePdf(label, value, isCurrency = true, isBoldValue = false) {
if (yPos > pdf.internal.pageSize.getHeight() - 25) { pdf.addPage(); yPos = pageMargin; }
const valStr = isCurrency ? `$${dfccFormatCurrency(value)}` : String(value);
pdf.setFont("helvetica", "normal");
pdf.text(label + ":", pageMargin, yPos);
if(isBoldValue) pdf.setFont("helvetica", "bold");
pdf.text(valStr, pageMargin + 220, yPos); // Adjust X offset for value
if(isBoldValue) pdf.setFont("helvetica", "normal");
yPos += lineH;
}
function addSubSectionTitle(text) {
if (yPos > pdf.internal.pageSize.getHeight() - 40) { pdf.addPage(); yPos = pageMargin; }
pdf.setFontSize(10); pdf.setFont("helvetica", "bold"); pdf.setTextColor(44, 62, 80);
pdf.text(text, pageMargin, yPos);
yPos += lineH;
pdf.setFont("helvetica", "normal"); pdf.setFontSize(9); pdf.setTextColor(51,51,51);
}
addTitle("Deferment vs. Forbearance Cost Estimation");
addSectionTitle("Inputs Provided:");
addLinePdf("Subsidized Loan Balance", dfccCalcData.inputs.subBalance);
addLinePdf("Subsidized Loan Interest Rate", `${dfccCalcData.inputs.subRate.toFixed(2)}%`, false);
addLinePdf("Unsubsidized/PLUS Loan Balance", dfccCalcData.inputs.unsubPlusBalance);
addLinePdf("Unsubsidized/PLUS Loan Interest Rate", `${dfccCalcData.inputs.unsubPlusRate.toFixed(2)}%`, false);
addLinePdf("Duration of Period", `${dfccCalcData.inputs.periodMonths} months`, false);
addLinePdf("Paying Accrued Interest During Period", dfccCalcData.inputs.payInterestDuringPeriod === 'yes' ? "Yes" : "No", false);
yPos += sectionGap;
// Deferment Results
if(dfccCalcData.deferment){
addSectionTitle("Estimated Impact of DEFERMENT");
addSubSectionTitle("Subsidized Loans (Deferment):");
addLinePdf(" Interest Accrued (paid by gov't)", dfccCalcData.deferment.subAccrued);
addLinePdf(" Interest Capitalized", dfccCalcData.deferment.subCapitalized);
addLinePdf(" New Subsidized Balance", dfccCalcData.deferment.newSubBalance);
yPos += 5;
addSubSectionTitle("Unsubsidized & PLUS Loans (Deferment):");
addLinePdf(" Interest Accrued", dfccCalcData.deferment.unsubAccrued);
addLinePdf(" Interest Capitalized (if unpaid)", dfccCalcData.deferment.unsubCapitalized, true, true);
addLinePdf(" New Unsubsidized/PLUS Balance", dfccCalcData.deferment.newUnsubBalance);
yPos += 5;
addSubSectionTitle("Totals for Deferment:");
addLinePdf(" Total Interest Accrued (Borrower Responsibility)", dfccCalcData.deferment.unsubAccrued);
addLinePdf(" Total Interest Capitalized", dfccCalcData.deferment.totalCapitalized, true, true);
addLinePdf(" New Total Loan Balance", dfccCalcData.deferment.newTotalBalance, true, true);
yPos += sectionGap;
}
// Forbearance Results
if(dfccCalcData.forbearance){
addSectionTitle("Estimated Impact of FORBEARANCE");
addSubSectionTitle("Subsidized Loans (Forbearance):");
addLinePdf(" Interest Accrued", dfccCalcData.forbearance.subAccrued);
addLinePdf(" Interest Capitalized (if unpaid)", dfccCalcData.forbearance.subCapitalized, true, true);
addLinePdf(" New Subsidized Balance", dfccCalcData.forbearance.newSubBalance);
yPos += 5;
addSubSectionTitle("Unsubsidized & PLUS Loans (Forbearance):");
addLinePdf(" Interest Accrued", dfccCalcData.forbearance.unsubAccrued);
addLinePdf(" Interest Capitalized (if unpaid)", dfccCalcData.forbearance.unsubCapitalized, true, true);
addLinePdf(" New Unsubsidized/PLUS Balance", dfccCalcData.forbearance.newUnsubBalance);
yPos += 5;
addSubSectionTitle("Totals for Forbearance:");
addLinePdf(" Total Interest Accrued (Borrower Responsibility)", dfccCalcData.forbearance.totalAccrued);
addLinePdf(" Total Interest Capitalized", dfccCalcData.forbearance.totalCapitalized, true, true);
addLinePdf(" New Total Loan Balance", dfccCalcData.forbearance.newTotalBalance, true, true);
yPos += sectionGap;
}
// General Comparison Table to PDF
if (yPos > pdf.internal.pageSize.getHeight() - 150) { pdf.addPage(); yPos = pageMargin; }
addSectionTitle("General Differences Quick View");
if (typeof JsPDFAPI?.autoTable === 'function') {
pdf.autoTable({
head: [["Feature", "Deferment", "Forbearance"]],
body: [
["Interest on Subsidized Loans?", "Gov't typically pays (no accrual for borrower).", "Interest accrues (borrower's responsibility)."],
["Interest on Unsubsidized & PLUS Loans?", "Accrues (borrower's responsibility).", "Accrues (borrower's responsibility)."],
["Interest Capitalization?", "Accrued interest (Unsub/PLUS) typically capitalizes if unpaid.", "Accrued interest typically capitalizes if unpaid."],
["Eligibility Basis", "Specific criteria (hardship, in-school, etc.).", "General or mandatory; often more accessible."]
],
startY: yPos, theme: 'grid',
headStyles: { fillColor: [230, 230, 230], textColor: [50,50,50], fontStyle: 'bold', fontSize: 9 },
styles: { fontSize: 8, cellPadding: 3, overflow: 'linebreak' },
columnStyles: { 0: {fontStyle: 'bold', cellWidth: 120}, 1:{cellWidth: 'auto'}, 2:{cellWidth: 'auto'} },
margin: {left: pageMargin, right: pageMargin}
});
yPos = pdf.lastAutoTable.finalY + 20;
} else {
pdf.text("General differences table could not be rendered without AutoTable plugin.", pageMargin, yPos);
yPos += 20;
}
pdf.setFontSize(8); pdf.setTextColor(120);
if (yPos > pdf.internal.pageSize.getHeight() - 40) { pdf.addPage(); yPos = pageMargin; }
const disclaimer = "This is an estimate for illustrative purposes based on simple interest accrual before capitalization. Actual amounts may vary based on your loan servicer's calculation methods (e.g., daily interest compounding) and specific loan terms. Always consult your loan servicer or StudentAid.gov for official information.";
const splitDisclaimer = pdf.splitTextToSize(disclaimer, contentWidth);
pdf.text(splitDisclaimer, pageMargin, yPos);
pdf.save("Deferment_vs_Forbearance_Cost_Estimate.pdf");
}