`).join('')}
`;
sortedResults.appendChild(folderEl);
});
};
// --- PDF Generation ---
downloadPdfButton.addEventListener('click', () => {
const { jsPDF } = window.jspdf;
const doc = new jsPDF({ orientation: 'p', unit: 'mm', format: 'a4' });
const margin = 15;
const pageWidth = doc.internal.pageSize.getWidth();
const usableWidth = pageWidth - (margin * 2);
let y = margin;
// Header
doc.setFont('helvetica', 'bold');
doc.setFontSize(20);
doc.text('Email Sorting System Report', pageWidth / 2, y, { align: 'center' });
y += 15;
// Rules Section
doc.setFontSize(14);
doc.text('Applied Sorting Rules', margin, y);
y += 8;
doc.setFont('helvetica', 'normal');
doc.setFontSize(10);
if (rules.length > 0) {
rules.forEach(rule => {
const ruleText = `• IF ${rule.condition} contains "${rule.value}" THEN move to ${rule.folder}`;
const splitText = doc.splitTextToSize(ruleText, usableWidth);
if (y + (splitText.length * 5) > doc.internal.pageSize.getHeight() - margin) {
doc.addPage(); y = margin;
}
doc.text(splitText, margin, y);
y += (splitText.length * 4) + 2;
});
} else {
doc.text('No rules were applied.', margin, y);
y += 6;
}
y += 5;
doc.setDrawColor('#E5E7EB').line(margin, y, pageWidth - margin, y);
y += 10;
// Results Section
const resultFolders = sortedResults.querySelectorAll('.p-4.border.rounded-lg');
resultFolders.forEach(folderNode => {
const folderTitle = folderNode.querySelector('h3').textContent.trim();
const emailNodes = folderNode.querySelectorAll('.p-3.bg-white');
if (y + 10 > doc.internal.pageSize.getHeight() - margin) {
doc.addPage(); y = margin;
}
doc.setFont('helvetica', 'bold');
doc.setFontSize(14);
doc.text(folderTitle, margin, y);
y += 8;
emailNodes.forEach(emailNode => {
const from = emailNode.querySelector('p:nth-child(1)').textContent;
const subject = emailNode.querySelector('p:nth-child(2)').textContent;
const emailText = ` - ${from}\n ${subject}`;
const splitEmailText = doc.splitTextToSize(emailText, usableWidth - 5);
if (y + (splitEmailText.length * 5) > doc.internal.pageSize.getHeight() - margin) {
doc.addPage(); y = margin;
}
doc.setFont('helvetica', 'normal');
doc.setFontSize(10);
doc.text(splitEmailText, margin + 5, y);
y += (splitEmailText.length * 4) + 2;
});
y += 5;
});
doc.save('Email-Sorting-Report.pdf');
});
// --- Initial Setup ---
showTab(0);
renderRules();
});
