@pdfme/common 2.2.0 → 2.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.
- package/dist/cjs/__tests__/barcode.test.js +107 -0
- package/dist/cjs/__tests__/barcode.test.js.map +1 -0
- package/dist/cjs/__tests__/font.test.js +33 -32
- package/dist/cjs/__tests__/font.test.js.map +1 -1
- package/dist/cjs/__tests__/helper.test.js +2 -0
- package/dist/cjs/__tests__/helper.test.js.map +1 -1
- package/dist/cjs/src/barcode.js +53 -0
- package/dist/cjs/src/barcode.js.map +1 -0
- package/dist/cjs/src/constants.js +4 -2
- package/dist/cjs/src/constants.js.map +1 -1
- package/dist/cjs/src/font.js +14 -8
- package/dist/cjs/src/font.js.map +1 -1
- package/dist/cjs/src/helper.js +1 -0
- package/dist/cjs/src/helper.js.map +1 -1
- package/dist/cjs/src/index.js +5 -1
- package/dist/cjs/src/index.js.map +1 -1
- package/dist/cjs/src/schema.js +7 -2
- package/dist/cjs/src/schema.js.map +1 -1
- package/dist/esm/__tests__/barcode.test.js +102 -0
- package/dist/esm/__tests__/barcode.test.js.map +1 -0
- package/dist/esm/__tests__/font.test.js +33 -32
- package/dist/esm/__tests__/font.test.js.map +1 -1
- package/dist/esm/__tests__/helper.test.js +2 -0
- package/dist/esm/__tests__/helper.test.js.map +1 -1
- package/dist/esm/src/barcode.js +44 -0
- package/dist/esm/src/barcode.js.map +1 -0
- package/dist/esm/src/constants.js +3 -1
- package/dist/esm/src/constants.js.map +1 -1
- package/dist/esm/src/font.js +14 -8
- package/dist/esm/src/font.js.map +1 -1
- package/dist/esm/src/helper.js +1 -0
- package/dist/esm/src/helper.js.map +1 -1
- package/dist/esm/src/index.js +3 -2
- package/dist/esm/src/index.js.map +1 -1
- package/dist/esm/src/schema.js +7 -2
- package/dist/esm/src/schema.js.map +1 -1
- package/dist/types/__tests__/barcode.test.d.ts +1 -0
- package/dist/types/src/barcode.d.ts +19 -0
- package/dist/types/src/constants.d.ts +3 -1
- package/dist/types/src/font.d.ts +2 -2
- package/dist/types/src/index.d.ts +3 -2
- package/dist/types/src/schema.d.ts +239 -29
- package/dist/types/src/type.d.ts +28 -25
- package/package.json +9 -4
- package/src/barcode.ts +51 -0
- package/src/constants.ts +3 -1
- package/src/font.ts +14 -12
- package/src/helper.ts +1 -0
- package/src/index.ts +6 -0
- package/src/schema.ts +8 -2
package/dist/types/src/type.d.ts
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
import { z } from 'zod';
|
2
2
|
import type { Font as FontKitFont } from 'fontkit';
|
3
3
|
import { Lang, Size, Alignment, BarcodeSchemaType, SchemaType, CommonSchema as _CommonSchema, TextSchema, ImageSchema, BarcodeSchema, Schema, SchemaInputs, SchemaForUI, Font, BasePdf, Template, CommonProps, GeneratorOptions, GenerateProps, UIOptions, UIProps, PreviewProps, PreviewReactProps, DesignerProps, DesignerReactProps } from './schema.js';
|
4
|
-
export
|
4
|
+
export type FontWidthCalcValues = {
|
5
5
|
font: FontKitFont;
|
6
6
|
fontSize: number;
|
7
7
|
characterSpacing: number;
|
8
8
|
boxWidthInPt: number;
|
9
9
|
};
|
10
|
-
|
10
|
+
type CommonSchema = z.infer<typeof _CommonSchema>;
|
11
11
|
export declare const schemaTypes: readonly ["text", "image", "qrcode", "japanpost", "ean13", "ean8", "code39", "code128", "nw7", "itf14", "upca", "upce", "gs1datamatrix"];
|
12
12
|
export declare const isTextSchema: (arg: CommonSchema) => arg is {
|
13
13
|
type: "text";
|
@@ -51,28 +51,31 @@ export declare const isBarcodeSchema: (arg: CommonSchema) => arg is {
|
|
51
51
|
y: number;
|
52
52
|
};
|
53
53
|
rotate?: number | undefined;
|
54
|
+
backgroundcolor?: string | undefined;
|
55
|
+
barcolor?: string | undefined;
|
56
|
+
textcolor?: string | undefined;
|
54
57
|
};
|
55
|
-
export
|
56
|
-
export
|
57
|
-
export
|
58
|
-
export
|
59
|
-
export
|
60
|
-
export
|
61
|
-
export
|
62
|
-
export
|
63
|
-
export
|
64
|
-
export
|
65
|
-
export
|
66
|
-
export
|
67
|
-
export
|
68
|
-
export
|
69
|
-
export
|
70
|
-
export
|
71
|
-
export
|
72
|
-
export
|
73
|
-
export
|
74
|
-
export
|
75
|
-
export
|
76
|
-
export
|
77
|
-
export
|
58
|
+
export type Lang = z.infer<typeof Lang>;
|
59
|
+
export type Size = z.infer<typeof Size>;
|
60
|
+
export type Alignment = z.infer<typeof Alignment>;
|
61
|
+
export type SchemaType = z.infer<typeof SchemaType>;
|
62
|
+
export type BarCodeType = z.infer<typeof BarcodeSchemaType>;
|
63
|
+
export type TextSchema = z.infer<typeof TextSchema>;
|
64
|
+
export type ImageSchema = z.infer<typeof ImageSchema>;
|
65
|
+
export type BarcodeSchema = z.infer<typeof BarcodeSchema>;
|
66
|
+
export type Schema = z.infer<typeof Schema>;
|
67
|
+
export type SchemaInputs = z.infer<typeof SchemaInputs>;
|
68
|
+
export type SchemaForUI = z.infer<typeof SchemaForUI>;
|
69
|
+
export type Font = z.infer<typeof Font>;
|
70
|
+
export type BasePdf = z.infer<typeof BasePdf>;
|
71
|
+
export type Template = z.infer<typeof Template>;
|
72
|
+
export type CommonProps = z.infer<typeof CommonProps>;
|
73
|
+
export type GeneratorOptions = z.infer<typeof GeneratorOptions>;
|
74
|
+
export type GenerateProps = z.infer<typeof GenerateProps>;
|
75
|
+
export type UIOptions = z.infer<typeof UIOptions>;
|
76
|
+
export type UIProps = z.infer<typeof UIProps>;
|
77
|
+
export type PreviewProps = z.infer<typeof PreviewProps>;
|
78
|
+
export type PreviewReactProps = z.infer<typeof PreviewReactProps>;
|
79
|
+
export type DesignerProps = z.infer<typeof DesignerProps>;
|
80
|
+
export type DesignerReactProps = z.infer<typeof DesignerReactProps>;
|
78
81
|
export {};
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@pdfme/common",
|
3
|
-
"version": "2.2.
|
3
|
+
"version": "2.2.1",
|
4
4
|
"sideEffects": false,
|
5
5
|
"author": "hand-dot",
|
6
6
|
"license": "MIT",
|
@@ -47,9 +47,17 @@
|
|
47
47
|
},
|
48
48
|
"dependencies": {
|
49
49
|
"buffer": "^6.0.3",
|
50
|
+
"bwip-js": "^3.4.5",
|
50
51
|
"fontkit": "^2.0.2",
|
51
52
|
"zod": "^3.20.2"
|
52
53
|
},
|
54
|
+
"devDependencies": {
|
55
|
+
"@types/bwip-js": "^3.2.0",
|
56
|
+
"@types/fontkit": "^2.0.3",
|
57
|
+
"@types/pngjs": "^6.0.1",
|
58
|
+
"jsqr": "^1.4.0",
|
59
|
+
"pngjs": "^7.0.0"
|
60
|
+
},
|
53
61
|
"jest": {
|
54
62
|
"resolver": "ts-jest-resolver",
|
55
63
|
"moduleFileExtensions": [
|
@@ -70,8 +78,5 @@
|
|
70
78
|
},
|
71
79
|
"publishConfig": {
|
72
80
|
"access": "public"
|
73
|
-
},
|
74
|
-
"devDependencies": {
|
75
|
-
"@types/fontkit": "^2.0.3"
|
76
81
|
}
|
77
82
|
}
|
package/src/barcode.ts
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
import { b64toUint8Array, } from './helper';
|
2
|
+
import { BarCodeType, } from './type';
|
3
|
+
import bwipjs, { ToBufferOptions } from 'bwip-js';
|
4
|
+
import { Buffer } from 'buffer';
|
5
|
+
|
6
|
+
/**
|
7
|
+
* The bwip.js lib has a different name for nw7 type barcodes
|
8
|
+
*/
|
9
|
+
export const barCodeType2Bcid = (type: BarCodeType) =>
|
10
|
+
type === 'nw7' ? 'rationalizedCodabar' : type;
|
11
|
+
|
12
|
+
/**
|
13
|
+
* Strip hash from the beginning of HTML hex color codes for the bwip.js lib
|
14
|
+
*/
|
15
|
+
export const mapHexColorForBwipJsLib = (color: string | undefined, fallback?: string) =>
|
16
|
+
color ? color.replace('#', '') : fallback ? fallback.replace('#', '') : '000000';
|
17
|
+
|
18
|
+
export const createBarCode = async (arg: {
|
19
|
+
type: BarCodeType;
|
20
|
+
input: string;
|
21
|
+
width: number;
|
22
|
+
height: number;
|
23
|
+
backgroundcolor?: string;
|
24
|
+
barcolor?: string;
|
25
|
+
textcolor?: string;
|
26
|
+
}): Promise<Buffer> => {
|
27
|
+
const { type, input, width, height, backgroundcolor, barcolor, textcolor } = arg;
|
28
|
+
const bcid = barCodeType2Bcid(type);
|
29
|
+
const includetext = true;
|
30
|
+
const scale = 5;
|
31
|
+
const bwipjsArg: ToBufferOptions = { bcid, text: input, width, height, scale, includetext };
|
32
|
+
|
33
|
+
if (backgroundcolor) bwipjsArg.backgroundcolor = mapHexColorForBwipJsLib(backgroundcolor);
|
34
|
+
if (barcolor) bwipjsArg.barcolor = mapHexColorForBwipJsLib(barcolor);
|
35
|
+
if (textcolor) bwipjsArg.textcolor = mapHexColorForBwipJsLib(textcolor);
|
36
|
+
|
37
|
+
let res: Buffer;
|
38
|
+
|
39
|
+
if (typeof window !== 'undefined') {
|
40
|
+
const canvas = document.createElement('canvas');
|
41
|
+
bwipjs.toCanvas(canvas, bwipjsArg);
|
42
|
+
const dataUrl = canvas.toDataURL('image/png');
|
43
|
+
res = b64toUint8Array(dataUrl).buffer as Buffer;
|
44
|
+
} else {
|
45
|
+
res = await bwipjs.toBuffer(bwipjsArg);
|
46
|
+
}
|
47
|
+
|
48
|
+
return res;
|
49
|
+
};
|
50
|
+
|
51
|
+
|
package/src/constants.ts
CHANGED
@@ -7,7 +7,9 @@ export const VERTICAL_ALIGN_BOTTOM = 'bottom';
|
|
7
7
|
export const DEFAULT_VERTICAL_ALIGNMENT = VERTICAL_ALIGN_TOP;
|
8
8
|
export const DEFAULT_LINE_HEIGHT = 1;
|
9
9
|
export const DEFAULT_CHARACTER_SPACING = 0;
|
10
|
-
export const DEFAULT_FONT_COLOR = '#
|
10
|
+
export const DEFAULT_FONT_COLOR = '#000000';
|
11
|
+
export const DEFAULT_BARCODE_BG_COLOR = '#ffffff';
|
12
|
+
export const DEFAULT_BARCODE_COLOR = '#000000';
|
11
13
|
export const DYNAMIC_FIT_VERTICAL = 'vertical';
|
12
14
|
export const DYNAMIC_FIT_HORIZONTAL = 'horizontal';
|
13
15
|
export const DEFAULT_DYNAMIC_FIT = DYNAMIC_FIT_VERTICAL;
|
package/src/font.ts
CHANGED
@@ -51,10 +51,7 @@ const getFontNamesInSchemas = (schemas: { [key: string]: Schema }[]) =>
|
|
51
51
|
);
|
52
52
|
|
53
53
|
export const checkFont = (arg: { font: Font; template: Template }) => {
|
54
|
-
const {
|
55
|
-
font,
|
56
|
-
template: { schemas },
|
57
|
-
} = arg;
|
54
|
+
const { font, template: { schemas } } = arg;
|
58
55
|
const fontValues = Object.values(font);
|
59
56
|
const fallbackFontNum = fontValues.reduce((acc, cur) => (cur.fallback ? acc + 1 : acc), 0);
|
60
57
|
if (fallbackFontNum === 0) {
|
@@ -239,12 +236,12 @@ export const getSplittedLines = (textLine: string, calcValues: FontWidthCalcValu
|
|
239
236
|
export const calculateDynamicFontSize = async ({
|
240
237
|
textSchema,
|
241
238
|
font,
|
242
|
-
|
239
|
+
value,
|
243
240
|
startingFontSize,
|
244
241
|
}: {
|
245
242
|
textSchema: TextSchema;
|
246
243
|
font: Font;
|
247
|
-
|
244
|
+
value: string;
|
248
245
|
startingFontSize?: number | undefined;
|
249
246
|
}) => {
|
250
247
|
const {
|
@@ -261,7 +258,7 @@ export const calculateDynamicFontSize = async ({
|
|
261
258
|
|
262
259
|
const characterSpacing = schemaCharacterSpacing ?? DEFAULT_CHARACTER_SPACING;
|
263
260
|
const fontKitFont = await getFontKitFont(textSchema, font);
|
264
|
-
const
|
261
|
+
const paragraphs = value.split('\n');
|
265
262
|
|
266
263
|
let dynamicFontSize = fontSize;
|
267
264
|
if (dynamicFontSize < dynamicFontSizeSetting.min) {
|
@@ -276,17 +273,18 @@ export const calculateDynamicFontSize = async ({
|
|
276
273
|
let totalHeightInMm = 0;
|
277
274
|
|
278
275
|
const boxWidthInPt = mm2pt(boxWidth);
|
279
|
-
const
|
280
|
-
const
|
276
|
+
const firstLineTextHeight = heightOfFontAtSize(fontKitFont, size);
|
277
|
+
const firstLineHeightInMm = pt2mm(firstLineTextHeight * lineHeight);
|
278
|
+
const otherRowHeightInMm = pt2mm(size * lineHeight);
|
281
279
|
|
282
|
-
|
280
|
+
paragraphs.forEach((paragraph, paraIndex) => {
|
283
281
|
const lines = getSplittedLines(paragraph, {
|
284
282
|
font: fontKitFont,
|
285
283
|
fontSize: size,
|
286
284
|
characterSpacing,
|
287
285
|
boxWidthInPt,
|
288
286
|
});
|
289
|
-
lines.forEach((line) => {
|
287
|
+
lines.forEach((line, lineIndex) => {
|
290
288
|
if (dynamicFontFit === DYNAMIC_FIT_VERTICAL) {
|
291
289
|
// For vertical fit we want to consider the width of text lines where we detect a split
|
292
290
|
const textWidth = widthOfTextAtSize(line, fontKitFont, size, characterSpacing);
|
@@ -294,7 +292,11 @@ export const calculateDynamicFontSize = async ({
|
|
294
292
|
totalWidthInMm = Math.max(totalWidthInMm, textWidthInMm);
|
295
293
|
}
|
296
294
|
|
297
|
-
|
295
|
+
if (paraIndex + lineIndex === 0) {
|
296
|
+
totalHeightInMm += firstLineHeightInMm;
|
297
|
+
} else {
|
298
|
+
totalHeightInMm += otherRowHeightInMm;
|
299
|
+
}
|
298
300
|
});
|
299
301
|
if (dynamicFontFit === DYNAMIC_FIT_HORIZONTAL) {
|
300
302
|
// For horizontal fit we want to consider the line's width 'unsplit'
|
package/src/helper.ts
CHANGED
@@ -193,6 +193,7 @@ export const validateBarcodeInput = (type: BarCodeType, input: string) => {
|
|
193
193
|
let res = input.match(regexp);
|
194
194
|
if (
|
195
195
|
res != null &&
|
196
|
+
input.length <= 52 && // 52 is the max length of a GS1 DataMatrix barcode before bwip-js throws an error
|
196
197
|
res[1] === '01' &&
|
197
198
|
(res[2].length === 14 || res[2].length === 8 || res[2].length === 12 || res[2].length === 13)
|
198
199
|
) {
|
package/src/index.ts
CHANGED
@@ -9,6 +9,8 @@ import {
|
|
9
9
|
DEFAULT_LINE_HEIGHT,
|
10
10
|
DEFAULT_CHARACTER_SPACING,
|
11
11
|
DEFAULT_FONT_COLOR,
|
12
|
+
DEFAULT_BARCODE_BG_COLOR,
|
13
|
+
DEFAULT_BARCODE_COLOR,
|
12
14
|
DYNAMIC_FIT_VERTICAL,
|
13
15
|
DYNAMIC_FIT_HORIZONTAL,
|
14
16
|
DEFAULT_DYNAMIC_FIT,
|
@@ -19,6 +21,7 @@ import {
|
|
19
21
|
BLANK_PDF,
|
20
22
|
DEFAULT_FONT_VALUE,
|
21
23
|
} from './constants.js';
|
24
|
+
import { createBarCode } from './barcode';
|
22
25
|
import { schemaTypes, isImageSchema, isBarcodeSchema, isTextSchema } from './type.js';
|
23
26
|
import type {
|
24
27
|
FontWidthCalcValues,
|
@@ -84,6 +87,8 @@ export {
|
|
84
87
|
DEFAULT_LINE_HEIGHT,
|
85
88
|
DEFAULT_CHARACTER_SPACING,
|
86
89
|
DEFAULT_FONT_COLOR,
|
90
|
+
DEFAULT_BARCODE_BG_COLOR,
|
91
|
+
DEFAULT_BARCODE_COLOR,
|
87
92
|
DYNAMIC_FIT_VERTICAL,
|
88
93
|
DYNAMIC_FIT_HORIZONTAL,
|
89
94
|
DEFAULT_DYNAMIC_FIT,
|
@@ -119,6 +124,7 @@ export {
|
|
119
124
|
checkGenerateProps,
|
120
125
|
validateBarcodeInput,
|
121
126
|
calculateDynamicFontSize,
|
127
|
+
createBarCode
|
122
128
|
};
|
123
129
|
|
124
130
|
export type {
|
package/src/schema.ts
CHANGED
@@ -2,7 +2,8 @@
|
|
2
2
|
import { z } from 'zod';
|
3
3
|
import { VERTICAL_ALIGN_TOP, VERTICAL_ALIGN_MIDDLE, VERTICAL_ALIGN_BOTTOM } from "./constants";
|
4
4
|
|
5
|
-
const langs = ['en', 'ja', 'ar', 'th', 'pl'] as const;
|
5
|
+
const langs = ['en', 'ja', 'ar', 'th', 'pl', 'it'] as const;
|
6
|
+
|
6
7
|
export const Lang = z.enum(langs);
|
7
8
|
|
8
9
|
export const Size = z.object({ height: z.number(), width: z.number() });
|
@@ -52,7 +53,12 @@ export const TextSchema = CommonSchema.extend({
|
|
52
53
|
|
53
54
|
export const ImageSchema = CommonSchema.extend({ type: z.literal(SchemaType.Enum.image) });
|
54
55
|
|
55
|
-
export const BarcodeSchema = CommonSchema.extend({
|
56
|
+
export const BarcodeSchema = CommonSchema.extend({
|
57
|
+
type: BarcodeSchemaType,
|
58
|
+
backgroundcolor: z.string().optional(),
|
59
|
+
barcolor: z.string().optional(),
|
60
|
+
textcolor: z.string().optional(),
|
61
|
+
});
|
56
62
|
|
57
63
|
export const Schema = z.union([TextSchema, ImageSchema, BarcodeSchema]);
|
58
64
|
|