@pdfme/common 5.1.6 → 5.1.7-dev.2

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 (52) hide show
  1. package/dist/cjs/__tests__/dynamicTemplate.test.js +197 -0
  2. package/dist/cjs/__tests__/dynamicTemplate.test.js.map +1 -0
  3. package/dist/cjs/__tests__/expression.test.js +271 -0
  4. package/dist/cjs/__tests__/expression.test.js.map +1 -0
  5. package/dist/cjs/__tests__/helper.test.js +92 -254
  6. package/dist/cjs/__tests__/helper.test.js.map +1 -1
  7. package/dist/cjs/src/constants.js +6 -0
  8. package/dist/cjs/src/constants.js.map +1 -1
  9. package/dist/cjs/src/dynamicTemplate.js +239 -0
  10. package/dist/cjs/src/dynamicTemplate.js.map +1 -0
  11. package/dist/cjs/src/expression.js +392 -0
  12. package/dist/cjs/src/expression.js.map +1 -0
  13. package/dist/cjs/src/helper.js +6 -238
  14. package/dist/cjs/src/helper.js.map +1 -1
  15. package/dist/cjs/src/index.js +34 -31
  16. package/dist/cjs/src/index.js.map +1 -1
  17. package/dist/cjs/src/schema.js +1 -0
  18. package/dist/cjs/src/schema.js.map +1 -1
  19. package/dist/esm/__tests__/dynamicTemplate.test.js +172 -0
  20. package/dist/esm/__tests__/dynamicTemplate.test.js.map +1 -0
  21. package/dist/esm/__tests__/expression.test.js +269 -0
  22. package/dist/esm/__tests__/expression.test.js.map +1 -0
  23. package/dist/esm/__tests__/helper.test.js +94 -256
  24. package/dist/esm/__tests__/helper.test.js.map +1 -1
  25. package/dist/esm/src/constants.js +6 -0
  26. package/dist/esm/src/constants.js.map +1 -1
  27. package/dist/esm/src/dynamicTemplate.js +235 -0
  28. package/dist/esm/src/dynamicTemplate.js.map +1 -0
  29. package/dist/esm/src/expression.js +365 -0
  30. package/dist/esm/src/expression.js.map +1 -0
  31. package/dist/esm/src/helper.js +5 -236
  32. package/dist/esm/src/helper.js.map +1 -1
  33. package/dist/esm/src/index.js +5 -3
  34. package/dist/esm/src/index.js.map +1 -1
  35. package/dist/esm/src/schema.js +1 -0
  36. package/dist/esm/src/schema.js.map +1 -1
  37. package/dist/types/__tests__/dynamicTemplate.test.d.ts +1 -0
  38. package/dist/types/__tests__/expression.test.d.ts +1 -0
  39. package/dist/types/src/dynamicTemplate.d.ts +15 -0
  40. package/dist/types/src/expression.d.ts +6 -0
  41. package/dist/types/src/helper.d.ts +34 -15
  42. package/dist/types/src/index.d.ts +6 -4
  43. package/dist/types/src/schema.d.ts +3631 -517
  44. package/dist/types/src/types.d.ts +1 -1
  45. package/package.json +5 -1
  46. package/src/constants.ts +8 -0
  47. package/src/dynamicTemplate.ts +277 -0
  48. package/src/expression.ts +392 -0
  49. package/src/helper.ts +10 -282
  50. package/src/index.ts +6 -4
  51. package/src/schema.ts +1 -0
  52. package/src/types.ts +1 -1
