@pdfme/common 0.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.
- package/README.md +5 -0
- package/dist/cjs/__tests__/dynamicTemplate.test.js +275 -0
- package/dist/cjs/__tests__/dynamicTemplate.test.js.map +1 -0
- package/dist/cjs/__tests__/expression.test.js +474 -0
- package/dist/cjs/__tests__/expression.test.js.map +1 -0
- package/dist/cjs/__tests__/helper.test.js +647 -0
- package/dist/cjs/__tests__/helper.test.js.map +1 -0
- package/dist/cjs/__tests__/pluginRegistry.test.js +83 -0
- package/dist/cjs/__tests__/pluginRegistry.test.js.map +1 -0
- package/dist/cjs/src/constants.js +27 -0
- package/dist/cjs/src/constants.js.map +1 -0
- package/dist/cjs/src/dynamicTemplate.js +284 -0
- package/dist/cjs/src/dynamicTemplate.js.map +1 -0
- package/dist/cjs/src/expression.js +463 -0
- package/dist/cjs/src/expression.js.map +1 -0
- package/dist/cjs/src/helper.js +217 -0
- package/dist/cjs/src/helper.js.map +1 -0
- package/dist/cjs/src/index.js +42 -0
- package/dist/cjs/src/index.js.map +1 -0
- package/dist/cjs/src/pluginRegistry.js +33 -0
- package/dist/cjs/src/pluginRegistry.js.map +1 -0
- package/dist/cjs/src/schema.js +190 -0
- package/dist/cjs/src/schema.js.map +1 -0
- package/dist/cjs/src/types.js +3 -0
- package/dist/cjs/src/types.js.map +1 -0
- package/dist/cjs/src/version.js +5 -0
- package/dist/cjs/src/version.js.map +1 -0
- package/dist/esm/__tests__/dynamicTemplate.test.js +240 -0
- package/dist/esm/__tests__/dynamicTemplate.test.js.map +1 -0
- package/dist/esm/__tests__/expression.test.js +472 -0
- package/dist/esm/__tests__/expression.test.js.map +1 -0
- package/dist/esm/__tests__/helper.test.js +612 -0
- package/dist/esm/__tests__/helper.test.js.map +1 -0
- package/dist/esm/__tests__/pluginRegistry.test.js +81 -0
- package/dist/esm/__tests__/pluginRegistry.test.js.map +1 -0
- package/dist/esm/src/constants.js +24 -0
- package/dist/esm/src/constants.js.map +1 -0
- package/dist/esm/src/dynamicTemplate.js +280 -0
- package/dist/esm/src/dynamicTemplate.js.map +1 -0
- package/dist/esm/src/expression.js +426 -0
- package/dist/esm/src/expression.js.map +1 -0
- package/dist/esm/src/helper.js +193 -0
- package/dist/esm/src/helper.js.map +1 -0
- package/dist/esm/src/index.js +8 -0
- package/dist/esm/src/index.js.map +1 -0
- package/dist/esm/src/pluginRegistry.js +29 -0
- package/dist/esm/src/pluginRegistry.js.map +1 -0
- package/dist/esm/src/schema.js +187 -0
- package/dist/esm/src/schema.js.map +1 -0
- package/dist/esm/src/types.js +2 -0
- package/dist/esm/src/types.js.map +1 -0
- package/dist/esm/src/version.js +2 -0
- package/dist/esm/src/version.js.map +1 -0
- package/dist/node/__tests__/dynamicTemplate.test.js +275 -0
- package/dist/node/__tests__/dynamicTemplate.test.js.map +1 -0
- package/dist/node/__tests__/expression.test.js +474 -0
- package/dist/node/__tests__/expression.test.js.map +1 -0
- package/dist/node/__tests__/helper.test.js +647 -0
- package/dist/node/__tests__/helper.test.js.map +1 -0
- package/dist/node/__tests__/pluginRegistry.test.js +83 -0
- package/dist/node/__tests__/pluginRegistry.test.js.map +1 -0
- package/dist/node/src/constants.js +27 -0
- package/dist/node/src/constants.js.map +1 -0
- package/dist/node/src/dynamicTemplate.js +284 -0
- package/dist/node/src/dynamicTemplate.js.map +1 -0
- package/dist/node/src/expression.js +463 -0
- package/dist/node/src/expression.js.map +1 -0
- package/dist/node/src/helper.js +217 -0
- package/dist/node/src/helper.js.map +1 -0
- package/dist/node/src/index.js +42 -0
- package/dist/node/src/index.js.map +1 -0
- package/dist/node/src/pluginRegistry.js +33 -0
- package/dist/node/src/pluginRegistry.js.map +1 -0
- package/dist/node/src/schema.js +190 -0
- package/dist/node/src/schema.js.map +1 -0
- package/dist/node/src/types.js +3 -0
- package/dist/node/src/types.js.map +1 -0
- package/dist/node/src/version.js +5 -0
- package/dist/node/src/version.js.map +1 -0
- package/dist/types/__tests__/dynamicTemplate.test.d.ts +1 -0
- package/dist/types/__tests__/expression.test.d.ts +1 -0
- package/dist/types/__tests__/helper.test.d.ts +1 -0
- package/dist/types/__tests__/pluginRegistry.test.d.ts +1 -0
- package/dist/types/src/constants.d.ts +20 -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 +35 -0
- package/dist/types/src/index.d.ts +9 -0
- package/dist/types/src/pluginRegistry.d.ts +5 -0
- package/dist/types/src/schema.d.ts +820 -0
- package/dist/types/src/types.d.ts +181 -0
- package/dist/types/src/version.d.ts +1 -0
- package/eslint.config.mjs +22 -0
- package/package.json +84 -0
- package/set-version.js +31 -0
- package/src/constants.ts +30 -0
- package/src/dynamicTemplate.ts +349 -0
- package/src/expression.ts +460 -0
- package/src/helper.ts +284 -0
- package/src/index.ts +136 -0
- package/src/pluginRegistry.ts +30 -0
- package/src/schema.ts +223 -0
- package/src/types.ts +198 -0
- package/src/version.ts +1 -0
- package/tsconfig.cjs.json +10 -0
- package/tsconfig.esm.json +11 -0
- package/tsconfig.json +6 -0
- package/tsconfig.node.json +11 -0
package/src/helper.ts
ADDED
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { Buffer } from 'buffer';
|
|
3
|
+
import {
|
|
4
|
+
Schema,
|
|
5
|
+
Template,
|
|
6
|
+
Font,
|
|
7
|
+
BasePdf,
|
|
8
|
+
Plugins,
|
|
9
|
+
BlankPdf,
|
|
10
|
+
LegacySchemaPageArray,
|
|
11
|
+
SchemaPageArray,
|
|
12
|
+
} from './types.js';
|
|
13
|
+
import {
|
|
14
|
+
Inputs as InputsSchema,
|
|
15
|
+
UIOptions as UIOptionsSchema,
|
|
16
|
+
Template as TemplateSchema,
|
|
17
|
+
PreviewProps as PreviewPropsSchema,
|
|
18
|
+
DesignerProps as DesignerPropsSchema,
|
|
19
|
+
GenerateProps as GeneratePropsSchema,
|
|
20
|
+
UIProps as UIPropsSchema,
|
|
21
|
+
BlankPdf as BlankPdfSchema,
|
|
22
|
+
} from './schema.js';
|
|
23
|
+
import {
|
|
24
|
+
MM_TO_PT_RATIO,
|
|
25
|
+
PT_TO_MM_RATIO,
|
|
26
|
+
PT_TO_PX_RATIO,
|
|
27
|
+
DEFAULT_FONT_NAME,
|
|
28
|
+
DEFAULT_FONT_VALUE,
|
|
29
|
+
} from './constants.js';
|
|
30
|
+
|
|
31
|
+
export const cloneDeep = structuredClone;
|
|
32
|
+
|
|
33
|
+
const uniq = <T>(array: Array<T>) => Array.from(new Set(array));
|
|
34
|
+
|
|
35
|
+
export const getFallbackFontName = (font: Font) => {
|
|
36
|
+
const initial = '';
|
|
37
|
+
const fallbackFontName = Object.entries(font).reduce((acc, cur) => {
|
|
38
|
+
const [fontName, fontValue] = cur as [string, { data: string | ArrayBuffer | Uint8Array; fallback?: boolean; subset?: boolean }];
|
|
39
|
+
|
|
40
|
+
return !acc && fontValue.fallback ? fontName : acc;
|
|
41
|
+
}, initial);
|
|
42
|
+
if (fallbackFontName === initial) {
|
|
43
|
+
throw Error(
|
|
44
|
+
`[@pdfme/common] fallback flag is not found in font. true fallback flag must be only one.`,
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return fallbackFontName;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export const getDefaultFont = (): Font => ({
|
|
52
|
+
[DEFAULT_FONT_NAME]: { data: b64toUint8Array(DEFAULT_FONT_VALUE), fallback: true },
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
export const mm2pt = (mm: number): number => {
|
|
56
|
+
return parseFloat(String(mm)) * MM_TO_PT_RATIO;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
export const pt2mm = (pt: number): number => {
|
|
60
|
+
return pt * PT_TO_MM_RATIO;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export const pt2px = (pt: number): number => {
|
|
64
|
+
return pt * PT_TO_PX_RATIO;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
export const px2mm = (px: number): number => {
|
|
68
|
+
// http://www.endmemo.com/sconvert/millimeterpixel.php
|
|
69
|
+
const ratio = 0.26458333333333;
|
|
70
|
+
return parseFloat(String(px)) * ratio;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const blob2Base64Pdf = (blob: Blob) => {
|
|
74
|
+
return new Promise<string>((resolve, reject) => {
|
|
75
|
+
const reader = new FileReader();
|
|
76
|
+
reader.onloadend = () => {
|
|
77
|
+
if ((reader.result as string).startsWith('data:application/pdf;')) {
|
|
78
|
+
resolve(reader.result as string);
|
|
79
|
+
} else {
|
|
80
|
+
reject(Error('[@pdfme/common] template.basePdf must be pdf data.'));
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
reader.readAsDataURL(blob);
|
|
84
|
+
});
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
export const isHexValid = (hex: string): boolean => {
|
|
88
|
+
return /^#(?:[A-Fa-f0-9]{3,4}|[A-Fa-f0-9]{6}|[A-Fa-f0-9]{8})$/i.test(hex);
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Migrate from legacy keyed object format to array format
|
|
93
|
+
* @param template Template
|
|
94
|
+
*/
|
|
95
|
+
export const migrateTemplate = (template: Template) => {
|
|
96
|
+
if (!template.schemas) {
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (
|
|
101
|
+
Array.isArray(template.schemas) &&
|
|
102
|
+
template.schemas.length > 0 &&
|
|
103
|
+
!Array.isArray(template.schemas[0])
|
|
104
|
+
) {
|
|
105
|
+
template.schemas = (template.schemas as unknown as LegacySchemaPageArray).map(
|
|
106
|
+
(page: Record<string, Schema>) =>
|
|
107
|
+
Object.entries(page).map(([key, value]) => ({
|
|
108
|
+
...value,
|
|
109
|
+
name: key,
|
|
110
|
+
})),
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
export const getInputFromTemplate = (template: Template): { [key: string]: string }[] => {
|
|
116
|
+
migrateTemplate(template);
|
|
117
|
+
|
|
118
|
+
const input: { [key: string]: string } = {};
|
|
119
|
+
template.schemas.forEach((page) => {
|
|
120
|
+
page.forEach((schema) => {
|
|
121
|
+
if (!schema.readOnly) {
|
|
122
|
+
input[schema.name] = schema.content || '';
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
return [input];
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
export const getB64BasePdf = async (
|
|
131
|
+
customPdf: ArrayBuffer | Uint8Array | string,
|
|
132
|
+
): Promise<string> => {
|
|
133
|
+
if (
|
|
134
|
+
typeof customPdf === 'string' &&
|
|
135
|
+
!customPdf.startsWith('data:application/pdf;') &&
|
|
136
|
+
typeof window !== 'undefined'
|
|
137
|
+
) {
|
|
138
|
+
const response = await fetch(customPdf);
|
|
139
|
+
const blob = await response.blob();
|
|
140
|
+
return blob2Base64Pdf(blob);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (typeof customPdf === 'string') {
|
|
144
|
+
return customPdf;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const uint8Array = customPdf instanceof Uint8Array ? customPdf : new Uint8Array(customPdf);
|
|
148
|
+
return 'data:application/pdf;base64,' + Buffer.from(uint8Array).toString('base64');
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
export const isBlankPdf = (basePdf: BasePdf): basePdf is BlankPdf =>
|
|
152
|
+
BlankPdfSchema.safeParse(basePdf).success;
|
|
153
|
+
|
|
154
|
+
const getByteString = (base64: string) => Buffer.from(base64, 'base64').toString('binary');
|
|
155
|
+
|
|
156
|
+
export const b64toUint8Array = (base64: string) => {
|
|
157
|
+
const data = base64.split(';base64,')[1] ? base64.split(';base64,')[1] : base64;
|
|
158
|
+
|
|
159
|
+
const byteString = getByteString(data);
|
|
160
|
+
|
|
161
|
+
const unit8arr = new Uint8Array(byteString.length);
|
|
162
|
+
for (let i = 0; i < byteString.length; i += 1) {
|
|
163
|
+
unit8arr[i] = byteString.charCodeAt(i);
|
|
164
|
+
}
|
|
165
|
+
return unit8arr;
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
const getFontNamesInSchemas = (schemas: SchemaPageArray) =>
|
|
169
|
+
uniq(
|
|
170
|
+
schemas
|
|
171
|
+
.map((p) => p.map((v) => (v as Schema & { fontName?: string }).fontName ?? ''))
|
|
172
|
+
.reduce((acc, cur) => acc.concat(cur), [] as (string | undefined)[])
|
|
173
|
+
.filter(Boolean) as string[],
|
|
174
|
+
);
|
|
175
|
+
|
|
176
|
+
export const checkFont = (arg: { font: Font; template: Template }) => {
|
|
177
|
+
const {
|
|
178
|
+
font,
|
|
179
|
+
template: { schemas },
|
|
180
|
+
} = arg;
|
|
181
|
+
const fontValues = Object.values(font);
|
|
182
|
+
const fallbackFontNum = fontValues.reduce((acc, cur) => (cur.fallback ? acc + 1 : acc), 0 as number);
|
|
183
|
+
if (fallbackFontNum === 0) {
|
|
184
|
+
throw Error(
|
|
185
|
+
`[@pdfme/common] fallback flag is not found in font. true fallback flag must be only one.
|
|
186
|
+
Check this document: https://pdfme.com/docs/custom-fonts#about-font-type`,
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
if (fallbackFontNum > 1) {
|
|
190
|
+
throw Error(
|
|
191
|
+
`[@pdfme/common] ${fallbackFontNum} fallback flags found in font. true fallback flag must be only one.
|
|
192
|
+
Check this document: https://pdfme.com/docs/custom-fonts#about-font-type`,
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const fontNamesInSchemas = getFontNamesInSchemas(schemas);
|
|
197
|
+
const fontNames = Object.keys(font);
|
|
198
|
+
if (fontNamesInSchemas.some((f) => !fontNames.includes(f))) {
|
|
199
|
+
throw Error(
|
|
200
|
+
`[@pdfme/common] ${fontNamesInSchemas
|
|
201
|
+
.filter((f) => !fontNames.includes(f))
|
|
202
|
+
.join()} of template.schemas is not found in font.
|
|
203
|
+
Check this document: https://pdfme.com/docs/custom-fonts`,
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
export const checkPlugins = (arg: { plugins: Plugins; template: Template }) => {
|
|
209
|
+
const {
|
|
210
|
+
plugins,
|
|
211
|
+
template: { schemas },
|
|
212
|
+
} = arg;
|
|
213
|
+
const allSchemaTypes = uniq(schemas.map((p) => p.map((v) => v.type)).flat());
|
|
214
|
+
|
|
215
|
+
const pluginsSchemaTypes = Object.values(plugins).map((p) =>
|
|
216
|
+
p ? (p.propPanel.defaultSchema as Schema).type : undefined,
|
|
217
|
+
);
|
|
218
|
+
|
|
219
|
+
if (allSchemaTypes.some((s) => !pluginsSchemaTypes.includes(s))) {
|
|
220
|
+
throw Error(
|
|
221
|
+
`[@pdfme/common] ${allSchemaTypes
|
|
222
|
+
.filter((s) => !pluginsSchemaTypes.includes(s))
|
|
223
|
+
.join()} of template.schemas is not found in plugins.`,
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
const checkProps = <T>(data: unknown, zodSchema: z.ZodType<T>) => {
|
|
229
|
+
try {
|
|
230
|
+
zodSchema.parse(data);
|
|
231
|
+
} catch (e) {
|
|
232
|
+
if (e instanceof z.ZodError) {
|
|
233
|
+
const messages = e.issues.map(
|
|
234
|
+
(issue) => `ERROR POSITION: ${issue.path.join('.')}
|
|
235
|
+
ERROR MESSAGE: ${issue.message}
|
|
236
|
+
--------------------------`,
|
|
237
|
+
);
|
|
238
|
+
throw Error(`[@pdfme/common] Invalid argument:
|
|
239
|
+
--------------------------
|
|
240
|
+
${messages.join('\n')}`);
|
|
241
|
+
} else {
|
|
242
|
+
throw Error(
|
|
243
|
+
`[@pdfme/common] Unexpected parsing error: ${e instanceof Error ? e.message : String(e)}`,
|
|
244
|
+
);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// Check fonts if template and options exist
|
|
249
|
+
if (data && typeof data === 'object' && 'template' in data && 'options' in data) {
|
|
250
|
+
const { template, options } = data as { template: Template; options: { font?: Font } };
|
|
251
|
+
if (options && options.font) {
|
|
252
|
+
checkFont({ font: options.font, template });
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// Check plugins if template and plugins exist
|
|
257
|
+
if (data && typeof data === 'object' && 'template' in data && 'plugins' in data) {
|
|
258
|
+
const { template, plugins } = data as { template: Template; plugins: Plugins };
|
|
259
|
+
if (plugins) {
|
|
260
|
+
checkPlugins({ plugins, template });
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
export const checkInputs = (data: unknown) => checkProps(data, InputsSchema);
|
|
266
|
+
export const checkUIOptions = (data: unknown) => checkProps(data, UIOptionsSchema);
|
|
267
|
+
export const checkPreviewProps = (data: unknown) => checkProps(data, PreviewPropsSchema);
|
|
268
|
+
export const checkDesignerProps = (data: unknown) => checkProps(data, DesignerPropsSchema);
|
|
269
|
+
export const checkUIProps = (data: unknown) => {
|
|
270
|
+
if (typeof data === 'object' && data !== null && 'template' in data) {
|
|
271
|
+
migrateTemplate(data.template as Template);
|
|
272
|
+
}
|
|
273
|
+
checkProps(data, UIPropsSchema);
|
|
274
|
+
};
|
|
275
|
+
export const checkTemplate = (template: unknown) => {
|
|
276
|
+
migrateTemplate(template as Template);
|
|
277
|
+
checkProps(template, TemplateSchema);
|
|
278
|
+
};
|
|
279
|
+
export const checkGenerateProps = (data: unknown) => {
|
|
280
|
+
if (typeof data === 'object' && data !== null && 'template' in data) {
|
|
281
|
+
migrateTemplate(data.template as Template);
|
|
282
|
+
}
|
|
283
|
+
checkProps(data, GeneratePropsSchema);
|
|
284
|
+
};
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { PDFME_VERSION } from './version.js';
|
|
2
|
+
import {
|
|
3
|
+
MM_TO_PT_RATIO,
|
|
4
|
+
PT_TO_MM_RATIO,
|
|
5
|
+
PT_TO_PX_RATIO,
|
|
6
|
+
BLANK_PDF,
|
|
7
|
+
BLANK_A4_PDF,
|
|
8
|
+
CUSTOM_A4_PDF,
|
|
9
|
+
ZOOM,
|
|
10
|
+
DEFAULT_FONT_NAME,
|
|
11
|
+
} from './constants.js';
|
|
12
|
+
import type {
|
|
13
|
+
ChangeSchemaItem,
|
|
14
|
+
ChangeSchemas,
|
|
15
|
+
SchemaPageArray,
|
|
16
|
+
PropPanel,
|
|
17
|
+
PropPanelSchema,
|
|
18
|
+
PropPanelWidgetProps,
|
|
19
|
+
PDFRenderProps,
|
|
20
|
+
Mode,
|
|
21
|
+
UIRenderProps,
|
|
22
|
+
Plugin,
|
|
23
|
+
Lang,
|
|
24
|
+
Dict,
|
|
25
|
+
Size,
|
|
26
|
+
Schema,
|
|
27
|
+
SchemaForUI,
|
|
28
|
+
Font,
|
|
29
|
+
ColorType,
|
|
30
|
+
BasePdf,
|
|
31
|
+
BlankPdf,
|
|
32
|
+
CustomPdf,
|
|
33
|
+
Template,
|
|
34
|
+
CommonOptions,
|
|
35
|
+
GeneratorOptions,
|
|
36
|
+
Plugins,
|
|
37
|
+
PluginRegistry,
|
|
38
|
+
GenerateProps,
|
|
39
|
+
UIOptions,
|
|
40
|
+
UIProps,
|
|
41
|
+
PreviewProps,
|
|
42
|
+
DesignerProps,
|
|
43
|
+
} from './types.js';
|
|
44
|
+
import {
|
|
45
|
+
cloneDeep,
|
|
46
|
+
getFallbackFontName,
|
|
47
|
+
getDefaultFont,
|
|
48
|
+
getB64BasePdf,
|
|
49
|
+
b64toUint8Array,
|
|
50
|
+
checkFont,
|
|
51
|
+
checkInputs,
|
|
52
|
+
checkUIOptions,
|
|
53
|
+
checkTemplate,
|
|
54
|
+
checkUIProps,
|
|
55
|
+
checkPreviewProps,
|
|
56
|
+
checkDesignerProps,
|
|
57
|
+
checkGenerateProps,
|
|
58
|
+
mm2pt,
|
|
59
|
+
pt2mm,
|
|
60
|
+
pt2px,
|
|
61
|
+
px2mm,
|
|
62
|
+
isHexValid,
|
|
63
|
+
getInputFromTemplate,
|
|
64
|
+
isBlankPdf,
|
|
65
|
+
} from './helper.js';
|
|
66
|
+
import { getDynamicTemplate } from './dynamicTemplate.js';
|
|
67
|
+
import { replacePlaceholders } from './expression.js';
|
|
68
|
+
import { pluginRegistry } from './pluginRegistry.js';
|
|
69
|
+
|
|
70
|
+
export {
|
|
71
|
+
PDFME_VERSION,
|
|
72
|
+
MM_TO_PT_RATIO,
|
|
73
|
+
PT_TO_MM_RATIO,
|
|
74
|
+
PT_TO_PX_RATIO,
|
|
75
|
+
BLANK_PDF,
|
|
76
|
+
BLANK_A4_PDF,
|
|
77
|
+
CUSTOM_A4_PDF,
|
|
78
|
+
ZOOM,
|
|
79
|
+
DEFAULT_FONT_NAME,
|
|
80
|
+
cloneDeep,
|
|
81
|
+
getFallbackFontName,
|
|
82
|
+
getDefaultFont,
|
|
83
|
+
getB64BasePdf,
|
|
84
|
+
b64toUint8Array,
|
|
85
|
+
mm2pt,
|
|
86
|
+
pt2mm,
|
|
87
|
+
pt2px,
|
|
88
|
+
px2mm,
|
|
89
|
+
isHexValid,
|
|
90
|
+
getInputFromTemplate,
|
|
91
|
+
isBlankPdf,
|
|
92
|
+
getDynamicTemplate,
|
|
93
|
+
replacePlaceholders,
|
|
94
|
+
checkFont,
|
|
95
|
+
checkInputs,
|
|
96
|
+
checkUIOptions,
|
|
97
|
+
checkTemplate,
|
|
98
|
+
checkUIProps,
|
|
99
|
+
checkPreviewProps,
|
|
100
|
+
checkDesignerProps,
|
|
101
|
+
checkGenerateProps,
|
|
102
|
+
pluginRegistry,
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
export type {
|
|
106
|
+
Lang,
|
|
107
|
+
Dict,
|
|
108
|
+
Size,
|
|
109
|
+
Schema,
|
|
110
|
+
SchemaForUI,
|
|
111
|
+
Font,
|
|
112
|
+
ColorType,
|
|
113
|
+
BasePdf,
|
|
114
|
+
BlankPdf,
|
|
115
|
+
CustomPdf,
|
|
116
|
+
Template,
|
|
117
|
+
CommonOptions,
|
|
118
|
+
GeneratorOptions,
|
|
119
|
+
Plugin,
|
|
120
|
+
Plugins,
|
|
121
|
+
PluginRegistry,
|
|
122
|
+
GenerateProps,
|
|
123
|
+
UIOptions,
|
|
124
|
+
UIProps,
|
|
125
|
+
PreviewProps,
|
|
126
|
+
DesignerProps,
|
|
127
|
+
ChangeSchemaItem,
|
|
128
|
+
ChangeSchemas,
|
|
129
|
+
SchemaPageArray,
|
|
130
|
+
PropPanel,
|
|
131
|
+
PropPanelSchema,
|
|
132
|
+
PropPanelWidgetProps,
|
|
133
|
+
PDFRenderProps,
|
|
134
|
+
UIRenderProps,
|
|
135
|
+
Mode,
|
|
136
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { Plugins, Plugin, PluginRegistry } from './types.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Wraps plugins collection with utility methods
|
|
5
|
+
*/
|
|
6
|
+
export const pluginRegistry = (plugins: Plugins): PluginRegistry => {
|
|
7
|
+
return {
|
|
8
|
+
plugins: plugins,
|
|
9
|
+
entries: (): [string, Plugin][] => Object.entries(plugins),
|
|
10
|
+
values: (): Plugin[] => Object.values(plugins),
|
|
11
|
+
exists: (): boolean => Object.values(plugins).length > 0,
|
|
12
|
+
findWithLabelByType(type: string): [string, Plugin | undefined] {
|
|
13
|
+
for (const [label, plugin] of Object.entries(this.plugins) as [string, Plugin][]) {
|
|
14
|
+
if (!plugin || typeof plugin !== 'object') continue;
|
|
15
|
+
if (!plugin.propPanel || typeof plugin.propPanel !== 'object') continue;
|
|
16
|
+
|
|
17
|
+
const defaultSchema = plugin.propPanel.defaultSchema as Record<string, unknown>;
|
|
18
|
+
|
|
19
|
+
if (defaultSchema && 'type' in defaultSchema && defaultSchema.type === type) {
|
|
20
|
+
return [label, plugin];
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return ['', undefined];
|
|
24
|
+
},
|
|
25
|
+
findByType(type: string): Plugin | undefined {
|
|
26
|
+
const [, plugin] = this.findWithLabelByType(type);
|
|
27
|
+
return plugin;
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
};
|
package/src/schema.ts
ADDED
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
const langs = ['en', 'zh', 'ja', 'ko', 'ar', 'th', 'pl', 'it', 'de', 'es', 'fr'] as const;
|
|
4
|
+
|
|
5
|
+
export const Lang = z.enum(langs);
|
|
6
|
+
export const Dict = z.object({
|
|
7
|
+
// -----------------used in ui-----------------
|
|
8
|
+
cancel: z.string(),
|
|
9
|
+
close: z.string(),
|
|
10
|
+
set: z.string(),
|
|
11
|
+
clear: z.string(),
|
|
12
|
+
field: z.string(),
|
|
13
|
+
fieldName: z.string(),
|
|
14
|
+
align: z.string(),
|
|
15
|
+
width: z.string(),
|
|
16
|
+
opacity: z.string(),
|
|
17
|
+
height: z.string(),
|
|
18
|
+
rotate: z.string(),
|
|
19
|
+
edit: z.string(),
|
|
20
|
+
required: z.string(),
|
|
21
|
+
editable: z.string(),
|
|
22
|
+
plsInputName: z.string(),
|
|
23
|
+
fieldMustUniq: z.string(),
|
|
24
|
+
notUniq: z.string(),
|
|
25
|
+
noKeyName: z.string(),
|
|
26
|
+
fieldsList: z.string(),
|
|
27
|
+
editField: z.string(),
|
|
28
|
+
type: z.string(),
|
|
29
|
+
errorOccurred: z.string(),
|
|
30
|
+
errorBulkUpdateFieldName: z.string(),
|
|
31
|
+
commitBulkUpdateFieldName: z.string(),
|
|
32
|
+
bulkUpdateFieldName: z.string(),
|
|
33
|
+
addPageAfter: z.string(),
|
|
34
|
+
removePage: z.string(),
|
|
35
|
+
removePageConfirm: z.string(),
|
|
36
|
+
// --------------------validation-------------------
|
|
37
|
+
'validation.uniqueName': z.string(),
|
|
38
|
+
'validation.hexColor': z.string(),
|
|
39
|
+
'validation.dateTimeFormat': z.string(),
|
|
40
|
+
'validation.outOfBounds': z.string(),
|
|
41
|
+
|
|
42
|
+
// -----------------used in schemas-----------------
|
|
43
|
+
'schemas.color': z.string(),
|
|
44
|
+
'schemas.borderWidth': z.string(),
|
|
45
|
+
'schemas.borderColor': z.string(),
|
|
46
|
+
'schemas.backgroundColor': z.string(),
|
|
47
|
+
'schemas.textColor': z.string(),
|
|
48
|
+
'schemas.bgColor': z.string(),
|
|
49
|
+
'schemas.horizontal': z.string(),
|
|
50
|
+
'schemas.vertical': z.string(),
|
|
51
|
+
'schemas.left': z.string(),
|
|
52
|
+
'schemas.center': z.string(),
|
|
53
|
+
'schemas.right': z.string(),
|
|
54
|
+
'schemas.top': z.string(),
|
|
55
|
+
'schemas.middle': z.string(),
|
|
56
|
+
'schemas.bottom': z.string(),
|
|
57
|
+
'schemas.padding': z.string(),
|
|
58
|
+
|
|
59
|
+
'schemas.text.fontName': z.string(),
|
|
60
|
+
'schemas.text.size': z.string(),
|
|
61
|
+
'schemas.text.spacing': z.string(),
|
|
62
|
+
'schemas.text.textAlign': z.string(),
|
|
63
|
+
'schemas.text.verticalAlign': z.string(),
|
|
64
|
+
'schemas.text.lineHeight': z.string(),
|
|
65
|
+
'schemas.text.min': z.string(),
|
|
66
|
+
'schemas.text.max': z.string(),
|
|
67
|
+
'schemas.text.fit': z.string(),
|
|
68
|
+
'schemas.text.dynamicFontSize': z.string(),
|
|
69
|
+
'schemas.text.format': z.string(),
|
|
70
|
+
'schemas.radius': z.string(),
|
|
71
|
+
|
|
72
|
+
'schemas.mvt.typingInstructions': z.string(),
|
|
73
|
+
'schemas.mvt.sampleField': z.string(),
|
|
74
|
+
'schemas.mvt.variablesSampleData': z.string(),
|
|
75
|
+
|
|
76
|
+
'schemas.barcodes.barColor': z.string(),
|
|
77
|
+
'schemas.barcodes.includetext': z.string(),
|
|
78
|
+
|
|
79
|
+
'schemas.table.alternateBackgroundColor': z.string(),
|
|
80
|
+
'schemas.table.tableStyle': z.string(),
|
|
81
|
+
'schemas.table.showHead': z.string(),
|
|
82
|
+
'schemas.table.repeatHead': z.string(),
|
|
83
|
+
'schemas.table.headStyle': z.string(),
|
|
84
|
+
'schemas.table.bodyStyle': z.string(),
|
|
85
|
+
'schemas.table.columnStyle': z.string(),
|
|
86
|
+
|
|
87
|
+
'schemas.date.format': z.string(),
|
|
88
|
+
'schemas.date.locale': z.string(),
|
|
89
|
+
|
|
90
|
+
'schemas.select.options': z.string(),
|
|
91
|
+
'schemas.select.optionPlaceholder': z.string(),
|
|
92
|
+
|
|
93
|
+
'schemas.radioGroup.groupName': z.string(),
|
|
94
|
+
});
|
|
95
|
+
export const Mode = z.enum(['viewer', 'form', 'designer']);
|
|
96
|
+
|
|
97
|
+
export const ColorType = z.enum(['rgb', 'cmyk']).optional();
|
|
98
|
+
|
|
99
|
+
export const Size = z.object({ height: z.number(), width: z.number() });
|
|
100
|
+
|
|
101
|
+
export const Schema = z
|
|
102
|
+
.object({
|
|
103
|
+
name: z.string(),
|
|
104
|
+
type: z.string(),
|
|
105
|
+
content: z.string().optional(),
|
|
106
|
+
position: z.object({ x: z.number(), y: z.number() }),
|
|
107
|
+
width: z.number(),
|
|
108
|
+
height: z.number(),
|
|
109
|
+
rotate: z.number().optional(),
|
|
110
|
+
opacity: z.number().optional(),
|
|
111
|
+
readOnly: z.boolean().optional(),
|
|
112
|
+
required: z.boolean().optional(),
|
|
113
|
+
__bodyRange: z.object({ start: z.number(), end: z.number().optional() }).optional(),
|
|
114
|
+
__isSplit: z.boolean().optional(),
|
|
115
|
+
})
|
|
116
|
+
.passthrough();
|
|
117
|
+
|
|
118
|
+
const SchemaForUIAdditionalInfo = z.object({ id: z.string() });
|
|
119
|
+
export const SchemaForUI = Schema.merge(SchemaForUIAdditionalInfo);
|
|
120
|
+
|
|
121
|
+
const ArrayBufferSchema: z.ZodSchema<ArrayBuffer> = z.any().refine((v) => v instanceof ArrayBuffer);
|
|
122
|
+
const Uint8ArraySchema: z.ZodSchema<Uint8Array<ArrayBuffer>> = z
|
|
123
|
+
.any()
|
|
124
|
+
.refine((v) => v instanceof Uint8Array && v.buffer instanceof ArrayBuffer);
|
|
125
|
+
|
|
126
|
+
export const BlankPdf = z.object({
|
|
127
|
+
width: z.number(),
|
|
128
|
+
height: z.number(),
|
|
129
|
+
padding: z.tuple([z.number(), z.number(), z.number(), z.number()]),
|
|
130
|
+
staticSchema: z.array(Schema).optional(),
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
export const CustomPdf = z.union([z.string(), ArrayBufferSchema, Uint8ArraySchema]);
|
|
134
|
+
|
|
135
|
+
export const BasePdf = z.union([CustomPdf, BlankPdf]);
|
|
136
|
+
|
|
137
|
+
// Legacy keyed structure for BC - we convert to SchemaPageArray on import
|
|
138
|
+
export const LegacySchemaPageArray = z.array(z.record(z.string(), Schema));
|
|
139
|
+
export const SchemaPageArray = z.array(z.array(Schema));
|
|
140
|
+
|
|
141
|
+
export const Template = z
|
|
142
|
+
.object({
|
|
143
|
+
schemas: SchemaPageArray,
|
|
144
|
+
basePdf: BasePdf,
|
|
145
|
+
pdfmeVersion: z.string().optional(),
|
|
146
|
+
})
|
|
147
|
+
.passthrough();
|
|
148
|
+
|
|
149
|
+
export const Inputs = z.array(z.record(z.string(), z.any())).min(1);
|
|
150
|
+
|
|
151
|
+
export const Font = z.record(
|
|
152
|
+
z.string(),
|
|
153
|
+
z.object({
|
|
154
|
+
data: z.union([z.string(), ArrayBufferSchema, Uint8ArraySchema]),
|
|
155
|
+
fallback: z.boolean().optional(),
|
|
156
|
+
subset: z.boolean().optional(),
|
|
157
|
+
}),
|
|
158
|
+
);
|
|
159
|
+
|
|
160
|
+
export const Plugin = z
|
|
161
|
+
.object({
|
|
162
|
+
ui: z.any(),
|
|
163
|
+
pdf: z.any(),
|
|
164
|
+
propPanel: z.object({
|
|
165
|
+
schema: z.unknown(),
|
|
166
|
+
widgets: z.record(z.string(), z.any()).optional(),
|
|
167
|
+
defaultSchema: Schema,
|
|
168
|
+
}),
|
|
169
|
+
icon: z.string().optional(),
|
|
170
|
+
})
|
|
171
|
+
.passthrough();
|
|
172
|
+
|
|
173
|
+
export const CommonOptions = z.object({ font: Font.optional() }).passthrough();
|
|
174
|
+
|
|
175
|
+
const CommonProps = z.object({
|
|
176
|
+
template: Template,
|
|
177
|
+
options: CommonOptions.optional(),
|
|
178
|
+
plugins: z.record(z.string(), Plugin).optional(),
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
// -------------------generate-------------------
|
|
182
|
+
|
|
183
|
+
export const GeneratorOptions = CommonOptions.extend({
|
|
184
|
+
colorType: ColorType,
|
|
185
|
+
author: z.string().optional(),
|
|
186
|
+
creationDate: z.date().optional(),
|
|
187
|
+
creator: z.string().optional(),
|
|
188
|
+
keywords: z.array(z.string()).optional(),
|
|
189
|
+
lang: Lang.optional(),
|
|
190
|
+
modificationDate: z.date().optional(),
|
|
191
|
+
producer: z.string().optional(),
|
|
192
|
+
subject: z.string().optional(),
|
|
193
|
+
title: z.string().optional(),
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
export const GenerateProps = CommonProps.extend({
|
|
197
|
+
inputs: Inputs,
|
|
198
|
+
options: GeneratorOptions.optional(),
|
|
199
|
+
}).strict();
|
|
200
|
+
|
|
201
|
+
// ---------------------ui------------------------
|
|
202
|
+
|
|
203
|
+
export const UIOptions = CommonOptions.extend({
|
|
204
|
+
lang: Lang.optional(),
|
|
205
|
+
labels: z.record(z.string(), z.string()).optional(),
|
|
206
|
+
theme: z.record(z.string(), z.unknown()).optional(),
|
|
207
|
+
icons: z.record(z.string(), z.string()).optional(),
|
|
208
|
+
requiredByDefault: z.boolean().optional(),
|
|
209
|
+
maxZoom: z.number().optional(),
|
|
210
|
+
sidebarOpen: z.boolean().optional(),
|
|
211
|
+
zoomLevel: z.number().optional(),
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
const HTMLElementSchema: z.ZodSchema<HTMLElement> = z.any().refine((v) => v instanceof HTMLElement);
|
|
215
|
+
|
|
216
|
+
export const UIProps = CommonProps.extend({
|
|
217
|
+
domContainer: HTMLElementSchema,
|
|
218
|
+
options: UIOptions.optional(),
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
export const PreviewProps = UIProps.extend({ inputs: Inputs }).strict();
|
|
222
|
+
|
|
223
|
+
export const DesignerProps = UIProps.extend({}).strict();
|