@pdfme/common 1.1.10 → 1.2.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.
Files changed (40) hide show
  1. package/dist/cjs/__tests__/calculateDynamicFontSize.test.js +97 -0
  2. package/dist/cjs/__tests__/calculateDynamicFontSize.test.js.map +1 -0
  3. package/dist/cjs/src/calculateDynamicFontSize.js +103 -0
  4. package/dist/cjs/src/calculateDynamicFontSize.js.map +1 -0
  5. package/dist/cjs/src/constants.js +6 -2
  6. package/dist/cjs/src/constants.js.map +1 -1
  7. package/dist/cjs/src/helper.js +3 -10
  8. package/dist/cjs/src/helper.js.map +1 -1
  9. package/dist/cjs/src/index.js +8 -2
  10. package/dist/cjs/src/index.js.map +1 -1
  11. package/dist/cjs/src/schema.js +11 -6
  12. package/dist/cjs/src/schema.js.map +1 -1
  13. package/dist/cjs/src/type.js.map +1 -1
  14. package/dist/esm/__tests__/calculateDynamicFontSize.test.js +72 -0
  15. package/dist/esm/__tests__/calculateDynamicFontSize.test.js.map +1 -0
  16. package/dist/esm/src/calculateDynamicFontSize.js +76 -0
  17. package/dist/esm/src/calculateDynamicFontSize.js.map +1 -0
  18. package/dist/esm/src/constants.js +5 -1
  19. package/dist/esm/src/constants.js.map +1 -1
  20. package/dist/esm/src/helper.js +4 -11
  21. package/dist/esm/src/helper.js.map +1 -1
  22. package/dist/esm/src/index.js +3 -2
  23. package/dist/esm/src/index.js.map +1 -1
  24. package/dist/esm/src/schema.js +10 -5
  25. package/dist/esm/src/schema.js.map +1 -1
  26. package/dist/esm/src/type.js.map +1 -1
  27. package/dist/types/__tests__/calculateDynamicFontSize.test.d.ts +1 -0
  28. package/dist/types/src/calculateDynamicFontSize.d.ts +6 -0
  29. package/dist/types/src/constants.d.ts +5 -1
  30. package/dist/types/src/index.d.ts +5 -4
  31. package/dist/types/src/schema.d.ts +386 -43
  32. package/dist/types/src/type.d.ts +6 -1
  33. package/package.json +7 -2
  34. package/src/calculateDynamicFontSize.ts +101 -0
  35. package/src/constants.ts +6 -2
  36. package/src/helper.ts +4 -11
  37. package/src/index.ts +14 -2
  38. package/src/schema.ts +12 -5
  39. package/src/type.ts +2 -0
  40. package/tsconfig.json +6 -0
package/src/helper.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { z } from 'zod';
2
- import { HELVETICA } from './constants.js';
2
+ import { Buffer } from 'buffer';
3
+ import { DEFAULT_FONT_NAME, DEFAULT_FONT_VALUE } from './constants.js';
3
4
  import { Template, Schema, BasePdf, Font, CommonProps, isTextSchema, BarCodeType } from './type.js';
