@cj-tech-master/excelts 6.2.0 → 7.0.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.
Files changed (56) hide show
  1. package/README.md +45 -17
  2. package/README_zh.md +43 -15
  3. package/dist/browser/index.browser.d.ts +1 -1
  4. package/dist/browser/index.browser.js +1 -1
  5. package/dist/browser/index.d.ts +2 -2
  6. package/dist/browser/index.js +1 -1
  7. package/dist/browser/modules/excel/stream/workbook-writer.browser.d.ts +0 -2
  8. package/dist/browser/modules/excel/stream/workbook-writer.d.ts +2 -2
  9. package/dist/browser/modules/excel/types.d.ts +0 -2
  10. package/dist/browser/modules/pdf/excel-bridge.d.ts +29 -0
  11. package/dist/browser/modules/pdf/excel-bridge.js +423 -0
  12. package/dist/browser/modules/pdf/index.d.ts +22 -24
  13. package/dist/browser/modules/pdf/index.js +22 -25
  14. package/dist/browser/modules/pdf/pdf.d.ts +121 -0
  15. package/dist/browser/modules/pdf/pdf.js +255 -0
  16. package/dist/browser/modules/pdf/render/layout-engine.d.ts +10 -8
  17. package/dist/browser/modules/pdf/render/layout-engine.js +115 -209
  18. package/dist/browser/modules/pdf/render/pdf-exporter.d.ts +9 -62
  19. package/dist/browser/modules/pdf/render/pdf-exporter.js +38 -78
  20. package/dist/browser/modules/pdf/render/style-converter.d.ts +20 -18
  21. package/dist/browser/modules/pdf/render/style-converter.js +24 -23
  22. package/dist/browser/modules/pdf/types.d.ts +193 -11
  23. package/dist/browser/modules/pdf/types.js +22 -1
  24. package/dist/cjs/index.js +3 -3
  25. package/dist/cjs/modules/pdf/excel-bridge.js +426 -0
  26. package/dist/cjs/modules/pdf/index.js +25 -28
  27. package/dist/cjs/modules/pdf/pdf.js +258 -0
  28. package/dist/cjs/modules/pdf/render/layout-engine.js +116 -210
  29. package/dist/cjs/modules/pdf/render/pdf-exporter.js +37 -79
  30. package/dist/cjs/modules/pdf/render/style-converter.js +24 -23
  31. package/dist/cjs/modules/pdf/types.js +23 -2
  32. package/dist/esm/index.browser.js +1 -1
  33. package/dist/esm/index.js +1 -1
  34. package/dist/esm/modules/pdf/excel-bridge.js +423 -0
  35. package/dist/esm/modules/pdf/index.js +22 -25
  36. package/dist/esm/modules/pdf/pdf.js +255 -0
  37. package/dist/esm/modules/pdf/render/layout-engine.js +115 -209
  38. package/dist/esm/modules/pdf/render/pdf-exporter.js +38 -78
  39. package/dist/esm/modules/pdf/render/style-converter.js +24 -23
  40. package/dist/esm/modules/pdf/types.js +22 -1
  41. package/dist/iife/excelts.iife.js +728 -251
  42. package/dist/iife/excelts.iife.js.map +1 -1
  43. package/dist/iife/excelts.iife.min.js +34 -34
  44. package/dist/types/index.browser.d.ts +1 -1
  45. package/dist/types/index.d.ts +2 -2
  46. package/dist/types/modules/excel/stream/workbook-writer.browser.d.ts +0 -2
  47. package/dist/types/modules/excel/stream/workbook-writer.d.ts +2 -2
  48. package/dist/types/modules/excel/types.d.ts +0 -2
  49. package/dist/types/modules/pdf/excel-bridge.d.ts +29 -0
  50. package/dist/types/modules/pdf/index.d.ts +22 -24
  51. package/dist/types/modules/pdf/pdf.d.ts +121 -0
  52. package/dist/types/modules/pdf/render/layout-engine.d.ts +10 -8
  53. package/dist/types/modules/pdf/render/pdf-exporter.d.ts +9 -62
  54. package/dist/types/modules/pdf/render/style-converter.d.ts +20 -18
  55. package/dist/types/modules/pdf/types.d.ts +193 -11
  56. package/package.json +1 -1
