@pdfme/generator 3.1.5 → 3.2.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.
Files changed (38) hide show
  1. package/dist/cjs/__tests__/assets/templates/index.js +8 -1
  2. package/dist/cjs/__tests__/assets/templates/index.js.map +1 -1
  3. package/dist/cjs/__tests__/integration1.test.js +8 -1
  4. package/dist/cjs/__tests__/integration1.test.js.map +1 -1
  5. package/dist/cjs/__tests__/integration2.test.js +8 -1
  6. package/dist/cjs/__tests__/integration2.test.js.map +1 -1
  7. package/dist/cjs/__tests__/integration3.test.js +32 -4
  8. package/dist/cjs/__tests__/integration3.test.js.map +1 -1
  9. package/dist/cjs/__tests__/integration4.test.js +51 -0
  10. package/dist/cjs/__tests__/integration4.test.js.map +1 -0
  11. package/dist/cjs/__tests__/utils.js +2 -0
  12. package/dist/cjs/__tests__/utils.js.map +1 -1
  13. package/dist/cjs/src/generate.js +10 -7
  14. package/dist/cjs/src/generate.js.map +1 -1
  15. package/dist/cjs/src/helper.js +8 -3
  16. package/dist/cjs/src/helper.js.map +1 -1
  17. package/dist/esm/__tests__/assets/templates/index.js +7 -0
  18. package/dist/esm/__tests__/assets/templates/index.js.map +1 -1
  19. package/dist/esm/__tests__/integration1.test.js +8 -1
  20. package/dist/esm/__tests__/integration1.test.js.map +1 -1
  21. package/dist/esm/__tests__/integration2.test.js +8 -1
  22. package/dist/esm/__tests__/integration2.test.js.map +1 -1
  23. package/dist/esm/__tests__/integration3.test.js +34 -6
  24. package/dist/esm/__tests__/integration3.test.js.map +1 -1
  25. package/dist/esm/__tests__/integration4.test.js +46 -0
  26. package/dist/esm/__tests__/integration4.test.js.map +1 -0
  27. package/dist/esm/__tests__/utils.js +2 -0
  28. package/dist/esm/__tests__/utils.js.map +1 -1
  29. package/dist/esm/src/generate.js +10 -7
  30. package/dist/esm/src/generate.js.map +1 -1
  31. package/dist/esm/src/helper.js +8 -3
  32. package/dist/esm/src/helper.js.map +1 -1
  33. package/dist/types/__tests__/assets/templates/index.d.ts +134 -0
  34. package/dist/types/__tests__/integration4.test.d.ts +1 -0
  35. package/dist/types/src/helper.d.ts +2 -1
  36. package/package.json +3 -3
  37. package/src/generate.ts +13 -10
  38. package/src/helper.ts +14 -5
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pdfme/generator",
3
- "version": "3.1.5",
3
+ "version": "3.2.0",
4
4
  "sideEffects": false,
5
5
  "author": "hand-dot",
6
6
  "license": "MIT",
@@ -32,7 +32,7 @@
32
32
  }
33
33
  },
34
34
  "scripts": {
35
- "develop": "tsc -p tsconfig.esm.json -w",
35
+ "dev": "tsc -p tsconfig.esm.json -w",
36
36
  "build": "npm-run-all --parallel build:cjs build:esm",
37
37
  "build:cjs": "tsc -p tsconfig.cjs.json",
38
38
  "build:esm": "tsc -p tsconfig.esm.json",
@@ -43,7 +43,7 @@
43
43
  "prettier": "prettier --write 'src/**/*.ts'"
44
44
  },
45
45
  "dependencies": {
46
- "@pdfme/pdf-lib": "^1.17.3",
46
+ "@pdfme/pdf-lib": "^1.18.3",
47
47
  "atob": "^2.1.2",
48
48
  "fontkit": "^2.0.2"
49
49
  },