@@ -0,0 +1,392 @@
1
+ import * as acorn from 'acorn';
2
+ import type { Node as AcornNode, Identifier, Property } from 'estree';
3
+ import type { SchemaPageArray } from './types';
4
+
5
+ const expressionCache = new Map<string, (context: Record<string, unknown>) => unknown>();
6
+ const parseDataCache = new Map<string, Record<string, unknown>>();
7
+
8
+ const parseData = (data: Record<string, unknown>): Record<string, unknown> => {
9
+ const key = JSON.stringify(data);
10
+ if (parseDataCache.has(key)) {
11
+ return parseDataCache.get(key)!;
12
+ }
13
+
14
+ const parsed = Object.fromEntries(
15
+ Object.entries(data).map(([key, value]) => {
16
+ if (typeof value === 'string') {
17
+ try {
18
+ const parsedValue = JSON.parse(value) as unknown;
19
+ return [key, parsedValue];
20
+ } catch {
21
+ return [key, value];
22
+ }
23
+ }
24
+ return [key, value];
25
+ })
26
+ );
27
+
28
+ parseDataCache.set(key, parsed);
29
+ return parsed;
30
+ };
31
+
32
+ const padZero = (num: number): string => String(num).padStart(2, '0');
33
+
34
+ const formatDate = (date: Date): string =>
35
+ `${date.getFullYear()}/${padZero(date.getMonth() + 1)}/${padZero(date.getDate())}`;
36
+
37
+ const formatDateTime = (date: Date): string =>
38
+ `${formatDate(date)} ${padZero(date.getHours())}:${padZero(date.getMinutes())}`;
39
+
40
+ const allowedGlobals: Record<string, unknown> = {
41
+ Math,
42
+ String,
43
+ Number,
44
+ Boolean,
45
+ Array,
46
+ Object,
47
+ Date,
48
+ JSON,
49
+ isNaN,
50
+ parseFloat,
51
+ parseInt,
52
+ decodeURI,
53
+ decodeURIComponent,
54
+ encodeURI,
55
+ encodeURIComponent,
56
+ };
57
+
58
+ const validateAST = (node: AcornNode): void => {
59
+ switch (node.type) {
60
+ case 'Literal':
61
+ case 'Identifier':
62
+ break;
63
+ case 'BinaryExpression':
64
+ case 'LogicalExpression': {
65
+ const binaryNode = node;
66
+ validateAST(binaryNode.left);
67
+ validateAST(binaryNode.right);
68
+ break;
69
+ }
70
+ case 'UnaryExpression': {
71
+ const unaryNode = node;
72
+ validateAST(unaryNode.argument);
73
+ break;
74
+ }
75
+ case 'ConditionalExpression': {
76
+ const condNode = node;
77
+ validateAST(condNode.test);
78
+ validateAST(condNode.consequent);
79
+ validateAST(condNode.alternate);
80
+ break;
81
+ }
82
+ case 'MemberExpression': {
83
+ const memberNode = node;
84
+ validateAST(memberNode.object);
85
+ if (memberNode.computed) {
86
+ validateAST(memberNode.property);
87
+ } else {
88
+ const propName = (memberNode.property as Identifier).name;
89
+ if (['constructor', '__proto__', 'prototype'].includes(propName)) {
90
+ throw new Error('Access to prohibited property');
91
+ }
92
+ const prohibitedMethods = ['toLocaleString', 'valueOf'];
93
+ if (typeof propName === 'string' && prohibitedMethods.includes(propName)) {
94
+ throw new Error(`Access to prohibited method: ${propName}`);
95
+ }
96
+ }
97
+ break;
98
+ }
99
+ case 'CallExpression': {
100
+ const callNode = node;
101
+ validateAST(callNode.callee);
102
+ callNode.arguments.forEach(validateAST);
103
+ break;
104
+ }
105
+ case 'ArrayExpression': {
106
+ const arrayNode = node;
107
+ arrayNode.elements.forEach((elem) => {
108
+ if (elem) validateAST(elem);
109
+ });
110
+ break;
111
+ }
112
+ case 'ObjectExpression': {
113
+ const objectNode = node;
114
+ objectNode.properties.forEach((prop) => {
115
+ const propNode = prop as Property;
116
+ validateAST(propNode.key);
117
+ validateAST(propNode.value);
118
+ });
119
+ break;
120
+ }
121
+ case 'ArrowFunctionExpression': {
122
+ const arrowFuncNode = node;
123
+ arrowFuncNode.params.forEach((param) => {
124
+ if (param.type !== 'Identifier') {
125
+ throw new Error('Only identifier parameters are supported in arrow functions');
126
+ }
127
+ validateAST(param);
128
+ });
129
+ validateAST(arrowFuncNode.body);
130
+ break;
131
+ }
132
+ default:
133
+ throw new Error(`Unsupported syntax in placeholder: ${node.type}`);
134
+ }
135
+ };
136
+
137
+ const evaluateAST = (node: AcornNode, context: Record<string, unknown>): unknown => {
138
+ switch (node.type) {
139
+ case 'Literal': {
140
+ const literalNode = node;
141
+ return literalNode.value;
142
+ }
143
+ case 'Identifier': {
144
+ const idNode = node;
145
+ if (Object.prototype.hasOwnProperty.call(context, idNode.name)) {
146
+ return context[idNode.name];
147
+ } else if (Object.prototype.hasOwnProperty.call(allowedGlobals, idNode.name)) {
148
+ return allowedGlobals[idNode.name];
149
+ } else {
150
+ throw new Error(`Undefined variable: ${idNode.name}`);
151
+ }
152
+ }
153
+ case 'BinaryExpression': {
154
+ const binaryNode = node;
155
+ const left = evaluateAST(binaryNode.left, context) as number;
156
+ const right = evaluateAST(binaryNode.right, context) as number;
157
+ switch (binaryNode.operator) {
158
+ case '+':
159
+ return left + right;
160
+ case '-':
161
+ return left - right;
162
+ case '*':
163
+ return left * right;
164
+ case '/':
165
+ return left / right;
166
+ case '%':
167
+ return left % right;
168
+ case '**':
169
+ return left ** right;
170
+ default:
171
+ throw new Error(`Unsupported operator: ${binaryNode.operator}`);
172
+ }
173
+ }
174
+ case 'LogicalExpression': {
175
+ const logicalNode = node;
176
+ const leftLogical = evaluateAST(logicalNode.left, context);
177
+ const rightLogical = evaluateAST(logicalNode.right, context);
178
+ switch (logicalNode.operator) {
179
+ case '&&':
180
+ return leftLogical && rightLogical;
181
+ case '||':
182
+ return leftLogical || rightLogical;
183
+ default:
184
+ throw new Error(`Unsupported operator: ${logicalNode.operator}`);
185
+ }
186
+ }
187
+ case 'UnaryExpression': {
188
+ const unaryNode = node;
189
+ const arg = evaluateAST(unaryNode.argument, context) as number;
190
+ switch (unaryNode.operator) {
191
+ case '+':
192
+ return +arg;
193
+ case '-':
194
+ return -arg;
195
+ case '!':
196
+ return !arg;
197
+ default:
198
+ throw new Error(`Unsupported operator: ${unaryNode.operator}`);
199
+ }
200
+ }
201
+ case 'ConditionalExpression': {
202
+ const condNode = node;
203
+ const test = evaluateAST(condNode.test, context);
204
+ return test
205
+ ? evaluateAST(condNode.consequent, context)
206
+ : evaluateAST(condNode.alternate, context);
207
+ }
208
+ case 'MemberExpression': {
209
+ const memberNode = node;
210
+ const obj = evaluateAST(memberNode.object, context) as Record<string, unknown>;
211
+ let prop: string | number;
212
+ if (memberNode.computed) {
213
+ prop = evaluateAST(memberNode.property, context) as string | number;
214
+ } else {
215
+ prop = (memberNode.property as Identifier).name;
216
+ }
217
+ if (typeof prop === 'string' || typeof prop === 'number') {
218
+ if (typeof prop === 'string' && ['constructor', '__proto__', 'prototype'].includes(prop)) {
219
+ throw new Error('Access to prohibited property');
220
+ }
221
+ return obj[prop];
222
+ } else {
223
+ throw new Error('Invalid property access');
224
+ }
225
+ }
226
+ case 'CallExpression': {
227
+ const callNode = node;
228
+ const callee = evaluateAST(callNode.callee, context);
229
+ const args = callNode.arguments.map((argNode) => evaluateAST(argNode, context));
230
+ if (typeof callee === 'function') {
231
+ if (callNode.callee.type === 'MemberExpression') {
232
+ const memberExpr = callNode.callee;
233
+ const obj = evaluateAST(memberExpr.object, context);
234
+ if (
235
+ obj !== null &&
236
+ (typeof obj === 'object' ||
237
+ typeof obj === 'number' ||
238
+ typeof obj === 'string' ||
239
+ typeof obj === 'boolean')
240
+ ) {
241
+ return callee.call(obj, ...args);
242
+ } else {
243
+ throw new Error('Invalid object in member function call');
244
+ }
245
+ } else {
246
+ return callee(...args);
247
+ }
248
+ } else {
249
+ throw new Error('Attempted to call a non-function');
250
+ }
251
+ }
252
+ case 'ArrowFunctionExpression': {
253
+ const arrowFuncNode = node;
254
+ const params = arrowFuncNode.params.map((param) => (param as Identifier).name);
255
+ const body = arrowFuncNode.body;
256
+
257
+ return (...args: unknown[]) => {
258
+ const newContext = { ...context };
259
+ params.forEach((param, index) => {
260
+ newContext[param] = args[index];
261
+ });
262
+ return evaluateAST(body, newContext);
263
+ };
264
+ }
265
+ case 'ArrayExpression': {
266
+ const arrayNode = node;
267
+ return arrayNode.elements.map((elem) => (elem ? evaluateAST(elem, context) : null));
268
+ }
269
+ case 'ObjectExpression': {
270
+ const objectNode = node;
271
+ const objResult: Record<string, unknown> = {};
272
+ objectNode.properties.forEach((prop) => {
273
+ const propNode = prop as Property;
274
+ let key: string;
275
+ if (propNode.key.type === 'Identifier') {
276
+ key = propNode.key.name;
277
+ } else {
278
+ const evaluatedKey = evaluateAST(propNode.key, context);
279
+ if (typeof evaluatedKey !== 'string' && typeof evaluatedKey !== 'number') {
280
+ throw new Error('Object property keys must be strings or numbers');
281
+ }
282
+ key = String(evaluatedKey);
283
+ }
284
+ const value = evaluateAST(propNode.value, context);
285
+ objResult[key] = value;
286
+ });
287
+ return objResult;
288
+ }
289
+ default:
290
+ throw new Error(`Unsupported syntax in placeholder: ${node.type}`);
291
+ }
292
+ };
293
+
294
+ const evaluatePlaceholders = (arg: {
295
+ content: string;
296
+ context: Record<string, unknown>;
297
+ }): string => {
298
+ const { content, context } = arg;
299
+
300
+ let resultContent = '';
301
+ let index = 0;
302
+
303
+ while (index < content.length) {
304
+ const startIndex = content.indexOf('{', index);
305
+ if (startIndex === -1) {
306
+ resultContent += content.slice(index);
307
+ break;
308
+ }
309
+
310
+ resultContent += content.slice(index, startIndex);
311
+ let braceCount = 1;
312
+ let endIndex = startIndex + 1;
313
+
314
+ while (endIndex < content.length && braceCount > 0) {
315
+ if (content[endIndex] === '{') {
316
+ braceCount++;
317
+ } else if (content[endIndex] === '}') {
318
+ braceCount--;
319
+ }
320
+ endIndex++;
321
+ }
322
+
323
+ if (braceCount === 0) {
324
+ const code = content.slice(startIndex + 1, endIndex - 1).trim();
325
+
326
+ if (expressionCache.has(code)) {
327
+ const evalFunc = expressionCache.get(code)!;
328
+ try {
329
+ const value = evalFunc(context);
330
+ resultContent += String(value);
331
+ } catch {
332
+ resultContent += content.slice(startIndex, endIndex);
333
+ }
334
+ } else {
335
+ try {
336
+ const ast = acorn.parseExpressionAt(code, 0, { ecmaVersion: 'latest' }) as AcornNode;
337
+ validateAST(ast);
338
+ const evalFunc = (ctx: Record<string, unknown>) => evaluateAST(ast, ctx);
339
+ expressionCache.set(code, evalFunc);
340
+ const value = evalFunc(context);
341
+ resultContent += String(value);
342
+ } catch {
343
+ resultContent += content.slice(startIndex, endIndex);
344
+ }
345
+ }
346
+
347
+ index = endIndex;
348
+ } else {
349
+ throw new Error('Invalid placeholder');
350
+ }
351
+ }
352
+
353
+ return resultContent;
354
+ };
355
+
356
+
357
+ export const replacePlaceholders = (arg: {
358
+ content: string;
359
+ variables: Record<string, any>;
360
+ schemas: SchemaPageArray;
361
+ }): string => {
362
+ const { content, variables, schemas } = arg;
363
+ if (!content || typeof content !== 'string' || !content.includes('{') || !content.includes('}')) {
364
+ return content;
365
+ }
366
+
367
+ const date = new Date();
368
+ const formattedDate = formatDate(date);
369
+ const formattedDateTime = formatDateTime(date);
370
+
371
+ const data = {
372
+ ...Object.fromEntries(
373
+ schemas.flat().map((schema) => [schema.name, schema.readOnly ? schema.content || '' : ''])
374
+ ),
375
+ ...variables,
376
+ };
377
+ const parsedInput = parseData(data);
378
+
379
+ const context: Record<string, unknown> = {
380
+ date: formattedDate,
381
+ dateTime: formattedDateTime,
382
+ ...parsedInput,
383
+ };
384
+
385
+ Object.entries(context).forEach(([key, value]) => {
386
+ if (typeof value === 'string' && value.includes('{') && value.includes('}')) {
387
+ context[key] = evaluatePlaceholders({ content: value, context });
388
+ }
389
+ });
390
+
391
+ return evaluatePlaceholders({ content, context });
392
+ };
package/src/helper.ts CHANGED
@@ -7,9 +7,8 @@ import {
7
7
  BasePdf,
8
8
  Plugins,
9
9
  BlankPdf,
10
- CommonOptions,
11
10
  LegacySchemaPageArray,
12
- SchemaPageArray
11
+ SchemaPageArray,
13
12
  } from './types';