4
5
  import {
5
6
  Inputs as InputsSchema,
@@ -11,8 +12,6 @@ import {
11
12
  UIProps as UIPropsSchema,
12
13
  } from './schema.js';
13
14
 
14
- const DEFAULT_FONT_NAME = 'Helvetica';
15
-
16
15
  const blob2Base64Pdf = (blob: Blob) => {
17
16
  return new Promise<string>((resolve, reject) => {
18
17
  const reader = new FileReader();
@@ -42,13 +41,7 @@ export const getB64BasePdf = (basePdf: BasePdf) => {
42
41
  return basePdf as string;
43
42
  };
44
43
 
45
- const getByteString = (base64: string) => {
46
- if (typeof window !== 'undefined' && window.atob) {
47
- return window.atob(base64);
48
- } else {
49
- return Buffer.from(base64, 'base64').toString('binary');
50
- }
51
- };
44
+ const getByteString = (base64: string) => Buffer.from(base64, 'base64').toString('binary');
52
45
 
53
46
  export const b64toUint8Array = (base64: string) => {
54
47
  const data = base64.split(';base64,')[1] ? base64.split(';base64,')[1] : base64;
@@ -77,7 +70,7 @@ export const getFallbackFontName = (font: Font) => {
77
70
  };
78
71
 
79
72
  export const getDefaultFont = (): Font => ({
80
- [DEFAULT_FONT_NAME]: { data: b64toUint8Array(HELVETICA), fallback: true },
73
+ [DEFAULT_FONT_NAME]: { data: b64toUint8Array(DEFAULT_FONT_VALUE), fallback: true },
81
74
  });
82
75
 
83
76
  const uniq = <T>(array: Array<T>) => Array.from(new Set(array));
package/src/index.ts CHANGED
@@ -1,11 +1,15 @@
1
1
  import {
2
+ DEFAULT_FONT_NAME,
2
3
  DEFAULT_FONT_SIZE,
3
4
  DEFAULT_ALIGNMENT,
4
5
  DEFAULT_LINE_HEIGHT,
5
6
  DEFAULT_CHARACTER_SPACING,
6
7
  DEFAULT_FONT_COLOR,
8
+ DEFAULT_TOLERANCE,
9
+ DEFAULT_FONT_SIZE_ADJUSTMENT,
10
+ DEFAULT_PT_TO_MM_RATIO,
7
11
  BLANK_PDF,
8
- HELVETICA,
12
+ DEFAULT_FONT_VALUE,
9
13
  } from './constants.js';
10
14
  import { schemaTypes, isImageSchema, isBarcodeSchema, isTextSchema } from './type.js';
11
15
  import type {
@@ -18,6 +22,7 @@ import type {
18
22
  ImageSchema,
19
23
  BarcodeSchema,
20
24
  Schema,
25
+ SchemaInputs,
21
26
  SchemaForUI,
22
27
  Font,
23
28
  BasePdf,
@@ -47,15 +52,20 @@ import {
47
52
  checkGenerateProps,
48
53
  validateBarcodeInput,
49
54
  } from './helper.js';
55
+ import { calculateDynamicFontSize } from './calculateDynamicFontSize.js';
50
56
 
51
57
  export {
58
+ DEFAULT_FONT_NAME,
52
59
  DEFAULT_FONT_SIZE,
53
60
  DEFAULT_ALIGNMENT,
54
61
  DEFAULT_LINE_HEIGHT,
55
62
  DEFAULT_CHARACTER_SPACING,
56
63
  DEFAULT_FONT_COLOR,
64
+ DEFAULT_TOLERANCE,
65
+ DEFAULT_FONT_SIZE_ADJUSTMENT,
66
+ DEFAULT_PT_TO_MM_RATIO,
57
67
  BLANK_PDF,
58
- HELVETICA,
68
+ DEFAULT_FONT_VALUE,
59
69
  schemaTypes,
60
70
  isTextSchema,
61
71
  isImageSchema,
@@ -73,6 +83,7 @@ export {
73
83
  checkDesignerProps,
74
84
  checkGenerateProps,
75
85
  validateBarcodeInput,
86
+ calculateDynamicFontSize,
76
87
  };
77
88
 
78
89
  export type {
@@ -85,6 +96,7 @@ export type {
85
96
  ImageSchema,
86
97
  BarcodeSchema,
87
98
  Schema,
99
+ SchemaInputs,
88
100
  SchemaForUI,
89
101
  Font,
90
102
  BasePdf,
package/src/schema.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /* eslint dot-notation: "off"*/
2
2
  import { z } from 'zod';
3
3
 
4
- const langs = ['en', 'ja'] as const;
4
+ const langs = ['en', 'ja', 'ar'] as const;
5
5
  export const Lang = z.enum(langs);
6
6
 
7
7
  export const Size = z.object({ height: z.number(), width: z.number() });
@@ -34,6 +34,10 @@ export const TextSchema = CommonSchema.extend({
34
34
  backgroundColor: z.string().optional(),
35
35
  characterSpacing: z.number().optional(),
36
36
  lineHeight: z.number().optional(),
37
+ dynamicFontSize: z.object({
38
+ max: z.number(),
39
+ min: z.number(),
40
+ }).optional(),
37
41
  });
38
42
 
39
43
  export const ImageSchema = CommonSchema.extend({ type: z.literal(SchemaType.Enum.image) });
@@ -42,7 +46,10 @@ export const BarcodeSchema = CommonSchema.extend({ type: BarcodeSchemaType });
42
46
 
43
47
  export const Schema = z.union([TextSchema, ImageSchema, BarcodeSchema]);
44
48
 
45
- const SchemaForUIAdditionalInfo = z.object({ id: z.string(), key: z.string(), data: z.string() });
49
+ const SchemaForUIAdditionalInfo = z.object({
50
+ id: z.string(), key: z.string(), data: z.string(),
51
+ });
52
+
46
53
  export const SchemaForUI = z.union([
47
54
  TextSchema.merge(SchemaForUIAdditionalInfo),
48
55
  ImageSchema.merge(SchemaForUIAdditionalInfo),
@@ -80,15 +87,15 @@ export const CommonProps = z.object({
80
87
 
81
88
  // -------------------generate-------------------
82
89
 
83
- export const GeneratorOptions = CommonOptions.extend({
84
- splitThreshold: z.number().optional(),
85
- });
90
+ export const GeneratorOptions = CommonOptions;
86
91
 
87
92
  export const GenerateProps = CommonProps.extend({
88
93
  inputs: Inputs,
89
94
  options: GeneratorOptions.optional(),
90
95
  }).strict();
91
96
 
97
+ export const SchemaInputs = z.record(z.string());
98
+
92
99
  // ---------------------------------------------
93
100
 
94
101
  export const UIOptions = CommonOptions.extend({ lang: Lang.optional() });
package/src/type.ts CHANGED
@@ -12,6 +12,7 @@ import {
12
12
  ImageSchema,
13
13
  BarcodeSchema,
14
14
  Schema,
15
+ SchemaInputs,
15
16
  SchemaForUI,
16
17
  Font,
17
18
  BasePdf,
@@ -43,6 +44,7 @@ export type TextSchema = z.infer<typeof TextSchema>;
43
44
  export type ImageSchema = z.infer<typeof ImageSchema>;
44
45
  export type BarcodeSchema = z.infer<typeof BarcodeSchema>;
45
46
  export type Schema = z.infer<typeof Schema>;
47
+ export type SchemaInputs = z.infer<typeof SchemaInputs>;
46
48
  export type SchemaForUI = z.infer<typeof SchemaForUI>;
47
49
  export type Font = z.infer<typeof Font>;
48
50
  export type BasePdf = z.infer<typeof BasePdf>;
package/tsconfig.json ADDED
@@ -0,0 +1,6 @@
1
+ {
2
+ "extends": "./tsconfig.esm",
3
+ "compilerOptions": {
4
+ "outDir": "./dist",
5
+ }
6
+ }