@pdfme/common 6.0.3-dev.0 → 6.0.4-dev.1

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.
@@ -1,321 +0,0 @@
1
- import { Schema, Template, BasePdf, BlankPdf, CommonOptions } from './types.js';
2
- import { cloneDeep, isBlankPdf } from './helper.js';
3
-
4
- /** Floating point tolerance for comparisons */
5
- const EPSILON = 0.01;
6
-
7
- interface ModifyTemplateForDynamicTableArg {
8
- template: Template;
9
- input: Record<string, string>;
10
- _cache: Map<string | number, unknown>;
11
- options: CommonOptions;
12
- getDynamicHeights: (
13
- value: string,
14
- args: {
15
- schema: Schema;
16
- basePdf: BasePdf;
17
- options: CommonOptions;
18
- _cache: Map<string | number, unknown>;
19
- },
20
- ) => Promise<number[]>;
21
- }
22
-
23
- interface LayoutItem {
24
- schema: Schema;
25
- baseY: number;
26
- height: number;
27
- dynamicHeights: number[];
28
- }
29
-
30
- /** Calculate the content height of a page (drawable area excluding padding) */
31
- const getContentHeight = (basePdf: BlankPdf): number =>
32
- basePdf.height - basePdf.padding[0] - basePdf.padding[2];
33
-
34
- /** Get the input value for a schema */
35
- const getSchemaValue = (schema: Schema, input: Record<string, string>): string =>
36
- (schema.readOnly ? schema.content : input?.[schema.name]) || '';
37
-
38
- /**
39
- * Normalize schemas within a single page into layout items.
40
- * Returns items sorted by Y coordinate with their order preserved.
41
- */
42
- function normalizePageSchemas(
43
- pageSchemas: Schema[],
44
- paddingTop: number,
45
- ): { items: LayoutItem[]; orderMap: Map<string, number> } {
46
- const items: LayoutItem[] = [];
47
- const orderMap = new Map<string, number>();
48
-
49
- pageSchemas.forEach((schema, index) => {
50
- // Guard against negative Y position when schema.y < paddingTop
51
- // Prevents "Cannot read properties of undefined (reading 'push')" error
52
- const localY = Math.max(0, schema.position.y - paddingTop);
53
- items.push({
54
- schema: cloneDeep(schema),
55
- baseY: localY,
56
- height: schema.height,
57
- dynamicHeights: [schema.height], // Will be updated later
58
- });
59
- orderMap.set(schema.name, index);
60
- });
61
-
62
- // Sort by Y coordinate (preserve original order for same position)
63
- items.sort((a, b) => {
64
- if (Math.abs(a.baseY - b.baseY) > EPSILON) {
65
- return a.baseY - b.baseY;
66
- }
67
- return (orderMap.get(a.schema.name) ?? 0) - (orderMap.get(b.schema.name) ?? 0);
68
- });
69
-
70
- return { items, orderMap };
71
- }
72
-
73
- /**
74
- * Place rows on pages, splitting across pages as needed.
75
- * @returns The final global Y coordinate after placement
76
- */
77
- function placeRowsOnPages(
78
- schema: Schema,
79
- dynamicHeights: number[],
80
- startGlobalY: number,
81
- contentHeight: number,
82
- paddingTop: number,
83
- pages: Schema[][],
84
- ): number {
85
- let currentRowIndex = 0;
86
- let currentPageIndex = Math.floor(startGlobalY / contentHeight);
87
- let currentYInPage = startGlobalY % contentHeight;
88
-
89
- if (currentYInPage < 0) currentYInPage = 0;
90
-
91
- let actualGlobalEndY = 0;
92
- const isSplittable = dynamicHeights.length > 1;
93
-
94
- while (currentRowIndex < dynamicHeights.length) {
95
- // Ensure page exists
96
- while (pages.length <= currentPageIndex) pages.push([]);
97
-
98
- const spaceLeft = contentHeight - currentYInPage;
99
- const rowHeight = dynamicHeights[currentRowIndex];
100
-
101
- // If row doesn't fit, move to next page
102
- if (rowHeight > spaceLeft + EPSILON) {
103
- const isAtPageStart = Math.abs(spaceLeft - contentHeight) <= EPSILON;
104
-
105
- if (!isAtPageStart) {
106
- currentPageIndex++;
107
- currentYInPage = 0;
108
- continue;
109
- }
110
- // Force placement for oversized rows that don't fit even on a fresh page
111
- }
112
-
113
- // Pack as many rows as possible on this page
114
- let chunkHeight = 0;
115
- const startRowIndex = currentRowIndex;
116
-
117
- while (currentRowIndex < dynamicHeights.length) {
118
- const h = dynamicHeights[currentRowIndex];
119
- if (currentYInPage + chunkHeight + h <= contentHeight + EPSILON) {
120
- chunkHeight += h;
121
- currentRowIndex++;
122
- } else {
123
- break;
124
- }
125
- }
126
-
127
- // Don't leave header alone on a page without any data rows
128
- // If only header fits and there are data rows remaining, move everything to next page
129
- // BUT: if already at page top, don't move (prevents infinite loop when data row is too large)
130
- const isAtPageTop = currentYInPage <= EPSILON;
131
- if (
132
- isSplittable &&
133
- startRowIndex === 0 &&
134
- currentRowIndex === 1 &&
135
- dynamicHeights.length > 1 &&
136
- !isAtPageTop
137
- ) {
138
- currentRowIndex = 0;
139
- currentPageIndex++;
140
- currentYInPage = 0;
141
- continue;
142
- }
143
-
144
- // Force at least one row to prevent infinite loop
145
- if (currentRowIndex === startRowIndex) {
146
- chunkHeight += dynamicHeights[currentRowIndex];
147
- currentRowIndex++;
148
- }
149
-
150
- // Create schema for this chunk
151
- const newSchema: Schema = {
152
- ...schema,
153
- height: chunkHeight,
154
- position: { ...schema.position, y: currentYInPage + paddingTop },
155
- };
156
-
157
- // Set bodyRange for splittable elements
158
- // dynamicHeights[0] = header row, dynamicHeights[1] = body[0]
159
- // So subtract 1 to convert to body index
160
- if (isSplittable) {
161
- newSchema.__bodyRange = {
162
- start: startRowIndex === 0 ? 0 : startRowIndex - 1,
163
- end: currentRowIndex - 1,
164
- };
165
- newSchema.__isSplit = startRowIndex > 0;
166
- }
167
-
168
- pages[currentPageIndex].push(newSchema);
169
-
170
- // Update position
171
- currentYInPage += chunkHeight;
172
-
173
- if (currentYInPage >= contentHeight - EPSILON) {
174
- currentPageIndex++;
175
- currentYInPage = 0;
176
- }
177
-
178
- actualGlobalEndY = currentPageIndex * contentHeight + currentYInPage;
179
- }
180
-
181
- return actualGlobalEndY;
182
- }
183
-
184
- /** Sort elements within each page by their original order */
185
- function sortPagesByOrder(pages: Schema[][], orderMap: Map<string, number>): void {
186
- pages.forEach((page) => {
187
- page.sort((a, b) => (orderMap.get(a.name) ?? 0) - (orderMap.get(b.name) ?? 0));
188
- });
189
- }
190
-
191
- /** Remove trailing empty pages */
192
- function removeTrailingEmptyPages(pages: Schema[][]): void {
193
- while (pages.length > 1 && pages[pages.length - 1].length === 0) {
194
- pages.pop();
195
- }
196
- }
197
-
198
- /**
199
- * Process a single template page that has dynamic content.
200
- * Uses the same layout algorithm as the original implementation,
201
- * but scoped to a single page's schemas.
202
- */
203
- function processDynamicPage(
204
- items: LayoutItem[],
205
- orderMap: Map<string, number>,
206
- contentHeight: number,
207
- paddingTop: number,
208
- ): Schema[][] {
209
- const pages: Schema[][] = [];
210
- let totalYOffset = 0;
211
-
212
- for (const item of items) {
213
- const currentGlobalStartY = item.baseY + totalYOffset;
214
-
215
- const actualGlobalEndY = placeRowsOnPages(
216
- item.schema,
217
- item.dynamicHeights,
218
- currentGlobalStartY,
219
- contentHeight,
220
- paddingTop,
221
- pages,
222
- );
223
-
224
- // Update offset: difference between actual and original end position
225
- const originalGlobalEndY = item.baseY + item.height;
226
- totalYOffset = actualGlobalEndY - originalGlobalEndY;
227
- }
228
-
229
- sortPagesByOrder(pages, orderMap);
230
- removeTrailingEmptyPages(pages);
231
-
232
- return pages;
233
- }
234
-
235
- /**
236
- * Process a template containing tables with dynamic heights
237
- * and generate a new template with proper page breaks.
238
- *
239
- * Processing is done page-by-page:
240
- * - Pages with height changes are processed with full layout calculations
241
- * - Pages without height changes are copied as-is (no offset propagation between pages)
242
- *
243
- * This reduces computation cost by:
244
- * 1. Limiting layout calculations to pages that need them
245
- * 2. Avoiding cross-page offset propagation for static pages
246
- */
247
- export const getDynamicTemplate = async (
248
- arg: ModifyTemplateForDynamicTableArg,
249
- ): Promise<Template> => {
250
- const { template, input, options, _cache, getDynamicHeights } = arg;
251
- const basePdf = template.basePdf;
252
-
253
- if (!isBlankPdf(basePdf)) {
254
- return template;
255
- }
256
-
257
- const contentHeight = getContentHeight(basePdf);
258
- const paddingTop = basePdf.padding[0];
259
- const resultPages: Schema[][] = [];
260
- const PARALLEL_LIMIT = 10;
261
-
262
- // Process each template page independently
263
- for (let pageIndex = 0; pageIndex < template.schemas.length; pageIndex++) {
264
- const pageSchemas = template.schemas[pageIndex];
265
-
266
- // Normalize this page's schemas
267
- const { items, orderMap } = normalizePageSchemas(pageSchemas, paddingTop);
268
-
269
- // Calculate dynamic heights for this page's schemas with concurrency limit
270
- for (let i = 0; i < items.length; i += PARALLEL_LIMIT) {
271
- const chunk = items.slice(i, i + PARALLEL_LIMIT);
272
- const chunkResults = await Promise.all(
273
- chunk.map((item) => {
274
- const value = getSchemaValue(item.schema, input);
275
- return getDynamicHeights(value, {
276
- schema: item.schema,
277
- basePdf,
278
- options,
279
- _cache,
280
- }).then((heights) => (heights.length === 0 ? [0] : heights));
281
- }),
282
- );
283
- // Update items with calculated heights
284
- for (let j = 0; j < chunkResults.length; j++) {
285
- items[i + j].dynamicHeights = chunkResults[j];
286
- }
287
- }
288
-
289
- // Process all pages independently (no cross-page offset propagation)
290
- const processedPages = processDynamicPage(items, orderMap, contentHeight, paddingTop);
291
- resultPages.push(...processedPages);
292
- }
293
-
294
- removeTrailingEmptyPages(resultPages);
295
-
296
- // Check if anything changed - return original template if not
297
- if (resultPages.length === template.schemas.length) {
298
- let unchanged = true;
299
- for (let i = 0; i < resultPages.length && unchanged; i++) {
300
- if (resultPages[i].length !== template.schemas[i].length) {
301
- unchanged = false;
302
- break;
303
- }
304
- for (let j = 0; j < resultPages[i].length && unchanged; j++) {
305
- const orig = template.schemas[i][j];
306
- const result = resultPages[i][j];
307
- if (
308
- Math.abs(orig.height - result.height) > EPSILON ||
309
- Math.abs(orig.position.y - result.position.y) > EPSILON
310
- ) {
311
- unchanged = false;
312
- }
313
- }
314
- }
315
- if (unchanged) {
316
- return template;
317
- }
318
- }
319
-
320
- return { basePdf, schemas: resultPages };
321
- };