@@ -1,23 +1,14 @@
1
1
  "use strict";
2
2
  /**
3
- * PDF Exporter - Main orchestrator for Excel-to-PDF conversion.
3
+ * PDF Exporter - Main orchestrator for PDF document generation.
4
4
  *
5
5
  * Coordinates the layout engine, page renderer, font manager, and PDF writer
6
- * to produce a complete PDF document from an Excel workbook.
6
+ * to produce a complete PDF document from a PdfWorkbook data structure.
7
7
  *
8
- * @example
9
- * ```typescript
10
- * import { Workbook, PdfExporter } from "excelts";
11
- *
12
- * const workbook = new Workbook();
13
- * // ... populate workbook ...
14
- *
15
- * const exporter = new PdfExporter(workbook);
16
- * const pdfBuffer = exporter.export({ fitToPage: true });
17
- * ```
8
+ * This module is fully independent of the Excel module.
9
+ * It is used internally by the public `pdf()` and `excelToPdf()` APIs.
18
10
  */
19
11
  Object.defineProperty(exports, "__esModule", { value: true });
20
- exports.PdfExporter = void 0;
21
12
  exports.exportPdf = exportPdf;
22
13
  const pdf_writer_1 = require("../core/pdf-writer");
23
14
  const pdf_object_1 = require("../core/pdf-object");
@@ -34,22 +25,17 @@ const style_converter_1 = require("./style-converter");
34
25
  // Public API
35
26
  // =============================================================================
36
27
  /**
37
- * Export a workbook to PDF format.
28
+ * Export a PdfWorkbook to PDF format.
38
29
  *
39
- * @param workbook - The workbook to export
30
+ * @param workbook - The workbook data to export
40
31
  * @param options - Export options controlling layout, pagination, and appearance
41
32
  * @returns PDF file as a Uint8Array
42
- * @throws {PdfError} If the workbook has no worksheets or export fails
43
- *
44
- * @example
45
- * ```typescript
46
- * const pdf = exportPdf(workbook, { fitToPage: true, showGridLines: true });
47
- * ```
33
+ * @throws {PdfError} If the workbook has no sheets or export fails
48
34
  */
