@pdfme/common 6.1.0 → 6.1.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.
- package/dist/dynamicTemplate.d.ts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +51 -27
- package/dist/index.js.map +1 -1
- package/dist/schema.d.ts +95 -0
- package/dist/types.d.ts +20 -0
- package/dist/version.d.ts +1 -1
- package/package.json +1 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Schema, Template, BasePdf, CommonOptions } from './types.js';
|
|
1
|
+
import { Schema, Template, BasePdf, CommonOptions, DynamicLayoutCallbackResult } from './types.js';
|
|
2
2
|
interface ModifyTemplateForDynamicTableArg {
|
|
3
3
|
template: Template;
|
|
4
4
|
input: Record<string, string>;
|
|
@@ -9,7 +9,7 @@ interface ModifyTemplateForDynamicTableArg {
|
|
|
9
9
|
basePdf: BasePdf;
|
|
10
10
|
options: CommonOptions;
|
|
11
11
|
_cache: Map<string | number, unknown>;
|
|
12
|
-
}) => Promise<
|
|
12
|
+
}) => Promise<DynamicLayoutCallbackResult>;
|
|
13
13
|
}
|
|
14
14
|
/**
|
|
15
15
|
* Process a template containing tables with dynamic heights
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { PDFME_VERSION } from './version.js';
|
|
2
2
|
import { MM_TO_PT_RATIO, PT_TO_MM_RATIO, PT_TO_PX_RATIO, BLANK_PDF, BLANK_A4_PDF, CUSTOM_A4_PDF, ZOOM, DEFAULT_FONT_NAME } from './constants.js';
|
|
3
|
-
import type { ChangeSchemaItem, ChangeSchemas, SchemaPageArray, PropPanel, PropPanelSchema, PropPanelWidgetProps, PDFRenderProps, Mode, UIRenderProps, Plugin, Lang, Dict, Size, Schema, SchemaForUI, Font, ColorType, BasePdf, BlankPdf, CustomPdf, Template, CommonOptions, GeneratorOptions, Plugins, PluginRegistry, GenerateProps, UIOptions, UIProps, PreviewProps, DesignerProps } from './types.js';
|
|
3
|
+
import type { ChangeSchemaItem, ChangeSchemas, SchemaPageArray, PropPanel, PropPanelSchema, PropPanelWidgetProps, PDFRenderProps, Mode, UIRenderProps, Plugin, Lang, Dict, Size, Schema, SchemaForUI, Font, ColorType, BasePdf, BlankPdf, CustomPdf, Template, CommonOptions, GeneratorOptions, Plugins, PluginRegistry, GenerateProps, UIOptions, UIProps, PreviewProps, DesignerProps, DynamicLayoutArgs, DynamicLayoutCallbackResult, DynamicLayoutPatchArgs, DynamicLayoutResult, GetDynamicLayout } from './types.js';
|
|
4
4
|
import { cloneDeep, getFallbackFontName, getDefaultFont, getB64BasePdf, b64toUint8Array, checkFont, checkInputs, checkUIOptions, checkTemplate, checkUIProps, checkPreviewProps, checkDesignerProps, checkGenerateProps, mm2pt, pt2mm, pt2px, px2mm, isHexValid, getInputFromTemplate, isBlankPdf, isUrlSafeToFetch } from './helper.js';
|
|
5
5
|
import { getDynamicTemplate } from './dynamicTemplate.js';
|
|
6
6
|
import { replacePlaceholders } from './expression.js';
|
|
7
7
|
import { pluginRegistry } from './pluginRegistry.js';
|
|
8
8
|
export { PDFME_VERSION, MM_TO_PT_RATIO, PT_TO_MM_RATIO, PT_TO_PX_RATIO, BLANK_PDF, BLANK_A4_PDF, CUSTOM_A4_PDF, ZOOM, DEFAULT_FONT_NAME, cloneDeep, getFallbackFontName, getDefaultFont, getB64BasePdf, b64toUint8Array, mm2pt, pt2mm, pt2px, px2mm, isHexValid, getInputFromTemplate, isBlankPdf, getDynamicTemplate, replacePlaceholders, checkFont, checkInputs, checkUIOptions, checkTemplate, checkUIProps, checkPreviewProps, checkDesignerProps, checkGenerateProps, pluginRegistry, isUrlSafeToFetch, };
|
|
9
|
-
export type { Lang, Dict, Size, Schema, SchemaForUI, Font, ColorType, BasePdf, BlankPdf, CustomPdf, Template, CommonOptions, GeneratorOptions, Plugin, Plugins, PluginRegistry, GenerateProps, UIOptions, UIProps, PreviewProps, DesignerProps, ChangeSchemaItem, ChangeSchemas, SchemaPageArray, PropPanel, PropPanelSchema, PropPanelWidgetProps, PDFRenderProps, UIRenderProps, Mode, };
|
|
9
|
+
export type { Lang, Dict, Size, Schema, SchemaForUI, Font, ColorType, BasePdf, BlankPdf, CustomPdf, Template, CommonOptions, GeneratorOptions, Plugin, Plugins, PluginRegistry, GenerateProps, UIOptions, UIProps, PreviewProps, DesignerProps, ChangeSchemaItem, ChangeSchemas, SchemaPageArray, PropPanel, PropPanelSchema, PropPanelWidgetProps, PDFRenderProps, UIRenderProps, Mode, DynamicLayoutArgs, DynamicLayoutCallbackResult, DynamicLayoutPatchArgs, DynamicLayoutResult, GetDynamicLayout, };
|
package/dist/index.js
CHANGED
|
@@ -2,7 +2,7 @@ import { z } from "zod";
|
|
|
2
2
|
import { Buffer } from "buffer";
|
|
3
3
|
import * as acorn from "acorn";
|
|
4
4
|
//#region src/version.ts
|
|
5
|
-
var PDFME_VERSION = "6.1.
|
|
5
|
+
var PDFME_VERSION = "6.1.1";
|
|
6
6
|
//#endregion
|
|
7
7
|
//#region src/constants.ts
|
|
8
8
|
var PT_TO_PX_RATIO = 1.333;
|
|
@@ -133,7 +133,18 @@ z.object({
|
|
|
133
133
|
"schemas.date.locale": z.string(),
|
|
134
134
|
"schemas.select.options": z.string(),
|
|
135
135
|
"schemas.select.optionPlaceholder": z.string(),
|
|
136
|
-
"schemas.radioGroup.groupName": z.string()
|
|
136
|
+
"schemas.radioGroup.groupName": z.string(),
|
|
137
|
+
"schemas.list.listStyle": z.string(),
|
|
138
|
+
"schemas.list.bullet": z.string(),
|
|
139
|
+
"schemas.list.ordered": z.string(),
|
|
140
|
+
"schemas.list.markerWidth": z.string(),
|
|
141
|
+
"schemas.list.markerGap": z.string(),
|
|
142
|
+
"schemas.list.indentSize": z.string(),
|
|
143
|
+
"schemas.list.itemSpacing": z.string(),
|
|
144
|
+
"schemas.list.addItem": z.string(),
|
|
145
|
+
"schemas.list.removeItem": z.string(),
|
|
146
|
+
"schemas.list.indentItem": z.string(),
|
|
147
|
+
"schemas.list.outdentItem": z.string()
|
|
137
148
|
});
|
|
138
149
|
z.enum([
|
|
139
150
|
"viewer",
|
|
@@ -163,6 +174,10 @@ var Schema = z.object({
|
|
|
163
174
|
start: z.number(),
|
|
164
175
|
end: z.number().optional()
|
|
165
176
|
}).optional(),
|
|
177
|
+
__itemRange: z.object({
|
|
178
|
+
start: z.number(),
|
|
179
|
+
end: z.number().optional()
|
|
180
|
+
}).optional(),
|
|
166
181
|
__isSplit: z.boolean().optional()
|
|
167
182
|
}).passthrough();
|
|
168
183
|
var SchemaForUIAdditionalInfo = z.object({ id: z.string() });
|
|
@@ -451,7 +466,7 @@ function normalizePageSchemas(pageSchemas, paddingTop) {
|
|
|
451
466
|
schema: cloneDeep(schema),
|
|
452
467
|
baseY: localY,
|
|
453
468
|
height: schema.height,
|
|
454
|
-
|
|
469
|
+
dynamicLayout: { heights: [schema.height] }
|
|
455
470
|
});
|
|
456
471
|
orderMap.set(schema.name, index);
|
|
457
472
|
});
|
|
@@ -465,20 +480,21 @@ function normalizePageSchemas(pageSchemas, paddingTop) {
|
|
|
465
480
|
};
|
|
466
481
|
}
|
|
467
482
|
/**
|
|
468
|
-
* Place
|
|
483
|
+
* Place height units on pages, splitting across pages as needed.
|
|
469
484
|
* @returns The final global Y coordinate after placement
|
|
470
485
|
*/
|
|
471
|
-
function
|
|
472
|
-
|
|
486
|
+
function placeUnitsOnPages(schema, dynamicLayout, startGlobalY, contentHeight, paddingTop, pages) {
|
|
487
|
+
const dynamicHeights = dynamicLayout.heights;
|
|
488
|
+
let currentUnitIndex = 0;
|
|
473
489
|
let currentPageIndex = Math.floor(startGlobalY / contentHeight);
|
|
474
490
|
let currentYInPage = startGlobalY % contentHeight;
|
|
475
491
|
if (currentYInPage < 0) currentYInPage = 0;
|
|
476
492
|
let actualGlobalEndY = 0;
|
|
477
493
|
const isSplittable = dynamicHeights.length > 1;
|
|
478
|
-
while (
|
|
494
|
+
while (currentUnitIndex < dynamicHeights.length) {
|
|
479
495
|
while (pages.length <= currentPageIndex) pages.push([]);
|
|
480
496
|
const spaceLeft = contentHeight - currentYInPage;
|
|
481
|
-
if (dynamicHeights[
|
|
497
|
+
if (dynamicHeights[currentUnitIndex] > spaceLeft + EPSILON) {
|
|
482
498
|
if (!(Math.abs(spaceLeft - contentHeight) <= EPSILON)) {
|
|
483
499
|
currentPageIndex++;
|
|
484
500
|
currentYInPage = 0;
|
|
@@ -486,40 +502,41 @@ function placeRowsOnPages(schema, dynamicHeights, startGlobalY, contentHeight, p
|
|
|
486
502
|
}
|
|
487
503
|
}
|
|
488
504
|
let chunkHeight = 0;
|
|
489
|
-
const
|
|
490
|
-
while (
|
|
491
|
-
const h = dynamicHeights[
|
|
505
|
+
const startUnitIndex = currentUnitIndex;
|
|
506
|
+
while (currentUnitIndex < dynamicHeights.length) {
|
|
507
|
+
const h = dynamicHeights[currentUnitIndex];
|
|
492
508
|
if (currentYInPage + chunkHeight + h <= contentHeight + EPSILON) {
|
|
493
509
|
chunkHeight += h;
|
|
494
|
-
|
|
510
|
+
currentUnitIndex++;
|
|
495
511
|
} else break;
|
|
496
512
|
}
|
|
497
513
|
const isAtPageTop = currentYInPage <= EPSILON;
|
|
498
|
-
if (isSplittable &&
|
|
499
|
-
|
|
514
|
+
if (dynamicLayout.avoidFirstUnitOnly && isSplittable && startUnitIndex === 0 && currentUnitIndex === 1 && dynamicHeights.length > 1 && !isAtPageTop) {
|
|
515
|
+
currentUnitIndex = 0;
|
|
500
516
|
currentPageIndex++;
|
|
501
517
|
currentYInPage = 0;
|
|
502
518
|
continue;
|
|
503
519
|
}
|
|
504
|
-
if (
|
|
505
|
-
chunkHeight += dynamicHeights[
|
|
506
|
-
|
|
520
|
+
if (currentUnitIndex === startUnitIndex) {
|
|
521
|
+
chunkHeight += dynamicHeights[currentUnitIndex];
|
|
522
|
+
currentUnitIndex++;
|
|
507
523
|
}
|
|
524
|
+
const patch = dynamicLayout.patchSplitSchema?.({
|
|
525
|
+
schema,
|
|
526
|
+
start: startUnitIndex,
|
|
527
|
+
end: currentUnitIndex,
|
|
528
|
+
isSplit: startUnitIndex > 0,
|
|
529
|
+
chunkHeight
|
|
530
|
+
}) ?? {};
|
|
508
531
|
const newSchema = {
|
|
509
532
|
...schema,
|
|
533
|
+
...patch,
|
|
510
534
|
height: chunkHeight,
|
|
511
535
|
position: {
|
|
512
536
|
...schema.position,
|
|
513
537
|
y: currentYInPage + paddingTop
|
|
514
538
|
}
|
|
515
539
|
};
|
|
516
|
-
if (isSplittable) {
|
|
517
|
-
newSchema.__bodyRange = {
|
|
518
|
-
start: startRowIndex === 0 ? 0 : startRowIndex - 1,
|
|
519
|
-
end: currentRowIndex - 1
|
|
520
|
-
};
|
|
521
|
-
newSchema.__isSplit = startRowIndex > 0;
|
|
522
|
-
}
|
|
523
540
|
pages[currentPageIndex].push(newSchema);
|
|
524
541
|
currentYInPage += chunkHeight;
|
|
525
542
|
if (currentYInPage >= contentHeight - EPSILON) {
|
|
@@ -550,12 +567,19 @@ function processDynamicPage(items, orderMap, contentHeight, paddingTop) {
|
|
|
550
567
|
let totalYOffset = 0;
|
|
551
568
|
for (const item of items) {
|
|
552
569
|
const currentGlobalStartY = item.baseY + totalYOffset;
|
|
553
|
-
totalYOffset =
|
|
570
|
+
totalYOffset = placeUnitsOnPages(item.schema, item.dynamicLayout, currentGlobalStartY, contentHeight, paddingTop, pages) - (item.baseY + item.height);
|
|
554
571
|
}
|
|
555
572
|
sortPagesByOrder(pages, orderMap);
|
|
556
573
|
removeTrailingEmptyPages(pages);
|
|
557
574
|
return pages;
|
|
558
575
|
}
|
|
576
|
+
var normalizeDynamicLayoutResult = (result) => {
|
|
577
|
+
const dynamicLayout = Array.isArray(result) ? { heights: result } : result;
|
|
578
|
+
return {
|
|
579
|
+
...dynamicLayout,
|
|
580
|
+
heights: dynamicLayout.heights.length === 0 ? [0] : dynamicLayout.heights
|
|
581
|
+
};
|
|
582
|
+
};
|
|
559
583
|
/**
|
|
560
584
|
* Process a template containing tables with dynamic heights
|
|
561
585
|
* and generate a new template with proper page breaks.
|
|
@@ -587,9 +611,9 @@ var getDynamicTemplate = async (arg) => {
|
|
|
587
611
|
basePdf,
|
|
588
612
|
options,
|
|
589
613
|
_cache
|
|
590
|
-
}).then(
|
|
614
|
+
}).then(normalizeDynamicLayoutResult);
|
|
591
615
|
}));
|
|
592
|
-
for (let j = 0; j < chunkResults.length; j++) items[i + j].
|
|
616
|
+
for (let j = 0; j < chunkResults.length; j++) items[i + j].dynamicLayout = chunkResults[j];
|
|
593
617
|
}
|
|
594
618
|
const processedPages = processDynamicPage(items, orderMap, contentHeight, paddingTop);
|
|
595
619
|
resultPages.push(...processedPages);
|