package/src/generate.ts CHANGED
@@ -7,16 +7,21 @@ const generate = async (props: GenerateProps) => {
7
7
  checkGenerateProps(props);
8
8
  const { inputs, template, options = {}, plugins: userPlugins = {} } = props;
9
9
 
10
- const { pdfDoc, embeddedPages, embedPdfBoxes, renderObj } = await preprocessing({
11
- template,
12
- userPlugins,
13
- });
10
+ if (inputs.length === 0) {
11
+ throw new Error('inputs should not be empty');
12
+ }
14
13
 
15
- const _cache = new Map();
14
+ const { pdfDoc, embeddedPages, embedPdfBoxes, renderObj, readOnlySchemaKeys } =
15
+ await preprocessing({ template, userPlugins });
16
+
17
+ const keys = readOnlySchemaKeys.concat(Object.keys(inputs[0]));
18
+ if (template.columns) {
19
+ keys.sort((a, b) => (template.columns ?? []).indexOf(a) - (template.columns ?? []).indexOf(b));
20
+ }
16
21
 
22
+ const _cache = new Map();
17
23
  for (let i = 0; i < inputs.length; i += 1) {
18
24
  const inputObj = inputs[i];
19
- const keys = Object.keys(inputObj);
20
25
  for (let j = 0; j < embeddedPages.length; j += 1) {
21
26
  const embeddedPage = embeddedPages[j];
22
27
  const { width: pageWidth, height: pageHeight } = embeddedPage;
@@ -29,9 +34,7 @@ const generate = async (props: GenerateProps) => {
29
34
  const key = keys[l];
30
35
  const schemaObj = template.schemas[j];
31
36
  const schema = schemaObj[key];
32
- const value = inputObj[key];
33
-
34
- if (!schema || !value) {
37
+ if (!schema) {
35
38
  continue;
36
39
  }
37
40
 
@@ -39,7 +42,7 @@ const generate = async (props: GenerateProps) => {
39
42
  if (!render) {
40
43
  continue;
41
44
  }
42
-
45
+ const value = schema.readOnly ? schema.readOnlyValue || '' : inputObj[key];
43
46
  await render({ key, value, schema, pdfLib, pdfDoc, page, options, _cache });
44
47
  }
45
48
  }
package/src/helper.ts CHANGED
@@ -55,7 +55,7 @@ export const drawEmbeddedPage = (arg: {
55
55
 
56
56
  export const preprocessing = async (arg: { template: Template; userPlugins: Plugins }) => {
57
57
  const { template, userPlugins } = arg;
58
- const { basePdf } = template;
58
+ const { basePdf, schemas } = template;
59
59
 
60
60
  const pdfDoc = await pdfLib.PDFDocument.create();
61
61
  // @ts-ignore
@@ -70,7 +70,7 @@ export const preprocessing = async (arg: { template: Template; userPlugins: Plug
70
70
  : Object.values(builtInPlugins)
71
71
  ) as Plugin<Schema>[];
72
72
 
73
- const schemaTypes = template.schemas.flatMap((schemaObj) =>
73
+ const schemaTypes = schemas.flatMap((schemaObj) =>
74
74
  Object.values(schemaObj).map((schema) => schema.type)
75
75
  );
76
76
 
@@ -82,9 +82,18 @@ export const preprocessing = async (arg: { template: Template; userPlugins: Plug
82
82
  Check this document: https://pdfme.com/docs/custom-schemas`);
83
83
  }
84
84
  return { ...acc, [type]: render.pdf };
85
- }, {} as Record<string, (arg: PDFRenderProps<Schema>) => Promise<void>>);
86
-
87
- return { pdfDoc, embeddedPages, embedPdfBoxes, renderObj };
85
+ }, {} as Record<string, (arg: PDFRenderProps<Schema>) => Promise<void> | void>);
86
+
87
+ const readOnlySchemaKeys = schemas.reduce((acc, schema) => {
88
+ const entries = Object.entries(schema);
89
+ const keys = entries.reduce(
90
+ (acc, [key, value]) => (value.readOnly ? [...acc, key] : acc),
91
+ [] as string[]
92
+ );
93
+ return [...acc, ...keys];
94
+ }, [] as string[]);
95
+
96
+ return { pdfDoc, embeddedPages, embedPdfBoxes, renderObj, readOnlySchemaKeys };
88
97
  };
89
98
 
90
99
  export const postProcessing = (props: {