49
35
  function exportPdf(workbook, options) {
50
- const worksheets = selectWorksheets(workbook, options?.sheets);
51
- if (worksheets.length === 0) {
52
- throw new errors_1.PdfError("No worksheets to export. The workbook is empty or no sheets matched.");
36
+ const sheets = selectSheets(workbook, options?.sheets);
37
+ if (sheets.length === 0) {
38
+ throw new errors_1.PdfError("No sheets to export. The workbook is empty or no sheets matched.");
53
39
  }
54
40
  const fontManager = new font_manager_1.FontManager();
55
41
  const writer = new pdf_writer_1.PdfWriter();
@@ -63,19 +49,19 @@ function exportPdf(workbook, options) {
63
49
  throw new errors_1.PdfRenderError("Failed to parse TrueType font", { cause: err });
64
50
  }
65
51
  }
66
- // --- Step 1: Layout all worksheets ---
52
+ // --- Step 1: Layout all sheets ---
67
53
  const allPages = [];
68
- for (const worksheet of worksheets) {
54
+ for (const sheet of sheets) {
69
55
  try {
70
- const resolved = resolveOptions(options, worksheet);
71
- const pages = (0, layout_engine_1.layoutWorksheet)(worksheet, resolved, fontManager);
56
+ const resolved = resolveOptions(options, sheet);
57
+ const pages = (0, layout_engine_1.layoutSheet)(sheet, resolved, fontManager);
72
58
  allPages.push(...pages);
73
59
  }
74
60
  catch (err) {
75
- throw new errors_1.PdfRenderError(`Failed to layout worksheet "${worksheet.name}"`, { cause: err });
61
+ throw new errors_1.PdfRenderError(`Failed to layout sheet "${sheet.name}"`, { cause: err });
76
62
  }
77
63
  }
78
- const documentOptions = resolveOptions(options, worksheets[0]);
64
+ const documentOptions = resolveOptions(options, sheets[0]);
79
65
  if (allPages.length === 0) {
80
66
  // Create at least one empty page
81
67
  allPages.push({
@@ -84,17 +70,17 @@ function exportPdf(workbook, options) {
84
70
  cells: [],
85
71
  width: documentOptions.pageSize.width,
86
72
  height: documentOptions.pageSize.height,
87
- sheetName: worksheets[0]?.name ?? "Sheet1",
88
- worksheetCols: [],
73
+ sheetName: sheets[0]?.name ?? "Sheet1",
74
+ sheetCols: [],
89
75
  columnOffsets: [],
90
76
  columnWidths: [],
91
- worksheetRows: [],
77
+ sheetRows: [],
92
78
  rowYPositions: [],
93
79
  rowHeights: [],
94
80
  images: []
95
81
  });
96
82
  }
97
- // Fix page numbers (they may be off after combining multiple worksheets)
83
+ // Fix page numbers (they may be off after combining multiple sheets)
98
84
  for (let i = 0; i < allPages.length; i++) {
99
85
  allPages[i].pageNumber = i + 1;
100
86
  }
@@ -226,7 +212,7 @@ function exportPdf(workbook, options) {
226
212
  }
227
213
  // --- Step 6: Build catalog ---
228
214
  writer.addCatalog(pagesTreeObjNum, outlinesRef);
229
- // --- Step 6: Add document info ---
215
+ // --- Step 7: Add document info ---
230
216
  writer.addInfoDict({
231
217
  title: documentOptions.title || workbook.title || undefined,
232
218
  author: documentOptions.author || workbook.creator || undefined,
@@ -241,41 +227,16 @@ function exportPdf(workbook, options) {
241
227
  // --- Step 9: Build the PDF ---
242
228
  return writer.build();
243
229
  }
244
- /**
245
- * Class-based API for PDF export (wraps {@link exportPdf}).
246
- *
247
- * @example
248
- * ```typescript
249
- * const exporter = new PdfExporter(workbook);
250
- * const pdfBuffer = exporter.export({ fitToPage: true });
251
- * ```
252
- */
253
- class PdfExporter {
254
- constructor(workbook) {
255
- this.workbook = workbook;
256
- }
257
- /**
258
- * Export the workbook as a PDF document.
259
- *
260
- * @param options - Export options controlling layout, pagination, and appearance
261
- * @returns PDF file as a Uint8Array
262
- * @throws {PdfError} If the workbook has no worksheets or export fails
263
- */
264
- export(options) {
265
- return exportPdf(this.workbook, options);
266
- }
267
- }
268
- exports.PdfExporter = PdfExporter;
269
230
  // =============================================================================
270
- // Worksheet Selection
231
+ // Sheet Selection
271
232
  // =============================================================================
272
233
  /**
273
- * Select which worksheets to export based on the options.
234
+ * Select which sheets to export based on the options.
274
235
  */
275
- function selectWorksheets(workbook, sheets) {
276
- const allSheets = workbook.worksheets;
236
+ function selectSheets(workbook, sheets) {
237
+ const allSheets = workbook.sheets;
277
238
  if (!sheets || sheets.length === 0) {
278
- // Export all visible worksheets
239
+ // Export all visible sheets
279
240
  return allSheets.filter(ws => ws.state !== "hidden" && ws.state !== "veryHidden");
280
241
  }
281
242
  const result = [];
@@ -287,7 +248,7 @@ function selectWorksheets(workbook, sheets) {
287
248
  }
288
249
  }
289
250
  else if (typeof selector === "number") {
290
- // 1-based position in the worksheets array
251
+ // 1-based position in the sheets array
291
252
  const ws = allSheets[selector - 1];
292
253
  if (ws) {
293
254
  result.push(ws);
@@ -302,9 +263,9 @@ function selectWorksheets(workbook, sheets) {
302
263
  /**
303
264
  * Resolve user options with defaults.
304
265
  */
305
- function resolveOptions(options, worksheet) {
306
- // Use worksheet's pageSetup as fallback for unspecified options
307
- const ps = worksheet?.pageSetup;
266
+ function resolveOptions(options, sheet) {
267
+ // Use sheet's pageSetup as fallback for unspecified options
268
+ const ps = sheet?.pageSetup;
308
269
  const pageSize = resolvePageSize(options?.pageSize, ps?.paperSize);
309
270
  const orientation = options?.orientation ?? (ps?.orientation === "landscape" ? "landscape" : "portrait");
310
271
  const margins = resolveMargins(options?.margins, ps?.margins);
@@ -314,7 +275,7 @@ function resolveOptions(options, worksheet) {
314
275
  g: 0.816,
315
276
  b: 0.816
316
277
  };
317
- // Use worksheet's printTitlesRow as fallback for repeatRows
278
+ // Use sheet's printTitlesRow as fallback for repeatRows
318
279
  let repeatRows = options?.repeatRows ?? false;
319
280
  if (repeatRows === false && ps?.printTitlesRow) {
320
281
  // printTitlesRow format: "1:3" (repeat rows 1-3) or "1" (repeat row 1)
@@ -342,10 +303,7 @@ function resolveOptions(options, worksheet) {
342
303
  creator: options?.creator ?? "excelts"
343
304
  };
344
305
  }
345
- /**
346
- * Resolve the page size from options.
347
- */
348
- /** Map Excel PaperSize enum values to PDF page sizes. */
306
+ /** Map PaperSize enum values to PDF page sizes. */
349
307
  const PAPER_SIZE_MAP = {
350
308
  1: types_1.PageSizes.LETTER,
351
309
  5: types_1.PageSizes.LEGAL,
@@ -361,19 +319,19 @@ function resolvePageSize(size, paperSize) {
361
319
  }
362
320
  return size;
363
321
  }
364
- // Fallback to worksheet paperSize
322
+ // Fallback to sheet paperSize
365
323
  if (paperSize !== undefined) {
366
324
  return PAPER_SIZE_MAP[paperSize] ?? types_1.PageSizes.A4;
367
325
  }
368
326
  return types_1.PageSizes.A4;
369
327
  }
370
328
  /**
371
- * Resolve margins with defaults. Worksheet margins are in inches, convert to points (×72).
372
- * When partial PDF margins are specified, unset sides fall back to worksheet margins,
329
+ * Resolve margins with defaults. Sheet margins are in inches, convert to points (×72).
330
+ * When partial PDF margins are specified, unset sides fall back to sheet margins,
373
331
  * then to the default 72pt (1 inch).
374
332
  */
375
333
  function resolveMargins(margins, wsMargins) {
376
- // Build a base from worksheet pageSetup margins (inches → points), or default 72pt
334
+ // Build a base from sheet pageSetup margins (inches → points), or default 72pt
377
335
  const base = wsMargins
378
336
  ? {
379
337
  top: wsMargins.top * 72,
@@ -397,7 +355,7 @@ function resolveMargins(margins, wsMargins) {
397
355
  // =============================================================================
398
356
  /**
399
357
  * Build a PDF outlines tree for sheet-level navigation.
400
- * Creates one bookmark entry per worksheet, pointing to the first page.
358
+ * Creates one bookmark entry per sheet, pointing to the first page.
401
359
  */
402
360
  function buildOutlines(writer, sheetFirstPage, pageObjNums) {
403
361
  const outlinesObjNum = writer.allocObject();
@@ -1,9 +1,12 @@
1
1
  "use strict";
2
2
  /**
3
- * Converts Excel styles to PDF rendering parameters.
3
+ * Converts input styles to PDF rendering parameters.
4
4
  *
5
- * Maps Excel font, color, border, fill, and alignment properties
5
+ * Maps font, color, border, fill, and alignment properties
6
6
  * to their PDF equivalents for the layout engine and page renderer.
7
+ *
8
+ * This module is fully independent of the Excel module — it works with
9
+ * the PDF module's own style interfaces (PdfFontStyle, PdfFillData, etc.).
7
10
  */
8
11
  Object.defineProperty(exports, "__esModule", { value: true });
9
12
  exports.DEFAULT_COLORS = void 0;
@@ -19,8 +22,8 @@ exports.excelVAlignToPdf = excelVAlignToPdf;
19
22
  // Color Conversion
20
23
  // =============================================================================
21
24
  /**
22
- * Convert an Excel ARGB color string to PDF RGB color.
23
- * Excel uses "AARRGGBB" format (e.g., "FF000000" for black).
25
+ * Convert an ARGB color string to PDF RGB color.
26
+ * Handles "AARRGGBB" (8-char) and "RRGGBB" (6-char) formats.
24
27
  * PDF uses 0-1 floats for each component.
25
28
  */
26
29
  function argbToPdfColor(argb) {
@@ -57,7 +60,7 @@ function argbToPdfColor(argb) {
57
60
  };
58
61
  }
59
62
  /**
60
- * Convert an Excel Color object to PDF color.
63
+ * Convert a color data object to PDF color.
61
64
  * Handles both ARGB and theme-based colors.
62
65
  */
63
66
  function excelColorToPdf(color) {
@@ -74,7 +77,6 @@ function excelColorToPdf(color) {
74
77
  if (!base) {
75
78
  return null;
76
79
  }
77
- // Apply tint if present (tint field exists at runtime from XLSX layer)
78
80
  const tint = color.tint;
79
81
  if (tint !== undefined && tint !== 0) {
80
82
  return applyTint(base, tint);
@@ -84,7 +86,7 @@ function excelColorToPdf(color) {
84
86
  return null;
85
87
  }
86
88
  /**
87
- * Map Excel theme color indices to PDF colors.
89
+ * Map theme color indices to PDF colors.
88
90
  * These are the default Office theme colors.
89
91
  */
90
92
  function themeColorToPdf(themeIndex) {
@@ -138,7 +140,7 @@ exports.DEFAULT_COLORS = {
138
140
  // Font Conversion
139
141
  // =============================================================================
140
142
  /**
141
- * Extract font properties from an Excel font for PDF rendering.
143
+ * Extract font properties for PDF rendering.
142
144
  */
143
145
  function extractFontProperties(font, defaultFamily, defaultSize) {
144
146
  const fontFamily = font?.name ?? defaultFamily;
@@ -154,7 +156,7 @@ function extractFontProperties(font, defaultFamily, defaultSize) {
154
156
  // Fill Conversion
155
157
  // =============================================================================
156
158
  /**
157
- * Convert an Excel fill to a PDF background color.
159
+ * Convert a fill to a PDF background color.
158
160
  * Only pattern fills with "solid" pattern are supported as PDF backgrounds.
159
161
  * Other patterns are approximated or ignored.
160
162
  */
@@ -163,21 +165,20 @@ function excelFillToPdfColor(fill) {
163
165
  return null;
164
166
  }
165
167
  if (fill.type === "pattern") {
166
- const patternFill = fill;
167
- if (patternFill.pattern === "solid" && patternFill.fgColor) {
168
- return excelColorToPdf(patternFill.fgColor);
168
+ if (fill.pattern === "solid" && fill.fgColor) {
169
+ return excelColorToPdf(fill.fgColor);
169
170
  }
170
- if (patternFill.pattern === "none") {
171
+ if (fill.pattern === "none") {
171
172
  return null;
172
173
  }
173
174
  // For other patterns, use fgColor as approximation
174
- if (patternFill.fgColor) {
175
- return excelColorToPdf(patternFill.fgColor);
175
+ if (fill.fgColor) {
176
+ return excelColorToPdf(fill.fgColor);
176
177
  }
177
178
  }
178
179
  if (fill.type === "gradient") {
179
180
  // For gradient fills, use the first stop color as approximation
180
- if ("stops" in fill && fill.stops.length > 0) {
181
+ if (fill.stops && fill.stops.length > 0) {
181
182
  return excelColorToPdf(fill.stops[0].color);
182
183
  }
183
184
  }
@@ -187,7 +188,7 @@ function excelFillToPdfColor(fill) {
187
188
  // Border Conversion
188
189
  // =============================================================================
189
190
  /**
190
- * Map Excel border styles to PDF line widths and dash patterns.
191
+ * Map border styles to PDF line widths.
191
192
  */
192
193
  function borderStyleToLineWidth(style) {
193
194
  switch (style) {
@@ -222,7 +223,7 @@ function borderStyleToLineWidth(style) {
222
223
  }
223
224
  }
224
225
  /**
225
- * Map Excel border styles to PDF dash patterns.
226
+ * Map border styles to PDF dash patterns.
226
227
  * An empty array means a solid line.
227
228
  */
228
229
  function borderStyleToDashPattern(style) {
@@ -247,7 +248,7 @@ function borderStyleToDashPattern(style) {
247
248
  }
248
249
  }
249
250
  /**
250
- * Convert a single Excel border to a PDF LayoutBorder.
251
+ * Convert a single border side to a PDF LayoutBorder.
251
252
  */
252
253
  function convertBorder(border) {
253
254
  if (!border || !border.style) {
@@ -260,7 +261,7 @@ function convertBorder(border) {
260
261
  };
261
262
  }
262
263
  /**
263
- * Convert Excel Borders to PDF LayoutBorders.
264
+ * Convert border data to PDF LayoutBorders.
264
265
  */
265
266
  function excelBordersToPdf(borders) {
266
267
  if (!borders) {
@@ -277,7 +278,7 @@ function excelBordersToPdf(borders) {
277
278
  // Alignment Conversion
278
279
  // =============================================================================
279
280
  /**
280
- * Convert Excel horizontal alignment to PDF alignment.
281
+ * Convert horizontal alignment to PDF alignment.
281
282
  */
282
283
  function excelHAlignToPdf(alignment) {
283
284
  if (!alignment?.horizontal) {
@@ -298,11 +299,11 @@ function excelHAlignToPdf(alignment) {
298
299
  }
299
300
  }
300
301
  /**
301
- * Convert Excel vertical alignment to PDF alignment.
302
+ * Convert vertical alignment to PDF alignment.
302
303
  */
303
304
  function excelVAlignToPdf(alignment) {
304
305
  if (!alignment?.vertical) {
305
- return "bottom"; // Excel default is bottom
306
+ return "bottom"; // Default is bottom
306
307
  }
307
308
  switch (alignment.vertical) {
308
309
  case "top":
@@ -1,10 +1,31 @@
1
1
  "use strict";
2
2
  /**
3
3
  * Type definitions for the PDF module.
4
- * Covers export options, page layout, and internal rendering models.
4
+ * Covers input data models, export options, page layout, and internal rendering models.
5
+ *
6
+ * The input data models (PdfWorkbook, PdfSheetData, etc.) are fully independent of
7
+ * the Excel module, allowing the PDF engine to be used standalone.
5
8
  */
6
9
  Object.defineProperty(exports, "__esModule", { value: true });
7
- exports.PageSizes = void 0;
10
+ exports.PageSizes = exports.PdfCellType = void 0;
11
+ // =============================================================================
12
+ // PDF Input Data Model (Excel-independent)
13
+ // =============================================================================
14
+ /**
15
+ * Cell value type discriminator for the PDF engine.
16
+ */
17
+ exports.PdfCellType = {
18
+ Empty: 0,
19
+ String: 1,
20
+ Number: 2,
21
+ Boolean: 3,
22
+ Date: 4,
23
+ RichText: 5,
24
+ Error: 6,
25
+ Formula: 7,
26
+ Hyperlink: 8,
27
+ Merge: 9
28
+ };
8
29
  /**
9
30
  * Predefined page sizes.
10
31
  */
@@ -58,7 +58,7 @@ export { concatUint8Arrays, toUint8Array, stringToUint8Array, uint8ArrayToString
58
58
  // =============================================================================
59
59
  // PDF Export (Browser-compatible, zero external dependencies)
60
60
  // =============================================================================
61
- export { PdfExporter, exportPdf, PageSizes, PdfError, PdfRenderError, PdfFontError, PdfStructureError, isPdfError } from "./modules/pdf/index.js";
61
+ export { pdf, excelToPdf, PageSizes, PdfError, PdfRenderError, PdfFontError, PdfStructureError, isPdfError } from "./modules/pdf/index.js";
62
62
  // =============================================================================
63
63
  // Errors
64
64
  // =============================================================================
package/dist/esm/index.js CHANGED
@@ -54,7 +54,7 @@ export { concatUint8Arrays, toUint8Array, stringToUint8Array, uint8ArrayToString
54
54
  // =============================================================================
55
55
  // PDF Export
56
56
  // =============================================================================
57
- export { PdfExporter, exportPdf, PageSizes, PdfError, PdfRenderError, PdfFontError, PdfStructureError, isPdfError } from "./modules/pdf/index.js";
57
+ export { pdf, excelToPdf, PageSizes, PdfError, PdfRenderError, PdfFontError, PdfStructureError, isPdfError } from "./modules/pdf/index.js";
58
58
  // =============================================================================
59
59
  // Errors
60
60
  // =============================================================================