@harbortouch/skytab-analytics-report-utils 0.16.1 → 0.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -41,6 +41,155 @@ import {
41
41
  } from '@harbortouch/skytab-analytics-report-utils';
42
42
  ```
43
43
 
44
+ ### Exporting report data to PDF
45
+
46
+ Import from the `/pdf` sub-path. This keeps `pdfkit` out of frontend bundles — it is only included when this import is actually used.
47
+
48
+ ```typescript
49
+ import { PdfExporter } from '@harbortouch/skytab-analytics-report-utils/pdf';
50
+
51
+ const exporter = new PdfExporter();
52
+
53
+ const buffer = await exporter.generate({
54
+ pages: [
55
+ {
56
+ filters: [
57
+ { label: 'Date Range', value: 'Jan 1 – Jan 31' },
58
+ { label: 'Location', value: 'Downtown' },
59
+ ],
60
+ table: {
61
+ columns: [
62
+ { header: 'Item', key: 'name' },
63
+ { header: 'Net Sales', key: 'netSales' },
64
+ { header: 'Qty', key: 'qty' },
65
+ ],
66
+ rows: reportRows,
67
+ },
68
+ },
69
+ ],
70
+ });
71
+
72
+ // buffer is a Node.js Buffer containing the PDF binary
73
+ fs.writeFileSync('report.pdf', buffer);
74
+ ```
75
+
76
+ `PdfExporter.generate()` returns a `Promise<Buffer>`. Each entry in `pages` produces one PDF page (additional pages are added automatically when table rows overflow).
77
+
78
+ **`PdfPage`**
79
+
80
+ | Field | Type | Description |
81
+ | --------- | ------------- | ---------------------------------------------- |
82
+ | `filters` | `PdfFilter[]` | Optional applied-filters block above the table |
83
+ | `table` | `PdfTable` | Optional table to render on the page |
84
+
85
+ **`PdfTable`**
86
+
87
+ | Field | Type | Description |
88
+ | --------- | --------------------------- | ---------------------------------------- |
89
+ | `columns` | `PdfTableColumn[]` | Column definitions (order preserved) |
90
+ | `rows` | `Record<string, unknown>[]` | Row data — keyed by `PdfTableColumn.key` |
91
+
92
+ **`PdfTableColumn`**
93
+
94
+ | Field | Type | Description |
95
+ | -------- | -------- | ------------------------------------------------------- |
96
+ | `header` | `string` | Column header label |
97
+ | `key` | `string` | Field name used to look up the value in each row object |
98
+ | `width` | `number` | Optional hint — auto-sized from content if omitted |
99
+
100
+ **Behaviour notes**
101
+
102
+ - Output is A4 landscape with 30 pt margins.
103
+ - Column widths are auto-calculated from header and cell content; columns are scaled down proportionally if they exceed the usable page width.
104
+ - Font size scales with column count (7–11 pt) so wide tables remain readable.
105
+ - Numeric cell values are right-aligned; text values are left-aligned.
106
+ - Alternating rows use a light grey background (`#F5F5F5`); the header row uses `#D3D3D3`.
107
+ - Emojis are stripped from all text automatically (pdfkit's built-in fonts do not support them).
108
+ - When a table spans multiple pages the header row is repeated at the top of each new page.
109
+
110
+ > **Note:** `pdfkit` is a dependency of this library — consuming projects do not need to install it separately.
111
+
112
+ ---
113
+
114
+ ### Exporting report data to Excel
115
+
116
+ Import from the `/excel` sub-path. This keeps `exceljs` out of frontend bundles — it is only included when this import is actually used.
117
+
118
+ ```typescript
119
+ import { ExcelExporter } from '@harbortouch/skytab-analytics-report-utils/excel';
120
+
121
+ const exporter = new ExcelExporter();
122
+
123
+ const buffer = await exporter.generate({
124
+ sheets: [
125
+ {
126
+ sheetName: 'Sales Summary',
127
+ filters: [
128
+ { label: 'Date Range', value: 'Jan 1 – Jan 31' },
129
+ { label: 'Location', value: 'Downtown' },
130
+ ],
131
+ table: {
132
+ columns: [
133
+ { header: 'Item', key: 'name' },
134
+ { header: 'Net Sales', key: 'netSales' },
135
+ { header: 'Qty', key: 'qty' },
136
+ ],
137
+ rows: reportRows,
138
+ },
139
+ },
140
+ ],
141
+ });
142
+
143
+ // buffer is a Node.js Buffer containing the .xlsx binary
144
+ fs.writeFileSync('report.xlsx', buffer);
145
+ ```
146
+
147
+ `ExcelExporter.generate()` returns a `Promise<Buffer>`. Each entry in `sheets` produces one worksheet tab in the workbook.
148
+
149
+ **`ExcelSheet`**
150
+
151
+ | Field | Type | Description |
152
+ | ----------- | --------------- | ------------------------------------------------------- |
153
+ | `sheetName` | `string` | Optional worksheet name (defaults to `'Report'`) |
154
+ | `filters` | `ExcelFilter[]` | Optional applied-filters block rendered above the table |
155
+ | `table` | `ExcelTable` | Optional table to render on the sheet |
156
+
157
+ **`ExcelTable`**
158
+
159
+ | Field | Type | Description |
160
+ | --------- | --------------------------- | ------------------------------------------ |
161
+ | `columns` | `ExcelTableColumn[]` | Column definitions (order preserved) |
162
+ | `rows` | `Record<string, unknown>[]` | Row data — keyed by `ExcelTableColumn.key` |
163
+
164
+ **`ExcelTableColumn`**
165
+
166
+ | Field | Type | Description |
167
+ | -------- | -------- | ------------------------------------------------------- |
168
+ | `header` | `string` | Column header label |
169
+ | `key` | `string` | Field name used to look up the value in each row object |
170
+ | `width` | `number` | Optional hint — auto-sized from content if omitted |
171
+
172
+ **`ExcelFilter`**
173
+
174
+ | Field | Type | Description |
175
+ | ------- | -------- | ------------ |
176
+ | `label` | `string` | Filter label |
177
+ | `value` | `string` | Filter value |
178
+
179
+ **Behaviour notes**
180
+
181
+ - Each `ExcelSheet` entry produces one worksheet tab in the workbook.
182
+ - Applied filters are rendered as `Label: value` rows above the table, separated from it by a blank row.
183
+ - The header row is bold with a grey fill (`#D3D3D3`) and has Excel auto-filter enabled.
184
+ - Alternating data rows use a light grey background (`#F5F5F5`).
185
+ - Numeric cell values are right-aligned; all other values are left-aligned.
186
+ - Column widths are auto-calculated from header and cell content; the optional `width` field overrides the auto-calculation for a specific column.
187
+ - `null` and `undefined` cell values are rendered as empty cells.
188
+
189
+ > **Note:** `exceljs` is a dependency of this library — consuming projects do not need to install it separately.
190
+
191
+ ---
192
+
44
193
  ### Exporting report data to CSV
45
194
 
46
195
  Import from the `/csv` sub-path. This keeps `csv-stringify` out of frontend bundles — it is only included when this import is actually used.
@@ -91,8 +240,15 @@ src/
91
240
  ├── types.ts — Core interfaces (FieldType, ColumnPresentationConfig, ReportConfig, …)
92
241
  ├── columns/
93
242
  │ └── index.ts — ReportColumnKey union type + COLUMN_REGISTRY (i18n keys)
94
- ├── csv/
95
- │ └── index.ts — CsvExporter — published as the ./csv sub-path export
243
+ ├── exporters/
244
+ │ ├── types.ts — Shared ExportTableColumn / ExportTable / ExportFilter interfaces
245
+ │ ├── csv/
246
+ │ │ └── index.ts — CsvExporter — published as the ./csv sub-path export
247
+ │ ├── excel/
248
+ │ │ └── index.ts — ExcelExporter — published as the ./excel sub-path export
249
+ │ └── pdf/
250
+ │ ├── index.ts — PdfExporter — published as the ./pdf sub-path export
251
+ │ └── pdf.utils.ts — stripEmojis() helper
96
252
  ├── reports/
97
253
  │ ├── salesSummary.ts
98
254
  │ ├── ticketSummary.ts
@@ -17,7 +17,7 @@ var __copyProps = (to, from, except, desc) => {
17
17
  };
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
 
20
- // src/csv/index.ts
20
+ // src/exporters/csv/index.ts
21
21
  var csv_exports = {};
22
22
  __export(csv_exports, {
23
23
  CsvExporter: () => CsvExporter
@@ -1,4 +1,4 @@
1
- // src/csv/index.ts
1
+ // src/exporters/csv/index.ts
2
2
  import { stringify } from "csv-stringify/sync";
3
3
  var CsvExporter = class {
4
4
  constructor(options = {}) {
@@ -0,0 +1,101 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/exporters/excel/index.ts
31
+ var excel_exports = {};
32
+ __export(excel_exports, {
33
+ ExcelExporter: () => ExcelExporter
34
+ });
35
+ module.exports = __toCommonJS(excel_exports);
36
+ var import_exceljs = __toESM(require("exceljs"), 1);
37
+ var GRAY_FILL = {
38
+ type: "pattern",
39
+ pattern: "solid",
40
+ fgColor: { argb: "FFD9D9D9" }
41
+ };
42
+ var toCell = (value) => {
43
+ if (value === null || value === void 0) {
44
+ return null;
45
+ }
46
+ if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
47
+ return value;
48
+ }
49
+ return null;
50
+ };
51
+ var ExcelExporter = class {
52
+ async generate(content) {
53
+ const workbook = new import_exceljs.default.Workbook();
54
+ const usedNames = /* @__PURE__ */ new Set();
55
+ for (const section of content.sections) {
56
+ let sheetName = section.sheetName.slice(0, 31);
57
+ if (usedNames.has(sheetName.toLowerCase())) {
58
+ let suffix = 2;
59
+ let candidate = `${sheetName.slice(0, 28)}_${suffix}`;
60
+ while (usedNames.has(candidate.toLowerCase())) {
61
+ suffix++;
62
+ candidate = `${sheetName.slice(0, 28)}_${suffix}`;
63
+ }
64
+ sheetName = candidate;
65
+ }
66
+ usedNames.add(sheetName.toLowerCase());
67
+ const sheet = workbook.addWorksheet(sheetName);
68
+ if (section.filters && section.filters.length > 0) {
69
+ const titleRow = sheet.addRow(["Applied Filters"]);
70
+ titleRow.getCell(1).font = { bold: true };
71
+ titleRow.getCell(1).fill = GRAY_FILL;
72
+ for (const filter of section.filters) {
73
+ const row = sheet.addRow([filter.label, filter.value]);
74
+ row.getCell(1).font = { bold: true };
75
+ }
76
+ sheet.addRow([]);
77
+ }
78
+ if (section.table) {
79
+ const headerRow = sheet.addRow(section.table.columns.map((col) => col.header));
80
+ headerRow.font = { bold: true };
81
+ headerRow.eachCell((cell) => {
82
+ cell.fill = GRAY_FILL;
83
+ });
84
+ section.table.columns.forEach((col, i) => {
85
+ if (col.width) {
86
+ sheet.getColumn(i + 1).width = col.width / 7;
87
+ }
88
+ });
89
+ for (const row of section.table.rows) {
90
+ sheet.addRow(section.table.columns.map((col) => toCell(row[col.key])));
91
+ }
92
+ }
93
+ }
94
+ const buf = await workbook.xlsx.writeBuffer();
95
+ return Buffer.from(buf);
96
+ }
97
+ };
98
+ // Annotate the CommonJS export names for ESM import in node:
99
+ 0 && (module.exports = {
100
+ ExcelExporter
101
+ });
@@ -0,0 +1,15 @@
1
+ import { E as ExportTable, a as ExportFilter } from '../../types-BzLsgnfc.cjs';
2
+ export { b as XlsxTableColumn } from '../../types-BzLsgnfc.cjs';
3
+
4
+ interface XlsxDocumentSection {
5
+ sheetName: string;
6
+ table?: ExportTable;
7
+ filters?: ExportFilter[];
8
+ }
9
+ declare class ExcelExporter {
10
+ generate(content: {
11
+ sections: XlsxDocumentSection[];
12
+ }): Promise<Buffer>;
13
+ }
14
+
15
+ export { ExcelExporter, type XlsxDocumentSection, ExportFilter as XlsxFilter, ExportTable as XlsxTable };
@@ -0,0 +1,15 @@
1
+ import { E as ExportTable, a as ExportFilter } from '../../types-BzLsgnfc.js';
2
+ export { b as XlsxTableColumn } from '../../types-BzLsgnfc.js';
3
+
4
+ interface XlsxDocumentSection {
5
+ sheetName: string;
6
+ table?: ExportTable;
7
+ filters?: ExportFilter[];
8
+ }
9
+ declare class ExcelExporter {
10
+ generate(content: {
11
+ sections: XlsxDocumentSection[];
12
+ }): Promise<Buffer>;
13
+ }
14
+
15
+ export { ExcelExporter, type XlsxDocumentSection, ExportFilter as XlsxFilter, ExportTable as XlsxTable };
@@ -0,0 +1,66 @@
1
+ // src/exporters/excel/index.ts
2
+ import ExcelJS from "exceljs";
3
+ var GRAY_FILL = {
4
+ type: "pattern",
5
+ pattern: "solid",
6
+ fgColor: { argb: "FFD9D9D9" }
7
+ };
8
+ var toCell = (value) => {
9
+ if (value === null || value === void 0) {
10
+ return null;
11
+ }
12
+ if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
13
+ return value;
14
+ }
15
+ return null;
16
+ };
17
+ var ExcelExporter = class {
18
+ async generate(content) {
19
+ const workbook = new ExcelJS.Workbook();
20
+ const usedNames = /* @__PURE__ */ new Set();
21
+ for (const section of content.sections) {
22
+ let sheetName = section.sheetName.slice(0, 31);
23
+ if (usedNames.has(sheetName.toLowerCase())) {
24
+ let suffix = 2;
25
+ let candidate = `${sheetName.slice(0, 28)}_${suffix}`;
26
+ while (usedNames.has(candidate.toLowerCase())) {
27
+ suffix++;
28
+ candidate = `${sheetName.slice(0, 28)}_${suffix}`;
29
+ }
30
+ sheetName = candidate;
31
+ }
32
+ usedNames.add(sheetName.toLowerCase());
33
+ const sheet = workbook.addWorksheet(sheetName);
34
+ if (section.filters && section.filters.length > 0) {
35
+ const titleRow = sheet.addRow(["Applied Filters"]);
36
+ titleRow.getCell(1).font = { bold: true };
37
+ titleRow.getCell(1).fill = GRAY_FILL;
38
+ for (const filter of section.filters) {
39
+ const row = sheet.addRow([filter.label, filter.value]);
40
+ row.getCell(1).font = { bold: true };
41
+ }
42
+ sheet.addRow([]);
43
+ }
44
+ if (section.table) {
45
+ const headerRow = sheet.addRow(section.table.columns.map((col) => col.header));
46
+ headerRow.font = { bold: true };
47
+ headerRow.eachCell((cell) => {
48
+ cell.fill = GRAY_FILL;
49
+ });
50
+ section.table.columns.forEach((col, i) => {
51
+ if (col.width) {
52
+ sheet.getColumn(i + 1).width = col.width / 7;
53
+ }
54
+ });
55
+ for (const row of section.table.rows) {
56
+ sheet.addRow(section.table.columns.map((col) => toCell(row[col.key])));
57
+ }
58
+ }
59
+ }
60
+ const buf = await workbook.xlsx.writeBuffer();
61
+ return Buffer.from(buf);
62
+ }
63
+ };
64
+ export {
65
+ ExcelExporter
66
+ };
@@ -0,0 +1,235 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/exporters/pdf/index.ts
31
+ var pdf_exports = {};
32
+ __export(pdf_exports, {
33
+ PdfExporter: () => PdfExporter
34
+ });
35
+ module.exports = __toCommonJS(pdf_exports);
36
+ var import_pdfkit = __toESM(require("pdfkit"), 1);
37
+
38
+ // src/exporters/pdf/pdf.utils.ts
39
+ var stripEmojis = (text) => {
40
+ if (!text) {
41
+ return text;
42
+ }
43
+ return text.replace(
44
+ /[\u{1F300}-\u{1F9FF}]|[\u{2600}-\u{26FF}]|[\u{2700}-\u{27BF}]|[\u{1F1E0}-\u{1F1FF}]|[\u{1F600}-\u{1F64F}]|[\u{1F680}-\u{1F6FF}]|[\u{1F900}-\u{1F9FF}]|[\u{1FA00}-\u{1FA6F}]|[\u{1FA70}-\u{1FAFF}]|[\u{FE00}-\u{FE0F}]|[\u{1F004}]|[\u{1F0CF}]|[\u{1F18E}]|[\u{1F191}-\u{1F19A}]|[\u{1F1E6}-\u{1F1FF}]|[\u{1F201}-\u{1F251}]/gu,
45
+ ""
46
+ ).replace(/\s+/g, " ").trim();
47
+ };
48
+
49
+ // src/exporters/pdf/index.ts
50
+ var FONT_BOLD = "Helvetica-Bold";
51
+ var FONT_REGULAR = "Helvetica";
52
+ var COLOR_BLACK = "#000000";
53
+ var COLOR_HEADER_BACKGROUND = "#D3D3D3";
54
+ var COLOR_ROW_BACKGROUND_ALTERNATE = "#F5F5F5";
55
+ var PdfExporter = class {
56
+ generate(content) {
57
+ const doc = new import_pdfkit.default({ margin: 30, size: "A4", layout: "landscape" });
58
+ const buffers = [];
59
+ return new Promise((resolve, reject) => {
60
+ doc.on("data", (chunk) => buffers.push(chunk));
61
+ doc.on("end", () => resolve(Buffer.concat(buffers)));
62
+ doc.on("error", reject);
63
+ try {
64
+ content.sections.forEach((section, index) => {
65
+ const sanitizedSections = this.sanitizeSections(section);
66
+ if (index > 0) {
67
+ doc.addPage();
68
+ }
69
+ let currentY = doc.y;
70
+ if (sanitizedSections.filters && sanitizedSections.filters.length > 0) {
71
+ currentY = this.drawFilters(doc, sanitizedSections.filters);
72
+ }
73
+ if (sanitizedSections.table) {
74
+ this.drawTable(doc, sanitizedSections.table, currentY);
75
+ }
76
+ });
77
+ doc.end();
78
+ } catch (err) {
79
+ reject(err instanceof Error ? err : new Error(String(err)));
80
+ }
81
+ });
82
+ }
83
+ sanitizeSections(sections) {
84
+ const sanitized = {};
85
+ if (sections.filters) {
86
+ sanitized.filters = sections.filters.map((filter) => ({
87
+ label: stripEmojis(filter.label),
88
+ value: stripEmojis(filter.value)
89
+ }));
90
+ }
91
+ if (sections.table) {
92
+ sanitized.table = {
93
+ columns: sections.table.columns.map((col) => ({
94
+ ...col,
95
+ header: stripEmojis(col.header)
96
+ })),
97
+ rows: sections.table.rows.map((row) => {
98
+ const sanitizedRow = {};
99
+ for (const [key, value] of Object.entries(row)) {
100
+ sanitizedRow[key] = typeof value === "string" ? stripEmojis(value) : value;
101
+ }
102
+ return sanitizedRow;
103
+ })
104
+ };
105
+ }
106
+ return sanitized;
107
+ }
108
+ drawFilters(doc, filters) {
109
+ doc.fillColor(COLOR_BLACK).font(FONT_BOLD).fontSize(11).text("Applied Filters", { align: "left" });
110
+ doc.moveDown(0.5);
111
+ filters.forEach((filter) => {
112
+ doc.font(FONT_BOLD).fontSize(11).text(filter.label, { continued: true });
113
+ doc.font(FONT_REGULAR).text(`: ${filter.value}`);
114
+ });
115
+ doc.moveDown(1);
116
+ return doc.y;
117
+ }
118
+ drawTable(doc, table, startY = doc.y) {
119
+ const columns = table.columns.length > 0 ? table.columns.map((c) => ({ ...c })) : [{ header: "No data", key: "no_data", width: 100 }];
120
+ const rows = table.rows ?? [];
121
+ const baseCellPadding = 4;
122
+ const minColumnWidth = 35;
123
+ const columnCount = columns.length;
124
+ const headerFontSize = columnCount > 13 ? 8 : columnCount > 10 ? 9 : columnCount > 7 ? 10 : 11;
125
+ const dataFontSize = columnCount > 13 ? 7 : columnCount > 10 ? 8 : columnCount > 7 ? 9 : 10;
126
+ columns.forEach((col) => {
127
+ doc.font(FONT_BOLD).fontSize(headerFontSize);
128
+ let maxWidth = doc.widthOfString(col.header);
129
+ doc.font(FONT_REGULAR).fontSize(dataFontSize);
130
+ rows.forEach((row) => {
131
+ const rv = row[col.key];
132
+ const cellValue = typeof rv === "string" || typeof rv === "number" || typeof rv === "boolean" ? String(rv) : "";
133
+ const textWidth = doc.widthOfString(cellValue);
134
+ if (textWidth > maxWidth) {
135
+ maxWidth = textWidth;
136
+ }
137
+ });
138
+ col.width = Math.max(maxWidth + baseCellPadding * 2, minColumnWidth);
139
+ });
140
+ const usableWidth = doc.page.width - doc.page.margins.left - doc.page.margins.right;
141
+ const totalColumnWidth = columns.reduce((sum, c) => sum + (c.width ?? minColumnWidth), 0);
142
+ if (totalColumnWidth > usableWidth) {
143
+ const scale = usableWidth / totalColumnWidth;
144
+ columns.forEach((c) => {
145
+ c.width = Math.max(Math.floor((c.width ?? minColumnWidth) * scale), minColumnWidth);
146
+ });
147
+ }
148
+ const tableWidth = columns.reduce((sum, c) => sum + (c.width ?? minColumnWidth), 0);
149
+ const pageBottom = doc.page.height - doc.page.margins.bottom;
150
+ const calculateRowHeight = (row, isHeader = false) => {
151
+ const fontSize = isHeader ? headerFontSize : dataFontSize;
152
+ doc.font(isHeader ? FONT_BOLD : FONT_REGULAR).fontSize(fontSize);
153
+ let maxLines = 1;
154
+ columns.forEach((col) => {
155
+ const cellValue = isHeader ? col.header : row?.[col.key] !== void 0 ? String(row[col.key]) : "";
156
+ const textWidth = doc.widthOfString(cellValue);
157
+ const availableWidth = (col.width ?? minColumnWidth) - baseCellPadding * 2;
158
+ const lines = Math.ceil(textWidth / availableWidth);
159
+ if (lines > maxLines) {
160
+ maxLines = lines;
161
+ }
162
+ });
163
+ return Math.max(maxLines * (fontSize + 2) + baseCellPadding * 2, 25);
164
+ };
165
+ const headerHeight = calculateRowHeight(null, true);
166
+ const drawTableHeader = (y) => this.drawHeader(doc, columns, y, tableWidth, headerHeight, baseCellPadding, headerFontSize, minColumnWidth);
167
+ let currentY = drawTableHeader(startY);
168
+ doc.font(FONT_REGULAR).fontSize(dataFontSize);
169
+ rows.forEach((row, rowIndex) => {
170
+ const currentRowHeight = calculateRowHeight(row, false);
171
+ if (currentY + currentRowHeight > pageBottom) {
172
+ doc.addPage();
173
+ currentY = drawTableHeader(doc.page.margins.top);
174
+ doc.font(FONT_REGULAR).fontSize(dataFontSize);
175
+ }
176
+ currentY = this.drawRow(
177
+ doc,
178
+ row,
179
+ rowIndex,
180
+ columns,
181
+ currentY,
182
+ tableWidth,
183
+ currentRowHeight,
184
+ baseCellPadding,
185
+ dataFontSize,
186
+ minColumnWidth
187
+ );
188
+ });
189
+ }
190
+ drawHeader(doc, columns, startY, tableWidth, headerHeight, cellPadding, headerFontSize, minColumnWidth) {
191
+ doc.strokeColor(COLOR_BLACK).lineWidth(0.5).moveTo(doc.page.margins.left, startY).lineTo(doc.page.margins.left + tableWidth, startY).stroke();
192
+ doc.fillColor(COLOR_HEADER_BACKGROUND).rect(doc.page.margins.left, startY, tableWidth, headerHeight).fill();
193
+ let xPosition = doc.page.margins.left;
194
+ doc.fillColor(COLOR_BLACK).font(FONT_BOLD).fontSize(headerFontSize);
195
+ columns.forEach((col) => {
196
+ doc.strokeColor(COLOR_BLACK).lineWidth(0.5).moveTo(xPosition, startY).lineTo(xPosition, startY + headerHeight).stroke();
197
+ doc.text(col.header, xPosition + cellPadding, startY + cellPadding, {
198
+ width: (col.width ?? minColumnWidth) - cellPadding * 2,
199
+ align: "left",
200
+ lineBreak: false
201
+ });
202
+ xPosition += col.width ?? minColumnWidth;
203
+ });
204
+ doc.moveTo(xPosition, startY).lineTo(xPosition, startY + headerHeight).stroke();
205
+ doc.moveTo(doc.page.margins.left, startY + headerHeight).lineTo(xPosition, startY + headerHeight).stroke();
206
+ return startY + headerHeight;
207
+ }
208
+ drawRow(doc, row, rowIndex, columns, currentY, tableWidth, rowHeight, cellPadding, dataFontSize, minColumnWidth) {
209
+ doc.font(FONT_REGULAR).fontSize(dataFontSize);
210
+ if (rowIndex % 2 === 0) {
211
+ doc.fillColor(COLOR_ROW_BACKGROUND_ALTERNATE).rect(doc.page.margins.left, currentY, tableWidth, rowHeight).fill();
212
+ }
213
+ let xPosition = doc.page.margins.left;
214
+ doc.fillColor(COLOR_BLACK);
215
+ columns.forEach((col) => {
216
+ doc.strokeColor(COLOR_BLACK).lineWidth(0.5).moveTo(xPosition, currentY).lineTo(xPosition, currentY + rowHeight).stroke();
217
+ const rawValue = row[col.key];
218
+ const cellValue = typeof rawValue === "string" || typeof rawValue === "number" || typeof rawValue === "boolean" ? String(rawValue) : "";
219
+ const isNumeric = typeof rawValue === "number" || typeof rawValue === "string" && rawValue !== "" && !isNaN(Number(rawValue));
220
+ doc.text(cellValue, xPosition + cellPadding, currentY + cellPadding, {
221
+ width: (col.width ?? minColumnWidth) - cellPadding * 2,
222
+ align: isNumeric ? "right" : "left",
223
+ lineBreak: false
224
+ });
225
+ xPosition += col.width ?? minColumnWidth;
226
+ });
227
+ doc.moveTo(xPosition, currentY).lineTo(xPosition, currentY + rowHeight).stroke();
228
+ doc.moveTo(doc.page.margins.left, currentY + rowHeight).lineTo(xPosition, currentY + rowHeight).stroke();
229
+ return currentY + rowHeight;
230
+ }
231
+ };
232
+ // Annotate the CommonJS export names for ESM import in node:
233
+ 0 && (module.exports = {
234
+ PdfExporter
235
+ });
@@ -0,0 +1,18 @@
1
+ import { E as ExportTable, a as ExportFilter, b as ExportTableColumn } from '../../types-BzLsgnfc.cjs';
2
+
3
+ interface PdfSection {
4
+ table?: ExportTable;
5
+ filters?: ExportFilter[];
6
+ }
7
+ declare class PdfExporter {
8
+ generate(content: {
9
+ sections: PdfSection[];
10
+ }): Promise<Buffer>;
11
+ private sanitizeSections;
12
+ drawFilters(doc: PDFKit.PDFDocument, filters: ExportFilter[]): number;
13
+ drawTable(doc: PDFKit.PDFDocument, table: ExportTable, startY?: number): void;
14
+ drawHeader(doc: PDFKit.PDFDocument, columns: ExportTableColumn[], startY: number, tableWidth: number, headerHeight: number, cellPadding: number, headerFontSize: number, minColumnWidth: number): number;
15
+ drawRow(doc: PDFKit.PDFDocument, row: Record<string, unknown>, rowIndex: number, columns: ExportTableColumn[], currentY: number, tableWidth: number, rowHeight: number, cellPadding: number, dataFontSize: number, minColumnWidth: number): number;
16
+ }
17
+
18
+ export { PdfExporter, ExportFilter as PdfFilter, type PdfSection, ExportTable as PdfTable, ExportTableColumn as PdfTableColumn };
@@ -0,0 +1,18 @@
1
+ import { E as ExportTable, a as ExportFilter, b as ExportTableColumn } from '../../types-BzLsgnfc.js';
2
+
3
+ interface PdfSection {
4
+ table?: ExportTable;
5
+ filters?: ExportFilter[];
6
+ }
7
+ declare class PdfExporter {
8
+ generate(content: {
9
+ sections: PdfSection[];
10
+ }): Promise<Buffer>;
11
+ private sanitizeSections;
12
+ drawFilters(doc: PDFKit.PDFDocument, filters: ExportFilter[]): number;
13
+ drawTable(doc: PDFKit.PDFDocument, table: ExportTable, startY?: number): void;
14
+ drawHeader(doc: PDFKit.PDFDocument, columns: ExportTableColumn[], startY: number, tableWidth: number, headerHeight: number, cellPadding: number, headerFontSize: number, minColumnWidth: number): number;
15
+ drawRow(doc: PDFKit.PDFDocument, row: Record<string, unknown>, rowIndex: number, columns: ExportTableColumn[], currentY: number, tableWidth: number, rowHeight: number, cellPadding: number, dataFontSize: number, minColumnWidth: number): number;
16
+ }
17
+
18
+ export { PdfExporter, ExportFilter as PdfFilter, type PdfSection, ExportTable as PdfTable, ExportTableColumn as PdfTableColumn };
@@ -0,0 +1,200 @@
1
+ // src/exporters/pdf/index.ts
2
+ import PDFDocument from "pdfkit";
3
+
4
+ // src/exporters/pdf/pdf.utils.ts
5
+ var stripEmojis = (text) => {
6
+ if (!text) {
7
+ return text;
8
+ }
9
+ return text.replace(
10
+ /[\u{1F300}-\u{1F9FF}]|[\u{2600}-\u{26FF}]|[\u{2700}-\u{27BF}]|[\u{1F1E0}-\u{1F1FF}]|[\u{1F600}-\u{1F64F}]|[\u{1F680}-\u{1F6FF}]|[\u{1F900}-\u{1F9FF}]|[\u{1FA00}-\u{1FA6F}]|[\u{1FA70}-\u{1FAFF}]|[\u{FE00}-\u{FE0F}]|[\u{1F004}]|[\u{1F0CF}]|[\u{1F18E}]|[\u{1F191}-\u{1F19A}]|[\u{1F1E6}-\u{1F1FF}]|[\u{1F201}-\u{1F251}]/gu,
11
+ ""
12
+ ).replace(/\s+/g, " ").trim();
13
+ };
14
+
15
+ // src/exporters/pdf/index.ts
16
+ var FONT_BOLD = "Helvetica-Bold";
17
+ var FONT_REGULAR = "Helvetica";
18
+ var COLOR_BLACK = "#000000";
19
+ var COLOR_HEADER_BACKGROUND = "#D3D3D3";
20
+ var COLOR_ROW_BACKGROUND_ALTERNATE = "#F5F5F5";
21
+ var PdfExporter = class {
22
+ generate(content) {
23
+ const doc = new PDFDocument({ margin: 30, size: "A4", layout: "landscape" });
24
+ const buffers = [];
25
+ return new Promise((resolve, reject) => {
26
+ doc.on("data", (chunk) => buffers.push(chunk));
27
+ doc.on("end", () => resolve(Buffer.concat(buffers)));
28
+ doc.on("error", reject);
29
+ try {
30
+ content.sections.forEach((section, index) => {
31
+ const sanitizedSections = this.sanitizeSections(section);
32
+ if (index > 0) {
33
+ doc.addPage();
34
+ }
35
+ let currentY = doc.y;
36
+ if (sanitizedSections.filters && sanitizedSections.filters.length > 0) {
37
+ currentY = this.drawFilters(doc, sanitizedSections.filters);
38
+ }
39
+ if (sanitizedSections.table) {
40
+ this.drawTable(doc, sanitizedSections.table, currentY);
41
+ }
42
+ });
43
+ doc.end();
44
+ } catch (err) {
45
+ reject(err instanceof Error ? err : new Error(String(err)));
46
+ }
47
+ });
48
+ }
49
+ sanitizeSections(sections) {
50
+ const sanitized = {};
51
+ if (sections.filters) {
52
+ sanitized.filters = sections.filters.map((filter) => ({
53
+ label: stripEmojis(filter.label),
54
+ value: stripEmojis(filter.value)
55
+ }));
56
+ }
57
+ if (sections.table) {
58
+ sanitized.table = {
59
+ columns: sections.table.columns.map((col) => ({
60
+ ...col,
61
+ header: stripEmojis(col.header)
62
+ })),
63
+ rows: sections.table.rows.map((row) => {
64
+ const sanitizedRow = {};
65
+ for (const [key, value] of Object.entries(row)) {
66
+ sanitizedRow[key] = typeof value === "string" ? stripEmojis(value) : value;
67
+ }
68
+ return sanitizedRow;
69
+ })
70
+ };
71
+ }
72
+ return sanitized;
73
+ }
74
+ drawFilters(doc, filters) {
75
+ doc.fillColor(COLOR_BLACK).font(FONT_BOLD).fontSize(11).text("Applied Filters", { align: "left" });
76
+ doc.moveDown(0.5);
77
+ filters.forEach((filter) => {
78
+ doc.font(FONT_BOLD).fontSize(11).text(filter.label, { continued: true });
79
+ doc.font(FONT_REGULAR).text(`: ${filter.value}`);
80
+ });
81
+ doc.moveDown(1);
82
+ return doc.y;
83
+ }
84
+ drawTable(doc, table, startY = doc.y) {
85
+ const columns = table.columns.length > 0 ? table.columns.map((c) => ({ ...c })) : [{ header: "No data", key: "no_data", width: 100 }];
86
+ const rows = table.rows ?? [];
87
+ const baseCellPadding = 4;
88
+ const minColumnWidth = 35;
89
+ const columnCount = columns.length;
90
+ const headerFontSize = columnCount > 13 ? 8 : columnCount > 10 ? 9 : columnCount > 7 ? 10 : 11;
91
+ const dataFontSize = columnCount > 13 ? 7 : columnCount > 10 ? 8 : columnCount > 7 ? 9 : 10;
92
+ columns.forEach((col) => {
93
+ doc.font(FONT_BOLD).fontSize(headerFontSize);
94
+ let maxWidth = doc.widthOfString(col.header);
95
+ doc.font(FONT_REGULAR).fontSize(dataFontSize);
96
+ rows.forEach((row) => {
97
+ const rv = row[col.key];
98
+ const cellValue = typeof rv === "string" || typeof rv === "number" || typeof rv === "boolean" ? String(rv) : "";
99
+ const textWidth = doc.widthOfString(cellValue);
100
+ if (textWidth > maxWidth) {
101
+ maxWidth = textWidth;
102
+ }
103
+ });
104
+ col.width = Math.max(maxWidth + baseCellPadding * 2, minColumnWidth);
105
+ });
106
+ const usableWidth = doc.page.width - doc.page.margins.left - doc.page.margins.right;
107
+ const totalColumnWidth = columns.reduce((sum, c) => sum + (c.width ?? minColumnWidth), 0);
108
+ if (totalColumnWidth > usableWidth) {
109
+ const scale = usableWidth / totalColumnWidth;
110
+ columns.forEach((c) => {
111
+ c.width = Math.max(Math.floor((c.width ?? minColumnWidth) * scale), minColumnWidth);
112
+ });
113
+ }
114
+ const tableWidth = columns.reduce((sum, c) => sum + (c.width ?? minColumnWidth), 0);
115
+ const pageBottom = doc.page.height - doc.page.margins.bottom;
116
+ const calculateRowHeight = (row, isHeader = false) => {
117
+ const fontSize = isHeader ? headerFontSize : dataFontSize;
118
+ doc.font(isHeader ? FONT_BOLD : FONT_REGULAR).fontSize(fontSize);
119
+ let maxLines = 1;
120
+ columns.forEach((col) => {
121
+ const cellValue = isHeader ? col.header : row?.[col.key] !== void 0 ? String(row[col.key]) : "";
122
+ const textWidth = doc.widthOfString(cellValue);
123
+ const availableWidth = (col.width ?? minColumnWidth) - baseCellPadding * 2;
124
+ const lines = Math.ceil(textWidth / availableWidth);
125
+ if (lines > maxLines) {
126
+ maxLines = lines;
127
+ }
128
+ });
129
+ return Math.max(maxLines * (fontSize + 2) + baseCellPadding * 2, 25);
130
+ };
131
+ const headerHeight = calculateRowHeight(null, true);
132
+ const drawTableHeader = (y) => this.drawHeader(doc, columns, y, tableWidth, headerHeight, baseCellPadding, headerFontSize, minColumnWidth);
133
+ let currentY = drawTableHeader(startY);
134
+ doc.font(FONT_REGULAR).fontSize(dataFontSize);
135
+ rows.forEach((row, rowIndex) => {
136
+ const currentRowHeight = calculateRowHeight(row, false);
137
+ if (currentY + currentRowHeight > pageBottom) {
138
+ doc.addPage();
139
+ currentY = drawTableHeader(doc.page.margins.top);
140
+ doc.font(FONT_REGULAR).fontSize(dataFontSize);
141
+ }
142
+ currentY = this.drawRow(
143
+ doc,
144
+ row,
145
+ rowIndex,
146
+ columns,
147
+ currentY,
148
+ tableWidth,
149
+ currentRowHeight,
150
+ baseCellPadding,
151
+ dataFontSize,
152
+ minColumnWidth
153
+ );
154
+ });
155
+ }
156
+ drawHeader(doc, columns, startY, tableWidth, headerHeight, cellPadding, headerFontSize, minColumnWidth) {
157
+ doc.strokeColor(COLOR_BLACK).lineWidth(0.5).moveTo(doc.page.margins.left, startY).lineTo(doc.page.margins.left + tableWidth, startY).stroke();
158
+ doc.fillColor(COLOR_HEADER_BACKGROUND).rect(doc.page.margins.left, startY, tableWidth, headerHeight).fill();
159
+ let xPosition = doc.page.margins.left;
160
+ doc.fillColor(COLOR_BLACK).font(FONT_BOLD).fontSize(headerFontSize);
161
+ columns.forEach((col) => {
162
+ doc.strokeColor(COLOR_BLACK).lineWidth(0.5).moveTo(xPosition, startY).lineTo(xPosition, startY + headerHeight).stroke();
163
+ doc.text(col.header, xPosition + cellPadding, startY + cellPadding, {
164
+ width: (col.width ?? minColumnWidth) - cellPadding * 2,
165
+ align: "left",
166
+ lineBreak: false
167
+ });
168
+ xPosition += col.width ?? minColumnWidth;
169
+ });
170
+ doc.moveTo(xPosition, startY).lineTo(xPosition, startY + headerHeight).stroke();
171
+ doc.moveTo(doc.page.margins.left, startY + headerHeight).lineTo(xPosition, startY + headerHeight).stroke();
172
+ return startY + headerHeight;
173
+ }
174
+ drawRow(doc, row, rowIndex, columns, currentY, tableWidth, rowHeight, cellPadding, dataFontSize, minColumnWidth) {
175
+ doc.font(FONT_REGULAR).fontSize(dataFontSize);
176
+ if (rowIndex % 2 === 0) {
177
+ doc.fillColor(COLOR_ROW_BACKGROUND_ALTERNATE).rect(doc.page.margins.left, currentY, tableWidth, rowHeight).fill();
178
+ }
179
+ let xPosition = doc.page.margins.left;
180
+ doc.fillColor(COLOR_BLACK);
181
+ columns.forEach((col) => {
182
+ doc.strokeColor(COLOR_BLACK).lineWidth(0.5).moveTo(xPosition, currentY).lineTo(xPosition, currentY + rowHeight).stroke();
183
+ const rawValue = row[col.key];
184
+ const cellValue = typeof rawValue === "string" || typeof rawValue === "number" || typeof rawValue === "boolean" ? String(rawValue) : "";
185
+ const isNumeric = typeof rawValue === "number" || typeof rawValue === "string" && rawValue !== "" && !isNaN(Number(rawValue));
186
+ doc.text(cellValue, xPosition + cellPadding, currentY + cellPadding, {
187
+ width: (col.width ?? minColumnWidth) - cellPadding * 2,
188
+ align: isNumeric ? "right" : "left",
189
+ lineBreak: false
190
+ });
191
+ xPosition += col.width ?? minColumnWidth;
192
+ });
193
+ doc.moveTo(xPosition, currentY).lineTo(xPosition, currentY + rowHeight).stroke();
194
+ doc.moveTo(doc.page.margins.left, currentY + rowHeight).lineTo(xPosition, currentY + rowHeight).stroke();
195
+ return currentY + rowHeight;
196
+ }
197
+ };
198
+ export {
199
+ PdfExporter
200
+ };
@@ -0,0 +1,15 @@
1
+ interface ExportTableColumn {
2
+ header: string;
3
+ key: string;
4
+ width?: number;
5
+ }
6
+ interface ExportTable {
7
+ columns: ExportTableColumn[];
8
+ rows: Record<string, unknown>[];
9
+ }
10
+ interface ExportFilter {
11
+ label: string;
12
+ value: string;
13
+ }
14
+
15
+ export type { ExportTable as E, ExportFilter as a, ExportTableColumn as b };
@@ -0,0 +1,15 @@
1
+ interface ExportTableColumn {
2
+ header: string;
3
+ key: string;
4
+ width?: number;
5
+ }
6
+ interface ExportTable {
7
+ columns: ExportTableColumn[];
8
+ rows: Record<string, unknown>[];
9
+ }
10
+ interface ExportFilter {
11
+ label: string;
12
+ value: string;
13
+ }
14
+
15
+ export type { ExportTable as E, ExportFilter as a, ExportTableColumn as b };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@harbortouch/skytab-analytics-report-utils",
3
- "version": "0.16.1",
3
+ "version": "0.18.0",
4
4
  "description": "Centralized report column presentation configuration for SkyTab Analytics",
5
5
  "engines": {
6
6
  "node": ">=22.6.0",
@@ -16,9 +16,19 @@
16
16
  "require": "./dist/index.cjs"
17
17
  },
18
18
  "./csv": {
19
- "types": "./dist/csv/index.d.ts",
20
- "import": "./dist/csv/index.js",
21
- "require": "./dist/csv/index.cjs"
19
+ "types": "./dist/exporters/csv/index.d.ts",
20
+ "import": "./dist/exporters/csv/index.js",
21
+ "require": "./dist/exporters/csv/index.cjs"
22
+ },
23
+ "./pdf": {
24
+ "types": "./dist/exporters/pdf/index.d.ts",
25
+ "import": "./dist/exporters/pdf/index.js",
26
+ "require": "./dist/exporters/pdf/index.cjs"
27
+ },
28
+ "./excel": {
29
+ "types": "./dist/exporters/excel/index.d.ts",
30
+ "import": "./dist/exporters/excel/index.js",
31
+ "require": "./dist/exporters/excel/index.cjs"
22
32
  }
23
33
  },
24
34
  "files": [
@@ -36,6 +46,8 @@
36
46
  "prepare": "husky"
37
47
  },
38
48
  "devDependencies": {
49
+ "@types/node": "^26.0.1",
50
+ "@types/pdfkit": "^0.17.6",
39
51
  "eslint": "^9.39.2",
40
52
  "eslint-config-prettier": "^10.1.8",
41
53
  "eslint-import-resolver-typescript": "^4.2.2",
@@ -52,6 +64,8 @@
52
64
  },
53
65
  "packageManager": "pnpm@10.24.0+sha512.01ff8ae71b4419903b65c60fb2dc9d34cf8bb6e06d03bde112ef38f7a34d6904c424ba66bea5cdcf12890230bf39f9580473140ed9c946fef328b6e5238a345a",
54
66
  "dependencies": {
55
- "csv-stringify": "^6.8.0"
67
+ "csv-stringify": "^6.8.0",
68
+ "exceljs": "^4.4.0",
69
+ "pdfkit": "^0.19.1"
56
70
  }
57
71
  }
File without changes
File without changes