File size: 2,025 Bytes
1615df8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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();
  });
}