gymco / src /modules /reports /exportService.js
moustafa7's picture
Implement advanced gym SaaS phases
1615df8
Raw History Blame Contribute Delete
2.03 kB
import ExcelJS from 'exceljs';
import PDFDocument from 'pdfkit';
export async function exportExcel(rows, columns, sheetName = 'Report') {
const workbook = new ExcelJS.Workbook();
const sheet = workbook.addWorksheet(sheetName);
sheet.columns = columns.map((column) => ({ header: column.label, key: column.key, width: 22 }));
sheet.getRow(1).eachCell((cell) => {
cell.fill = { type: 'pattern', pattern: 'solid', fgColor: { argb: 'FF1B3A6B' } };
cell.font = { color: { argb: 'FFFFFFFF' }, bold: true };
cell.alignment = { horizontal: 'center' };
});
rows.forEach((row) => sheet.addRow(row));
sheet.eachRow((row, index) => {
if (index > 1 && index % 2 === 0) {
row.eachCell((cell) => {
cell.fill = { type: 'pattern', pattern: 'solid', fgColor: { argb: 'FFEFF6FF' } };
});
}
});
return workbook.xlsx.writeBuffer();
}
export async function exportPDF(rows, columns, title, gymName = 'GymCo') {
return new Promise((resolve) => {
const doc = new PDFDocument({ margin: 40, size: 'A4' });
const chunks = [];
doc.on('data', (chunk) => chunks.push(chunk));
doc.on('end', () => resolve(Buffer.concat(chunks)));
doc.fontSize(18).text(gymName, { align: 'center' });
doc.moveDown(0.5).fontSize(14).text(title, { align: 'center' });
doc.moveDown().fontSize(9).text(`Generated: ${new Date().toLocaleString()}`);
doc.moveDown();
const widths = columns.map(() => Math.floor(520 / columns.length));
columns.forEach((column, index) => doc.font('Helvetica-Bold').text(column.label, 40 + widths.slice(0, index).reduce((a, b) => a + b, 0), doc.y, { width: widths[index] }));
doc.moveDown();
rows.slice(0, 150).forEach((row) => {
const y = doc.y;
columns.forEach((column, index) => doc.font('Helvetica').fontSize(8).text(String(row[column.key] ?? ''), 40 + widths.slice(0, index).reduce((a, b) => a + b, 0), y, { width: widths[index] }));
doc.moveDown();
if (doc.y > 760) doc.addPage();
});
doc.end();
});
}