@pdfme/generator 1.0.0-beta.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.
@@ -0,0 +1,64 @@
1
+ import { z } from 'zod';
2
+ import { Lang, Size, Alignment, BarcodeSchemaType, SchemaType, CommonSchema as _CommonSchema, TextSchema, ImageSchema, BarcodeSchema, Schema, SchemaForUI, Font, BasePdf, Template, CommonProps, GeneratorOptions, GenerateProps, UIOptions, UIProps, PreviewProps, PreviewReactProps, DesignerProps, DesignerReactProps } from './schema';
3
+ export declare type Lang = z.infer<typeof Lang>;
4
+ export declare type Size = z.infer<typeof Size>;
5
+ export declare type Alignment = z.infer<typeof Alignment>;
6
+ export declare type SchemaType = z.infer<typeof SchemaType>;
7
+ export declare const schemaTypes: readonly ["text", "image", "qrcode", "japanpost", "ean13", "ean8", "code39", "code128", "nw7", "itf14", "upca", "upce"];
8
+ export declare type BarCodeType = z.infer<typeof BarcodeSchemaType>;
9
+ export declare type TextSchema = z.infer<typeof TextSchema>;
10
+ declare type CommonSchema = z.infer<typeof _CommonSchema>;
11
+ export declare const isTextSchema: (arg: CommonSchema) => arg is {
12
+ rotate?: number | undefined;
13
+ alignment?: "left" | "center" | "right" | undefined;
14
+ fontSize?: number | undefined;
15
+ fontName?: string | undefined;
16
+ fontColor?: string | undefined;
17
+ backgroundColor?: string | undefined;
18
+ characterSpacing?: number | undefined;
19
+ lineHeight?: number | undefined;
20
+ type: "text";
21
+ height: number;
22
+ width: number;
23
+ position: {
24
+ x: number;
25
+ y: number;
26
+ };
27
+ };
28
+ export declare type ImageSchema = z.infer<typeof ImageSchema>;
29
+ export declare const isImageSchema: (arg: CommonSchema) => arg is {
30
+ rotate?: number | undefined;
31
+ type: "image";
32
+ height: number;
33
+ width: number;
34
+ position: {
35
+ x: number;
36
+ y: number;
37
+ };
38
+ };
39
+ export declare type BarcodeSchema = z.infer<typeof BarcodeSchema>;
40
+ export declare const isBarcodeSchema: (arg: CommonSchema) => arg is {
41
+ rotate?: number | undefined;
42
+ type: "qrcode" | "japanpost" | "ean13" | "ean8" | "code39" | "code128" | "nw7" | "itf14" | "upca" | "upce";
43
+ height: number;
44
+ width: number;
45
+ position: {
46
+ x: number;
47
+ y: number;
48
+ };
49
+ };
50
+ export declare type Schema = z.infer<typeof Schema>;
51
+ export declare type SchemaForUI = z.infer<typeof SchemaForUI>;
52
+ export declare type Font = z.infer<typeof Font>;
53
+ export declare type BasePdf = z.infer<typeof BasePdf>;
54
+ export declare type Template = z.infer<typeof Template>;
55
+ export declare type CommonProps = z.infer<typeof CommonProps>;
56
+ export declare type GeneratorOptions = z.infer<typeof GeneratorOptions>;
57
+ export declare type GenerateProps = z.infer<typeof GenerateProps>;
58
+ export declare type UIOptions = z.infer<typeof UIOptions>;
59
+ export declare type UIProps = z.infer<typeof UIProps>;
60
+ export declare type PreviewProps = z.infer<typeof PreviewProps>;
61
+ export declare type PreviewReactProps = z.infer<typeof PreviewReactProps>;
62
+ export declare type DesignerProps = z.infer<typeof DesignerProps>;
63
+ export declare type DesignerReactProps = z.infer<typeof DesignerReactProps>;
64
+ export {};
@@ -0,0 +1,12 @@
1
+ export declare const uuid: () => string;
2
+ export declare const set: <T extends object>(obj: T, path: string | string[], value: any) => void;
3
+ export declare const debounce: <T extends Function>(cb: T, wait?: number) => T;
4
+ export declare const cloneDeep: <T>(value: T) => T;
5
+ export declare const uniq: <T>(array: T[]) => T[];
6
+ export declare const round: (number: number, precision: number) => number;
7
+ export declare const b64toUint8Array: (base64: string) => Uint8Array;
8
+ export declare const b64toBlob: (base64: string) => Blob;
9
+ export declare const arrayMove: <T>(array: T[], from: number, to: number) => T[];
10
+ export declare const flatten: <T>(arr: T[][]) => T[];
11
+ export declare const pt2mm: (pt: number) => number;
12
+ export declare const mm2pt: (mm: number) => number;
@@ -0,0 +1 @@
1
+ export declare const TOOL_NAME: "pdfme (https://github.com/hand-dot/pdfme)";
@@ -0,0 +1,3 @@
1
+ import { GenerateProps } from '@pdfme/common';
2
+ declare const generate: (props: GenerateProps) => Promise<Uint8Array>;
3
+ export default generate;
@@ -0,0 +1,67 @@
1
+ import { PDFPage, PDFFont, PDFDocument, PDFImage, PDFEmbeddedPage } from 'pdf-lib';
2
+ import { Schema, Font, BasePdf, BarCodeType } from '../../common/src/type';
3
+ export interface InputImageCache {
4
+ [key: string]: PDFImage;
5
+ }
6
+ export declare const createBarCode: ({ type, input, width, height, backgroundColor, }: {
7
+ type: BarCodeType;
8
+ input: string;
9
+ width: number;
10
+ height: number;
11
+ backgroundColor?: string | undefined;
12
+ }) => Promise<Buffer>;
13
+ declare type EmbedPdfBox = {
14
+ mediaBox: {
15
+ x: number;
16
+ y: number;
17
+ width: number;
18
+ height: number;
19
+ };
20
+ bleedBox: {
21
+ x: number;
22
+ y: number;
23
+ width: number;
24
+ height: number;
25
+ };
26
+ trimBox: {
27
+ x: number;
28
+ y: number;
29
+ width: number;
30
+ height: number;
31
+ };
32
+ };
33
+ export declare const embedAndGetFontObj: (arg: {
34
+ pdfDoc: PDFDocument;
35
+ font: Font;
36
+ }) => Promise<{
37
+ [key: string]: PDFFont;
38
+ }>;
39
+ export declare const getEmbeddedPagesAndEmbedPdfBoxes: (arg: {
40
+ pdfDoc: PDFDocument;
41
+ basePdf: BasePdf;
42
+ }) => Promise<{
43
+ embeddedPages: PDFEmbeddedPage[];
44
+ embedPdfBoxes: EmbedPdfBox[];
45
+ }>;
46
+ interface TextSchemaSetting {
47
+ fontObj: {
48
+ [key: string]: PDFFont;
49
+ };
50
+ fallbackFontName: string;
51
+ splitThreshold: number;
52
+ }
53
+ export declare const drawInputByTemplateSchema: (arg: {
54
+ input: string;
55
+ templateSchema: Schema;
56
+ pdfDoc: PDFDocument;
57
+ page: PDFPage;
58
+ pageHeight: number;
59
+ textSchemaSetting: TextSchemaSetting;
60
+ inputImageCache: InputImageCache;
61
+ }) => Promise<void>;
62
+ export declare const drawEmbeddedPage: (arg: {
63
+ page: PDFPage;
64
+ embeddedPage: PDFEmbeddedPage;
65
+ embedPdfBox: EmbedPdfBox;
66
+ }) => void;
67
+ export {};
@@ -0,0 +1,68 @@
1
+ /// <reference types="node" />
2
+ import { PDFPage, PDFFont, PDFDocument, PDFImage, PDFEmbeddedPage } from 'pdf-lib';
3
+ import { Schema, Font, BasePdf, BarCodeType } from '@pdfme/common';
4
+ export interface InputImageCache {
5
+ [key: string]: PDFImage;
6
+ }
7
+ export declare const createBarCode: ({ type, input, width, height, backgroundColor, }: {
8
+ type: BarCodeType;
9
+ input: string;
10
+ width: number;
11
+ height: number;
12
+ backgroundColor?: string | undefined;
13
+ }) => Promise<Buffer>;
14
+ declare type EmbedPdfBox = {
15
+ mediaBox: {
16
+ x: number;
17
+ y: number;
18
+ width: number;
19
+ height: number;
20
+ };
21
+ bleedBox: {
22
+ x: number;
23
+ y: number;
24
+ width: number;
25
+ height: number;
26
+ };
27
+ trimBox: {
28
+ x: number;
29
+ y: number;
30
+ width: number;
31
+ height: number;
32
+ };
33
+ };
34
+ export declare const embedAndGetFontObj: (arg: {
35
+ pdfDoc: PDFDocument;
36
+ font: Font;
37
+ }) => Promise<{
38
+ [key: string]: PDFFont;
39
+ }>;
40
+ export declare const getEmbeddedPagesAndEmbedPdfBoxes: (arg: {
41
+ pdfDoc: PDFDocument;
42
+ basePdf: BasePdf;
43
+ }) => Promise<{
44
+ embeddedPages: PDFEmbeddedPage[];
45
+ embedPdfBoxes: EmbedPdfBox[];
46
+ }>;
47
+ interface TextSchemaSetting {
48
+ fontObj: {
49
+ [key: string]: PDFFont;
50
+ };
51
+ fallbackFontName: string;
52
+ splitThreshold: number;
53
+ }
54
+ export declare const drawInputByTemplateSchema: (arg: {
55
+ input: string;
56
+ templateSchema: Schema;
57
+ pdfDoc: PDFDocument;
58
+ page: PDFPage;
59
+ pageHeight: number;
60
+ textSchemaSetting: TextSchemaSetting;
61
+ inputImageCache: InputImageCache;
62
+ }) => Promise<void>;
63
+ export declare const drawEmbeddedPage: (arg: {
64
+ page: PDFPage;
65
+ embeddedPage: PDFEmbeddedPage;
66
+ embedPdfBox: EmbedPdfBox;
67
+ }) => void;
68
+ export {};
@@ -0,0 +1,3 @@
1
+ import generate from './generate';
2
+ import { BLANK_PDF, Lang, Size, Alignment, SchemaType, schemaTypes, BarCodeType, TextSchema, isTextSchema, ImageSchema, isImageSchema, BarcodeSchema, isBarcodeSchema, Schema, SchemaForUI, Font, BasePdf, Template, CommonProps, GeneratorOptions, GenerateProps, UIOptions, UIProps, PreviewProps, PreviewReactProps, DesignerProps, DesignerReactProps, checkTemplate, checkUIProps, checkPreviewProps, checkDesignerProps, checkGenerateProps, validateBarcodeInput } from '@pdfme/common';
3
+ export { generate, BLANK_PDF, Lang, Size, Alignment, SchemaType, schemaTypes, BarCodeType, TextSchema, isTextSchema, ImageSchema, isImageSchema, BarcodeSchema, isBarcodeSchema, Schema, SchemaForUI, Font, BasePdf, Template, CommonProps, GeneratorOptions, GenerateProps, UIOptions, UIProps, PreviewProps, PreviewReactProps, DesignerProps, DesignerReactProps, checkTemplate, checkUIProps, checkPreviewProps, checkDesignerProps, checkGenerateProps, validateBarcodeInput, };
package/package.json ADDED
@@ -0,0 +1,61 @@
1
+ {
2
+ "name": "@pdfme/generator",
3
+ "version": "1.0.0-beta.2",
4
+ "author": "hand-dot",
5
+ "license": "MIT",
6
+ "description": "TypeScript base PDF generator and React base UI. Open source, developed by the community, and completely free to use under the MIT license!",
7
+ "homepage": "https://pdfme.com",
8
+ "repository": "git@github.com:pdfme/pdfme.git",
9
+ "bugs": {
10
+ "url": "https://github.com/pdfme/pdfme/issues"
11
+ },
12
+ "main": "dist/@pdfme/generator.js",
13
+ "module": "src/index.ts",
14
+ "types": "dist/types/index.d.ts",
15
+ "engines": {
16
+ "node": ">=14"
17
+ },
18
+ "scripts": {
19
+ "develop": "webpack-dev-server --mode development",
20
+ "build": "NODE_ENV=production webpack --mode production",
21
+ "clean": "rimraf dist",
22
+ "lint": "tsc --noEmit",
23
+ "test": "jest",
24
+ "lint:watch": "tsc -w --noEmit",
25
+ "test:watch": "jest --coverage --watch",
26
+ "prune": "ts-prune src"
27
+ },
28
+ "dependencies": {
29
+ "@pdfme/common": "^1.0.0-beta.1",
30
+ "@pdf-lib/fontkit": "^1.1.1",
31
+ "pdf-lib": "^1.17.1",
32
+ "bwip-js": "^3.0.4"
33
+ },
34
+ "devDependencies": {
35
+ "@types/bwip-js": "^3.0.0",
36
+ "@types/pngjs": "^6.0.1",
37
+ "jsqr": "^1.4.0",
38
+ "pdf2json": "^2.0.0",
39
+ "pngjs": "^6.0.0"
40
+ },
41
+ "jest": {
42
+ "moduleNameMapper": {
43
+ "\\.(ttf)$": "<rootDir>/../../fontTransformer.js"
44
+ },
45
+ "moduleFileExtensions": [
46
+ "js",
47
+ "ts"
48
+ ],
49
+ "transform": {
50
+ "^.+\\.ts$": "ts-jest"
51
+ },
52
+ "globals": {
53
+ "ts-jest": {
54
+ "tsconfig": "tsconfig.json"
55
+ }
56
+ },
57
+ "testMatch": [
58
+ "**/*.test.ts"
59
+ ]
60
+ }
61
+ }
@@ -0,0 +1 @@
1
+ export const TOOL_NAME = 'pdfme (https://github.com/hand-dot/pdfme)' as const;
@@ -0,0 +1,94 @@
1
+ import { PDFDocument } from 'pdf-lib';
2
+ import fontkit from '@pdf-lib/fontkit';
3
+ import {
4
+ GenerateProps,
5
+ Template,
6
+ Font,
7
+ getDefaultFont,
8
+ getFallbackFontName,
9
+ checkGenerateProps,
10
+ } from '@pdfme/common';
11
+
12
+ import {
13
+ getEmbeddedPagesAndEmbedPdfBoxes,
14
+ drawInputByTemplateSchema,
15
+ drawEmbeddedPage,
16
+ embedAndGetFontObj,
17
+ InputImageCache,
18
+ } from './helper';
19
+ import { TOOL_NAME } from './constants';
20
+
21
+ const preprocessing = async (arg: {
22
+ inputs: { [key: string]: string }[];
23
+ template: Template;
24
+ font: Font;
25
+ }) => {
26
+ const { template, font } = arg;
27
+
28
+ const { basePdf } = template;
29
+
30
+ const pdfDoc = await PDFDocument.create();
31
+ pdfDoc.registerFontkit(fontkit);
32
+
33
+ const fallbackFontName = getFallbackFontName(font);
34
+ const fontObj = await embedAndGetFontObj({ pdfDoc, font });
35
+
36
+ const pagesAndBoxes = await getEmbeddedPagesAndEmbedPdfBoxes({ pdfDoc, basePdf });
37
+ const { embeddedPages, embedPdfBoxes } = pagesAndBoxes;
38
+
39
+ return { pdfDoc, fontObj, fallbackFontName, embeddedPages, embedPdfBoxes };
40
+ };
41
+
42
+ const postProcessing = (pdfDoc: PDFDocument) => {
43
+ pdfDoc.setProducer(TOOL_NAME);
44
+ pdfDoc.setCreator(TOOL_NAME);
45
+ };
46
+
47
+ const generate = async (props: GenerateProps) => {
48
+ checkGenerateProps(props);
49
+ const { inputs, template, options = {} } = props;
50
+ const { font = getDefaultFont(), splitThreshold = 3 } = options;
51
+ const { schemas } = template;
52
+
53
+ const preRes = await preprocessing({ inputs, template, font });
54
+ const { pdfDoc, fontObj, fallbackFontName, embeddedPages, embedPdfBoxes } = preRes;
55
+
56
+ const inputImageCache: InputImageCache = {};
57
+ for (let i = 0; i < inputs.length; i += 1) {
58
+ const inputObj = inputs[i];
59
+ const keys = Object.keys(inputObj);
60
+ for (let j = 0; j < embeddedPages.length; j += 1) {
61
+ const embeddedPage = embeddedPages[j];
62
+ const { width: pageWidth, height: pageHeight } = embeddedPage;
63
+ const embedPdfBox = embedPdfBoxes[j];
64
+
65
+ const page = pdfDoc.addPage([pageWidth, pageHeight]);
66
+
67
+ drawEmbeddedPage({ page, embeddedPage, embedPdfBox });
68
+ for (let l = 0; l < keys.length; l += 1) {
69
+ const key = keys[l];
70
+ const schema = schemas[j];
71
+ const templateSchema = schema[key];
72
+ const input = inputObj[key];
73
+ const textSchemaSetting = { fontObj, fallbackFontName, splitThreshold };
74
+
75
+ // eslint-disable-next-line no-await-in-loop
76
+ await drawInputByTemplateSchema({
77
+ input,
78
+ templateSchema,
79
+ pdfDoc,
80
+ page,
81
+ pageHeight,
82
+ textSchemaSetting,
83
+ inputImageCache,
84
+ });
85
+ }
86
+ }
87
+ }
88
+
89
+ postProcessing(pdfDoc);
90
+
91
+ return pdfDoc.save();
92
+ };
93
+
94
+ export default generate;