@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.
- package/dist/cjs/__tests__/dynamicTemplate.test.js +197 -0
- package/dist/cjs/__tests__/dynamicTemplate.test.js.map +1 -0
- package/dist/cjs/__tests__/expression.test.js +271 -0
- package/dist/cjs/__tests__/expression.test.js.map +1 -0
- package/dist/cjs/__tests__/helper.test.js +92 -254
- package/dist/cjs/__tests__/helper.test.js.map +1 -1
- package/dist/cjs/src/constants.js +6 -0
- package/dist/cjs/src/constants.js.map +1 -1
- package/dist/cjs/src/dynamicTemplate.js +239 -0
- package/dist/cjs/src/dynamicTemplate.js.map +1 -0
- package/dist/cjs/src/expression.js +392 -0
- package/dist/cjs/src/expression.js.map +1 -0
- package/dist/cjs/src/helper.js +6 -238
- package/dist/cjs/src/helper.js.map +1 -1
- package/dist/cjs/src/index.js +34 -31
- package/dist/cjs/src/index.js.map +1 -1
- package/dist/cjs/src/schema.js +1 -0
- package/dist/cjs/src/schema.js.map +1 -1
- package/dist/esm/__tests__/dynamicTemplate.test.js +172 -0
- package/dist/esm/__tests__/dynamicTemplate.test.js.map +1 -0
- package/dist/esm/__tests__/expression.test.js +269 -0
- package/dist/esm/__tests__/expression.test.js.map +1 -0
- package/dist/esm/__tests__/helper.test.js +94 -256
- package/dist/esm/__tests__/helper.test.js.map +1 -1
- package/dist/esm/src/constants.js +6 -0
- package/dist/esm/src/constants.js.map +1 -1
- package/dist/esm/src/dynamicTemplate.js +235 -0
- package/dist/esm/src/dynamicTemplate.js.map +1 -0
- package/dist/esm/src/expression.js +365 -0
- package/dist/esm/src/expression.js.map +1 -0
- package/dist/esm/src/helper.js +5 -236
- package/dist/esm/src/helper.js.map +1 -1
- package/dist/esm/src/index.js +5 -3
- package/dist/esm/src/index.js.map +1 -1
- package/dist/esm/src/schema.js +1 -0
- package/dist/esm/src/schema.js.map +1 -1
- package/dist/types/__tests__/dynamicTemplate.test.d.ts +1 -0
- package/dist/types/__tests__/expression.test.d.ts +1 -0
- package/dist/types/src/dynamicTemplate.d.ts +15 -0
- package/dist/types/src/expression.d.ts +6 -0
- package/dist/types/src/helper.d.ts +34 -15
- package/dist/types/src/index.d.ts +6 -4
- package/dist/types/src/schema.d.ts +3631 -517
- package/dist/types/src/types.d.ts +1 -1
- package/package.json +5 -1
- package/src/constants.ts +8 -0
- package/src/dynamicTemplate.ts +277 -0
- package/src/expression.ts +392 -0
- package/src/helper.ts +10 -282
- package/src/index.ts +6 -4
- package/src/schema.ts +1 -0
- package/src/types.ts +1 -1
@@ -73,7 +73,7 @@ export type UIRenderProps<T extends Schema> = {
|
|
73
73
|
options: UIOptions;
|
74
74
|
theme: GlobalToken;
|
75
75
|
i18n: (key: keyof Dict | string) => string;
|
76
|
-
pdfJs: typeof import('pdfjs-dist
|
76
|
+
pdfJs: typeof import('pdfjs-dist');
|
77
77
|
_cache: Map<any, any>;
|
78
78
|
};
|
79
79
|
/**
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@pdfme/common",
|
3
|
-
"version": "5.1.
|
3
|
+
"version": "5.1.7-dev.2",
|
4
4
|
"sideEffects": false,
|
5
5
|
"author": "hand-dot",
|
6
6
|
"license": "MIT",
|
@@ -44,6 +44,7 @@
|
|
44
44
|
},
|
45
45
|
"dependencies": {
|
46
46
|
"@pdfme/pdf-lib": "^1.18.3",
|
47
|
+
"acorn": "^8.14.0",
|
47
48
|
"buffer": "^6.0.3",
|
48
49
|
"zod": "^3.20.2"
|
49
50
|
},
|
@@ -71,5 +72,8 @@
|
|
71
72
|
},
|
72
73
|
"publishConfig": {
|
73
74
|
"access": "public"
|
75
|
+
},
|
76
|
+
"devDependencies": {
|
77
|
+
"@types/estree": "^1.0.6"
|
74
78
|
}
|
75
79
|
}
|
package/src/constants.ts
CHANGED
@@ -4,6 +4,14 @@ export const PT_TO_PX_RATIO = 1.333;
|
|
4
4
|
export const PT_TO_MM_RATIO = 0.3528;
|
5
5
|
export const MM_TO_PT_RATIO = 2.8346; // https://www.ddc.co.jp/words/archives/20090701114500.html
|
6
6
|
export const ZOOM = 3.7795275591;
|
7
|
+
|
8
|
+
// TODO replace in the future
|
9
|
+
// export const BLANK_PDF: BlankPdf = {
|
10
|
+
// width: 210,
|
11
|
+
// height: 297,
|
12
|
+
// padding: [10, 10, 10, 10],
|
13
|
+
// };
|
14
|
+
|
7
15
|
export const BLANK_PDF =
|
8
16
|
'data:application/pdf;base64,JVBERi0xLjcKJeLjz9MKNSAwIG9iago8PAovRmlsdGVyIC9GbGF0ZURlY29kZQovTGVuZ3RoIDM4Cj4+CnN0cmVhbQp4nCvkMlAwUDC1NNUzMVGwMDHUszRSKErlCtfiyuMK5AIAXQ8GCgplbmRzdHJlYW0KZW5kb2JqCjQgMCBvYmoKPDwKL1R5cGUgL1BhZ2UKL01lZGlhQm94IFswIDAgNTk1LjQ0IDg0MS45Ml0KL1Jlc291cmNlcyA8PAo+PgovQ29udGVudHMgNSAwIFIKL1BhcmVudCAyIDAgUgo+PgplbmRvYmoKMiAwIG9iago8PAovVHlwZSAvUGFnZXMKL0tpZHMgWzQgMCBSXQovQ291bnQgMQo+PgplbmRvYmoKMSAwIG9iago8PAovVHlwZSAvQ2F0YWxvZwovUGFnZXMgMiAwIFIKPj4KZW5kb2JqCjMgMCBvYmoKPDwKL3RyYXBwZWQgKGZhbHNlKQovQ3JlYXRvciAoU2VyaWYgQWZmaW5pdHkgRGVzaWduZXIgMS4xMC40KQovVGl0bGUgKFVudGl0bGVkLnBkZikKL0NyZWF0aW9uRGF0ZSAoRDoyMDIyMDEwNjE0MDg1OCswOScwMCcpCi9Qcm9kdWNlciAoaUxvdmVQREYpCi9Nb2REYXRlIChEOjIwMjIwMTA2MDUwOTA5WikKPj4KZW5kb2JqCjYgMCBvYmoKPDwKL1NpemUgNwovUm9vdCAxIDAgUgovSW5mbyAzIDAgUgovSUQgWzwyODhCM0VENTAyOEU0MDcyNERBNzNCOUE0Nzk4OUEwQT4gPEY1RkJGNjg4NkVERDZBQUNBNDRCNEZDRjBBRDUxRDlDPl0KL1R5cGUgL1hSZWYKL1cgWzEgMiAyXQovRmlsdGVyIC9GbGF0ZURlY29kZQovSW5kZXggWzAgN10KL0xlbmd0aCAzNgo+PgpzdHJlYW0KeJxjYGD4/5+RUZmBgZHhFZBgDAGxakAEP5BgEmFgAABlRwQJCmVuZHN0cmVhbQplbmRvYmoKc3RhcnR4cmVmCjUzMgolJUVPRgo=';
|
9
17
|
export const DEFAULT_FONT_NAME = 'Roboto';
|
@@ -0,0 +1,277 @@
|
|
1
|
+
import { Schema, Template, BasePdf, BlankPdf, CommonOptions } from './types';
|
2
|
+
import { cloneDeep, isBlankPdf } from './helper';
|
3
|
+
|
4
|
+
interface ModifyTemplateForDynamicTableArg {
|
5
|
+
template: Template;
|
6
|
+
input: Record<string, string>;
|
7
|
+
_cache: Map<any, any>;
|
8
|
+
options: CommonOptions;
|
9
|
+
getDynamicHeights: (
|
10
|
+
value: string,
|
11
|
+
args: { schema: Schema; basePdf: BasePdf; options: CommonOptions; _cache: Map<any, any> }
|
12
|
+
) => Promise<number[]>;
|
13
|
+
}
|
14
|
+
|
15
|
+
class LayoutNode {
|
16
|
+
index = 0;
|
17
|
+
|
18
|
+
schema?: Schema;
|
19
|
+
|
20
|
+
children: LayoutNode[] = [];
|
21
|
+
|
22
|
+
width = 0;
|
23
|
+
height = 0;
|
24
|
+
padding: [number, number, number, number] = [0, 0, 0, 0];
|
25
|
+
position: { x: number; y: number } = { x: 0, y: 0 };
|
26
|
+
|
27
|
+
constructor({ width = 0, height = 0 } = {}) {
|
28
|
+
this.width = width;
|
29
|
+
this.height = height;
|
30
|
+
}
|
31
|
+
|
32
|
+
setIndex(index: number): void {
|
33
|
+
this.index = index;
|
34
|
+
}
|
35
|
+
|
36
|
+
setSchema(schema: Schema): void {
|
37
|
+
this.schema = schema;
|
38
|
+
}
|
39
|
+
|
40
|
+
setWidth(width: number): void {
|
41
|
+
this.width = width;
|
42
|
+
}
|
43
|
+
|
44
|
+
setHeight(height: number): void {
|
45
|
+
this.height = height;
|
46
|
+
}
|
47
|
+
|
48
|
+
setPadding(padding: [number, number, number, number]): void {
|
49
|
+
this.padding = padding;
|
50
|
+
}
|
51
|
+
|
52
|
+
setPosition(position: { x: number; y: number }): void {
|
53
|
+
this.position = position;
|
54
|
+
}
|
55
|
+
|
56
|
+
insertChild(child: LayoutNode): void {
|
57
|
+
const index = this.getChildCount();
|
58
|
+
child.setIndex(index);
|
59
|
+
this.children.splice(index, 0, child);
|
60
|
+
}
|
61
|
+
|
62
|
+
getChildCount(): number {
|
63
|
+
return this.children.length;
|
64
|
+
}
|
65
|
+
|
66
|
+
getChild(index: number): LayoutNode {
|
67
|
+
return this.children[index];
|
68
|
+
}
|
69
|
+
}
|
70
|
+
|
71
|
+
function createPage(basePdf: BlankPdf) {
|
72
|
+
const page = new LayoutNode({ ...basePdf });
|
73
|
+
page.setPadding(basePdf.padding);
|
74
|
+
return page;
|
75
|
+
}
|
76
|
+
|
77
|
+
function createNode(arg: {
|
78
|
+
schema: Schema;
|
79
|
+
position: { x: number; y: number };
|
80
|
+
width: number;
|
81
|
+
height: number;
|
82
|
+
}) {
|
83
|
+
const { position, width, height, schema } = arg;
|
84
|
+
const node = new LayoutNode({ width, height });
|
85
|
+
node.setPosition(position);
|
86
|
+
node.setSchema(schema);
|
87
|
+
return node;
|
88
|
+
}
|
89
|
+
|
90
|
+
function resortChildren(page: LayoutNode, orderMap: Map<string, number>): void {
|
91
|
+
page.children = page.children
|
92
|
+
.sort((a, b) => {
|
93
|
+
const orderA = orderMap.get(a.schema?.name!);
|
94
|
+
const orderB = orderMap.get(b.schema?.name!);
|
95
|
+
if (orderA === undefined || orderB === undefined) {
|
96
|
+
throw new Error('[@pdfme/common] order is not defined');
|
97
|
+
}
|
98
|
+
return orderA - orderB;
|
99
|
+
})
|
100
|
+
.map((child, index) => {
|
101
|
+
child.setIndex(index);
|
102
|
+
return child;
|
103
|
+
});
|
104
|
+
}
|
105
|
+
|
106
|
+
async function createOnePage(
|
107
|
+
arg: {
|
108
|
+
basePdf: BlankPdf;
|
109
|
+
schemaPage: Schema[];
|
110
|
+
orderMap: Map<string, number>;
|
111
|
+
} & Omit<ModifyTemplateForDynamicTableArg, 'template'>
|
112
|
+
): Promise<LayoutNode> {
|
113
|
+
const { basePdf, schemaPage, orderMap, input, options, _cache, getDynamicHeights } = arg;
|
114
|
+
const page = createPage(basePdf);
|
115
|
+
|
116
|
+
const schemaPositions: number[] = [];
|
117
|
+
const sortedSchemaEntries = cloneDeep(schemaPage).sort((a, b) => a.position.y - b.position.y);
|
118
|
+
const diffMap = new Map();
|
119
|
+
for (const schema of sortedSchemaEntries) {
|
120
|
+
const { position, width } = schema;
|
121
|
+
|
122
|
+
const opt = { schema, basePdf, options, _cache };
|
123
|
+
const value = (schema.readOnly ? schema.content : input?.[schema.name]) || '';
|
124
|
+
const heights = await getDynamicHeights(value, opt);
|
125
|
+
|
126
|
+
const heightsSum = heights.reduce((acc, cur) => acc + cur, 0);
|
127
|
+
const originalHeight = schema.height;
|
128
|
+
if (heightsSum !== originalHeight) {
|
129
|
+
diffMap.set(position.y + originalHeight, heightsSum - originalHeight);
|
130
|
+
}
|
131
|
+
heights.forEach((height, index) => {
|
132
|
+
let y = schema.position.y + heights.reduce((acc, cur, i) => (i < index ? acc + cur : acc), 0);
|
133
|
+
for (const [diffY, diff] of diffMap.entries()) {
|
134
|
+
if (diffY <= schema.position.y) {
|
135
|
+
y += diff;
|
136
|
+
}
|
137
|
+
}
|
138
|
+
const node = createNode({ schema, position: { ...position, y }, width, height });
|
139
|
+
|
140
|
+
schemaPositions.push(y + height + basePdf.padding[2]);
|
141
|
+
page.insertChild(node);
|
142
|
+
});
|
143
|
+
}
|
144
|
+
|
145
|
+
const pageHeight = Math.max(...schemaPositions, basePdf.height - basePdf.padding[2]);
|
146
|
+
page.setHeight(pageHeight);
|
147
|
+
|
148
|
+
resortChildren(page, orderMap);
|
149
|
+
|
150
|
+
return page;
|
151
|
+
}
|
152
|
+
|
153
|
+
function breakIntoPages(arg: {
|
154
|
+
longPage: LayoutNode;
|
155
|
+
orderMap: Map<string, number>;
|
156
|
+
basePdf: BlankPdf;
|
157
|
+
}): LayoutNode[] {
|
158
|
+
const { longPage, orderMap, basePdf } = arg;
|
159
|
+
const pages: LayoutNode[] = [createPage(basePdf)];
|
160
|
+
const [paddingTop, , paddingBottom] = basePdf.padding;
|
161
|
+
const yAdjustments: { page: number; value: number }[] = [];
|
162
|
+
|
163
|
+
const getPageHeight = (pageIndex: number) =>
|
164
|
+
basePdf.height - paddingBottom - (pageIndex > 0 ? paddingTop : 0);
|
165
|
+
|
166
|
+
const calculateNewY = (y: number, pageIndex: number) => {
|
167
|
+
const newY = y - pageIndex * (basePdf.height - paddingTop - paddingBottom);
|
168
|
+
|
169
|
+
while (pages.length <= pageIndex) {
|
170
|
+
if (!pages[pageIndex]) {
|
171
|
+
pages.push(createPage(basePdf));
|
172
|
+
yAdjustments.push({ page: pageIndex, value: (newY - paddingTop) * -1 });
|
173
|
+
}
|
174
|
+
}
|
175
|
+
return newY + (yAdjustments.find((adj) => adj.page === pageIndex)?.value || 0);
|
176
|
+
};
|
177
|
+
|
178
|
+
const children = longPage.children.sort((a, b) => a.position.y - b.position.y);
|
179
|
+
for (let i = 0; i < children.length; i++) {
|
180
|
+
const { schema, position, height, width } = children[i];
|
181
|
+
const { y, x } = position;
|
182
|
+
|
183
|
+
let targetPageIndex = Math.floor(y / getPageHeight(pages.length - 1));
|
184
|
+
let newY = calculateNewY(y, targetPageIndex);
|
185
|
+
|
186
|
+
if (newY + height > basePdf.height - paddingBottom) {
|
187
|
+
targetPageIndex++;
|
188
|
+
newY = calculateNewY(y, targetPageIndex);
|
189
|
+
}
|
190
|
+
|
191
|
+
if (!schema) throw new Error('[@pdfme/common] schema is undefined');
|
192
|
+
|
193
|
+
const clonedElement = createNode({ schema, position: { x, y: newY }, width, height });
|
194
|
+
pages[targetPageIndex].insertChild(clonedElement);
|
195
|
+
}
|
196
|
+
|
197
|
+
pages.forEach((page) => resortChildren(page, orderMap));
|
198
|
+
|
199
|
+
return pages;
|
200
|
+
}
|
201
|
+
|
202
|
+
function createNewTemplate(pages: LayoutNode[], basePdf: BlankPdf): Template {
|
203
|
+
const newTemplate: Template = {
|
204
|
+
schemas: Array.from({ length: pages.length }, () => [] as Schema[]),
|
205
|
+
basePdf: basePdf,
|
206
|
+
};
|
207
|
+
|
208
|
+
const nameToSchemas = new Map<string, LayoutNode[]>();
|
209
|
+
|
210
|
+
cloneDeep(pages).forEach((page, pageIndex) => {
|
211
|
+
page.children.forEach((child) => {
|
212
|
+
const { schema } = child;
|
213
|
+
if (!schema) throw new Error('[@pdfme/common] schema is undefined');
|
214
|
+
|
215
|
+
const name = schema.name;
|
216
|
+
if (!nameToSchemas.has(name)) {
|
217
|
+
nameToSchemas.set(name, []);
|
218
|
+
}
|
219
|
+
nameToSchemas.get(name)!.push(child);
|
220
|
+
|
221
|
+
const sameNameSchemas = page.children.filter((c) => c.schema?.name === name);
|
222
|
+
const start = nameToSchemas.get(name)!.length - sameNameSchemas.length;
|
223
|
+
|
224
|
+
if (sameNameSchemas.length > 0) {
|
225
|
+
if (!sameNameSchemas[0].schema) {
|
226
|
+
throw new Error('[@pdfme/common] schema is undefined');
|
227
|
+
}
|
228
|
+
|
229
|
+
// Use the first schema to get the schema and position
|
230
|
+
const schema = sameNameSchemas[0].schema;
|
231
|
+
const height = sameNameSchemas.reduce((acc, cur) => acc + cur.height, 0);
|
232
|
+
const position = sameNameSchemas[0].position;
|
233
|
+
|
234
|
+
// Currently, __bodyRange exists for table schemas, but if we make it more abstract,
|
235
|
+
// it could be used for other schemas as well to render schemas that have been split by page breaks, starting from the middle.
|
236
|
+
schema.__bodyRange = {
|
237
|
+
start: Math.max(start - 1, 0),
|
238
|
+
end: start + sameNameSchemas.length - 1,
|
239
|
+
};
|
240
|
+
|
241
|
+
// Currently, this is used to determine whether to display the header when a table is split.
|
242
|
+
schema.__isSplit = start > 0;
|
243
|
+
|
244
|
+
const newSchema = Object.assign({}, schema, { position, height });
|
245
|
+
const index = newTemplate.schemas[pageIndex].findIndex((s) => s.name === name);
|
246
|
+
if (index !== -1) {
|
247
|
+
newTemplate.schemas[pageIndex][index] = newSchema;
|
248
|
+
} else {
|
249
|
+
newTemplate.schemas[pageIndex].push(newSchema);
|
250
|
+
}
|
251
|
+
}
|
252
|
+
});
|
253
|
+
});
|
254
|
+
|
255
|
+
return newTemplate;
|
256
|
+
}
|
257
|
+
|
258
|
+
export const getDynamicTemplate = async (
|
259
|
+
arg: ModifyTemplateForDynamicTableArg
|
260
|
+
): Promise<Template> => {
|
261
|
+
const { template } = arg;
|
262
|
+
if (!isBlankPdf(template.basePdf)) {
|
263
|
+
return template;
|
264
|
+
}
|
265
|
+
|
266
|
+
const basePdf = template.basePdf as BlankPdf;
|
267
|
+
const pages: LayoutNode[] = [];
|
268
|
+
|
269
|
+
for (const schemaPage of template.schemas) {
|
270
|
+
const orderMap = new Map(schemaPage.map((schema, index) => [schema.name, index]));
|
271
|
+
const longPage = await createOnePage({ basePdf, schemaPage, orderMap, ...arg });
|
272
|
+
const brokenPages = breakIntoPages({ longPage, basePdf, orderMap });
|
273
|
+
pages.push(...brokenPages);
|
274
|
+
}
|
275
|
+
|
276
|
+
return createNewTemplate(pages, template.basePdf);
|
277
|
+
};
|