14
13
  import {
15
14
  Inputs as InputsSchema,
@@ -98,7 +97,11 @@ export const migrateTemplate = (template: Template) => {
98
97
  return;
99
98
  }
100
99
 
101
- if (Array.isArray(template.schemas) && template.schemas.length > 0 && !Array.isArray(template.schemas[0])) {
100
+ if (
101
+ Array.isArray(template.schemas) &&
102
+ template.schemas.length > 0 &&
103
+ !Array.isArray(template.schemas[0])
104
+ ) {
102
105
  template.schemas = (template.schemas as unknown as LegacySchemaPageArray).map(
103
106
  (page: Record<string, Schema>) =>
104
107
  Object.entries(page).map(([key, value]) => ({
@@ -113,8 +116,8 @@ export const getInputFromTemplate = (template: Template): { [key: string]: strin
113
116
  migrateTemplate(template);
114
117
 
115
118
  const input: { [key: string]: string } = {};
116
- template.schemas.forEach(page => {
117
- page.forEach(schema => {
119
+ template.schemas.forEach((page) => {
120
+ page.forEach((schema) => {
118
121
  if (!schema.readOnly) {
119
122
  input[schema.name] = schema.content || '';
120
123
  }
@@ -258,289 +261,14 @@ export const checkUIProps = (data: unknown) => {
258
261
  migrateTemplate(data.template as Template);
259
262
  }
260
263
  checkProps(data, UIPropsSchema);
261
- }
264
+ };
262
265
  export const checkTemplate = (template: unknown) => {
263
266
  migrateTemplate(template as Template);
264
267
  checkProps(template, TemplateSchema);
265
- }
268
+ };
266
269
  export const checkGenerateProps = (data: unknown) => {
267
270
  if (typeof data === 'object' && data !== null && 'template' in data) {
268
271
  migrateTemplate(data.template as Template);
269
272
  }
270
273
  checkProps(data, GeneratePropsSchema);
271
- }
272
-
273
- interface ModifyTemplateForDynamicTableArg {
274
- template: Template;
275
- input: Record<string, string>;
276
- _cache: Map<any, any>;
277
- options: CommonOptions;
278
- getDynamicHeights: (
279
- value: string,
280
- args: { schema: Schema; basePdf: BasePdf; options: CommonOptions; _cache: Map<any, any> }
281
- ) => Promise<number[]>;
282
- }
283
-
284
- class Node {
285
- index = 0;
286
-
287
- schema?: Schema;
288
-
289
- children: Node[] = [];
290
-
291
- width = 0;
292
- height = 0;
293
- padding: [number, number, number, number] = [0, 0, 0, 0];
294
- position: { x: number; y: number } = { x: 0, y: 0 };
295
-
296
- constructor({ width = 0, height = 0 } = {}) {
297
- this.width = width;
298
- this.height = height;
299
- }
300
-
301
- setIndex(index: number): void {
302
- this.index = index;
303
- }
304
-
305
- setSchema(schema: Schema): void {
306
- this.schema = schema;
307
- }
308
-
309
- setWidth(width: number): void {
310
- this.width = width;
311
- }
312
-
313
- setHeight(height: number): void {
314
- this.height = height;
315
- }
316
-
317
- setPadding(padding: [number, number, number, number]): void {
318
- this.padding = padding;
319
- }
320
-
321
- setPosition(position: { x: number; y: number }): void {
322
- this.position = position;
323
- }
324
-
325
- insertChild(child: Node): void {
326
- const index = this.getChildCount();
327
- child.setIndex(index);
328
- this.children.splice(index, 0, child);
329
- }
330
-
331
- getChildCount(): number {
332
- return this.children.length;
333
- }
334
-
335
- getChild(index: number): Node {
336
- return this.children[index];
337
- }
338
- }
339
-
340
- function createPage(basePdf: BlankPdf) {
341
- const page = new Node({ ...basePdf });
342
- page.setPadding(basePdf.padding);
343
- return page;
344
- }
345
-
346
- function createNode(arg: {
347
- schema: Schema;
348
- position: { x: number; y: number };
349
- width: number;
350
- height: number;
351
- }) {
352
- const { position, width, height, schema } = arg;
353
- const node = new Node({ width, height });
354
- node.setPosition(position);
355
- node.setSchema(schema);
356
- return node;
357
- }
358
-
359
- function resortChildren(page: Node, orderMap: Map<string, number>): void {
360
- page.children = page.children
361
- .sort((a, b) => {
362
- const orderA = orderMap.get(a.schema?.name!);
363
- const orderB = orderMap.get(b.schema?.name!);
364
- if (orderA === undefined || orderB === undefined) {
365
- throw new Error('[@pdfme/common] order is not defined');
366
- }
367
- return orderA - orderB;
368
- })
369
- .map((child, index) => {
370
- child.setIndex(index);
371
- return child;
372
- });
373
- }
374
-
375
- async function createOnePage(
376
- arg: {
377
- basePdf: BlankPdf;
378
- schemaPage: Schema[];
379
- orderMap: Map<string, number>;
380
- } & Omit<ModifyTemplateForDynamicTableArg, 'template'>
381
- ): Promise<Node> {
382
- const { basePdf, schemaPage, orderMap, input, options, _cache, getDynamicHeights } = arg;
383
- const page = createPage(basePdf);
384
-
385
- const schemaPositions: number[] = [];
386
- const sortedSchemaEntries = cloneDeep(schemaPage).sort((a, b) => a.position.y - b.position.y);
387
- const diffMap = new Map();
388
- for (const schema of sortedSchemaEntries) {
389
- const { position, width } = schema;
390
-
391
- const opt = { schema, basePdf, options, _cache };
392
- const value = (schema.readOnly ? schema.content : input?.[schema.name]) || '';
393
- const heights = await getDynamicHeights(value, opt);
394
-
395
- const heightsSum = heights.reduce((acc, cur) => acc + cur, 0);
396
- const originalHeight = schema.height;
397
- if (heightsSum !== originalHeight) {
398
- diffMap.set(position.y + originalHeight, heightsSum - originalHeight);
399
- }
400
- heights.forEach((height, index) => {
401
- let y = schema.position.y + heights.reduce((acc, cur, i) => (i < index ? acc + cur : acc), 0);
402
- for (const [diffY, diff] of diffMap.entries()) {
403
- if (diffY <= schema.position.y) {
404
- y += diff;
405
- }
406
- }
407
- const node = createNode({ schema, position: { ...position, y }, width, height });
408
-
409
- schemaPositions.push(y + height + basePdf.padding[2]);
410
- page.insertChild(node);
411
- });
412
- }
413
-
414
- const pageHeight = Math.max(...schemaPositions, basePdf.height - basePdf.padding[2]);
415
- page.setHeight(pageHeight);
416
-
417
- resortChildren(page, orderMap);
418
-
419
- return page;
420
- }
421
-
422
- function breakIntoPages(arg: {
423
- longPage: Node;
424
- orderMap: Map<string, number>;
425
- basePdf: BlankPdf;
426
- }): Node[] {
427
- const { longPage, orderMap, basePdf } = arg;
428
- const pages: Node[] = [createPage(basePdf)];
429
- const [paddingTop, , paddingBottom] = basePdf.padding;
430
- const yAdjustments: { page: number; value: number }[] = [];
431
-
432
- const getPageHeight = (pageIndex: number) =>
433
- basePdf.height - paddingBottom - (pageIndex > 0 ? paddingTop : 0);
434
-
435
- const calculateNewY = (y: number, pageIndex: number) => {
436
- const newY = y - pageIndex * (basePdf.height - paddingTop - paddingBottom);
437
-
438
- while (pages.length <= pageIndex) {
439
- if (!pages[pageIndex]) {
440
- pages.push(createPage(basePdf));
441
- yAdjustments.push({ page: pageIndex, value: (newY - paddingTop) * -1 });
442
- }
443
- }
444
- return newY + (yAdjustments.find((adj) => adj.page === pageIndex)?.value || 0);
445
- };
446
-
447
- const children = longPage.children.sort((a, b) => a.position.y - b.position.y);
448
- for (let i = 0; i < children.length; i++) {
449
- const { schema, position, height, width } = children[i];
450
- const { y, x } = position;
451
-
452
- let targetPageIndex = Math.floor(y / getPageHeight(pages.length - 1));
453
- let newY = calculateNewY(y, targetPageIndex);
454
-
455
- if (newY + height > basePdf.height - paddingBottom) {
456
- targetPageIndex++;
457
- newY = calculateNewY(y, targetPageIndex);
458
- }
459
-
460
- if (!schema) throw new Error('[@pdfme/common] schema is undefined');
461
-
462
- const clonedElement = createNode({ schema, position: { x, y: newY }, width, height });
463
- pages[targetPageIndex].insertChild(clonedElement);
464
- }
465
-
466
- pages.forEach((page) => resortChildren(page, orderMap));
467
-
468
- return pages;
469
- }
470
-
471
- function createNewTemplate(pages: Node[], basePdf: BlankPdf): Template {
472
- const newTemplate: Template = {
473
- schemas: Array.from({ length: pages.length }, () => ([] as Schema[])),
474
- basePdf: basePdf,
475
- };
476
-
477
- const nameToSchemas = new Map<string, Node[]>();
478
-
479
- cloneDeep(pages).forEach((page, pageIndex) => {
480
- page.children.forEach((child) => {
481
- const { schema } = child;
482
- if (!schema) throw new Error('[@pdfme/common] schema is undefined');
483
-
484
- const name = schema.name
485
- if (!nameToSchemas.has(name)) {
486
- nameToSchemas.set(name, []);
487
- }
488
- nameToSchemas.get(name)!.push(child);
489
-
490
- const sameNameSchemas = page.children.filter((c) => c.schema?.name === name);
491
- const start = nameToSchemas.get(name)!.length - sameNameSchemas.length;
492
-
493
- if (sameNameSchemas.length > 0) {
494
- if (!sameNameSchemas[0].schema) {
495
- throw new Error('[@pdfme/common] schema is undefined');
496
- }
497
-
498
- // Use the first schema to get the schema and position
499
- const schema = sameNameSchemas[0].schema;
500
- const height = sameNameSchemas.reduce((acc, cur) => acc + cur.height, 0);
501
- const position = sameNameSchemas[0].position;
502
-
503
- // Currently, __bodyRange exists for table schemas, but if we make it more abstract,
504
- // it could be used for other schemas as well to render schemas that have been split by page breaks, starting from the middle.
505
- schema.__bodyRange = {
506
- start: Math.max(start - 1, 0),
507
- end: start + sameNameSchemas.length - 1,
508
- };
509
-
510
- // Currently, this is used to determine whether to display the header when a table is split.
511
- schema.__isSplit = start > 0;
512
-
513
- const newSchema = Object.assign({}, schema, { position, height });
514
- const index = newTemplate.schemas[pageIndex].findIndex((s) => s.name === name);
515
- if (index !== -1) {
516
- newTemplate.schemas[pageIndex][index] = newSchema;
517
- } else {
518
- newTemplate.schemas[pageIndex].push(newSchema);
519
- }
520
- }
521
- });
522
- });
523
-
524
- return newTemplate;
525
- }
526
-
527
- export const getDynamicTemplate = async (
528
- arg: ModifyTemplateForDynamicTableArg
529
- ): Promise<Template> => {
530
- const { template } = arg;
531
- if (!isBlankPdf(template.basePdf)) {
532
- return template;
533
- }
534
-
535
- const basePdf = template.basePdf as BlankPdf;
536
- const pages: Node[] = [];
537
-
538
- for (const schemaPage of template.schemas) {
539
- const orderMap = new Map(schemaPage.map((schema, index) => [schema.name, index]));
540
- const longPage = await createOnePage({ basePdf, schemaPage, orderMap, ...arg });
541
- const brokenPages = breakIntoPages({ longPage, basePdf, orderMap });
542
- pages.push(...brokenPages);
543
- }
544
-
545
- return createNewTemplate(pages, template.basePdf);
546
274
  };