@pdfme/generator 2.2.1 → 3.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.
- package/README.md +33 -36
- package/dist/cjs/__tests__/assets/templates/index.js +2 -0
- package/dist/cjs/__tests__/assets/templates/index.js.map +1 -1
- package/dist/cjs/__tests__/generate.test.js +52 -25
- package/dist/cjs/__tests__/generate.test.js.map +1 -1
- package/dist/cjs/src/builtInRenderer.js +2 -5
- package/dist/cjs/src/builtInRenderer.js.map +1 -1
- package/dist/cjs/src/generate.js +10 -9
- package/dist/cjs/src/generate.js.map +1 -1
- package/dist/cjs/src/index.js +1 -13
- package/dist/cjs/src/index.js.map +1 -1
- package/dist/cjs/src/pdfUtils.js +1 -21
- package/dist/cjs/src/pdfUtils.js.map +1 -1
- package/dist/esm/__tests__/assets/templates/index.js +2 -0
- package/dist/esm/__tests__/assets/templates/index.js.map +1 -1
- package/dist/esm/__tests__/generate.test.js +52 -25
- package/dist/esm/__tests__/generate.test.js.map +1 -1
- package/dist/esm/src/builtInRenderer.js +2 -5
- package/dist/esm/src/builtInRenderer.js.map +1 -1
- package/dist/esm/src/generate.js +12 -11
- package/dist/esm/src/generate.js.map +1 -1
- package/dist/esm/src/index.js +1 -2
- package/dist/esm/src/index.js.map +1 -1
- package/dist/esm/src/pdfUtils.js +0 -19
- package/dist/esm/src/pdfUtils.js.map +1 -1
- package/dist/types/__tests__/assets/templates/index.d.ts +725 -1936
- package/dist/types/src/builtInRenderer.d.ts +2 -2
- package/dist/types/src/index.d.ts +1 -4
- package/dist/types/src/pdfUtils.d.ts +2 -6
- package/dist/types/src/types.d.ts +5 -16
- package/package.json +5 -3
- package/src/builtInRenderer.ts +4 -13
- package/src/generate.ts +22 -14
- package/src/index.ts +1 -71
- package/src/pdfUtils.ts +34 -62
- package/src/types.ts +9 -19
- package/tsconfig.cjs.json +3 -2
- package/tsconfig.esm.json +3 -2
- package/tsconfig.json +3 -0
- package/dist/cjs/src/renderUtils.js +0 -65
- package/dist/cjs/src/renderUtils.js.map +0 -1
- package/dist/cjs/src/renders/barcodes.js +0 -37
- package/dist/cjs/src/renders/barcodes.js.map +0 -1
- package/dist/cjs/src/renders/image.js +0 -34
- package/dist/cjs/src/renders/image.js.map +0 -1
- package/dist/cjs/src/renders/text.js +0 -86
- package/dist/cjs/src/renders/text.js.map +0 -1
- package/dist/esm/src/renderUtils.js +0 -56
- package/dist/esm/src/renderUtils.js.map +0 -1
- package/dist/esm/src/renders/barcodes.js +0 -33
- package/dist/esm/src/renders/barcodes.js.map +0 -1
- package/dist/esm/src/renders/image.js +0 -30
- package/dist/esm/src/renders/image.js.map +0 -1
- package/dist/esm/src/renders/text.js +0 -82
- package/dist/esm/src/renders/text.js.map +0 -1
- package/dist/types/src/renderUtils.d.ts +0 -16
- package/dist/types/src/renders/barcodes.d.ts +0 -2
- package/dist/types/src/renders/image.d.ts +0 -2
- package/dist/types/src/renders/text.d.ts +0 -2
- package/src/renderUtils.ts +0 -68
- package/src/renders/barcodes.ts +0 -32
- package/src/renders/image.ts +0 -24
- package/src/renders/text.ts +0 -114
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
declare const renderer:
|
|
1
|
+
import type { PDFRenderer } from './types';
|
|
2
|
+
declare const renderer: PDFRenderer;
|
|
3
3
|
export default renderer;
|
|
@@ -1,5 +1,2 @@
|
|
|
1
1
|
import generate from './generate.js';
|
|
2
|
-
|
|
3
|
-
import type { Lang, Size, Alignment, SchemaType, BarCodeType, TextSchema, ImageSchema, BarcodeSchema, Schema, SchemaForUI, Font, BasePdf, Template, CommonProps, GeneratorOptions, GenerateProps, UIOptions, UIProps, PreviewProps, DesignerProps } from '@pdfme/common';
|
|
4
|
-
export { generate, BLANK_PDF, DEFAULT_FONT_VALUE, isTextSchema, isImageSchema, isBarcodeSchema, checkTemplate, checkUIProps, checkPreviewProps, checkDesignerProps, checkGenerateProps, validateBarcodeInput, };
|
|
5
|
-
export type { Lang, Size, Alignment, SchemaType, BarCodeType, TextSchema, ImageSchema, BarcodeSchema, Schema, SchemaForUI, Font, BasePdf, Template, CommonProps, GeneratorOptions, GenerateProps, UIOptions, UIProps, PreviewProps, DesignerProps, };
|
|
2
|
+
export { generate };
|
|
@@ -1,10 +1,6 @@
|
|
|
1
1
|
import { PDFPage, PDFDocument, PDFEmbeddedPage } from '@pdfme/pdf-lib';
|
|
2
|
-
import {
|
|
3
|
-
import type { EmbedPdfBox } from
|
|
4
|
-
export declare const embedAndGetFontObj: (arg: {
|
|
5
|
-
pdfDoc: PDFDocument;
|
|
6
|
-
font: Font;
|
|
7
|
-
}) => Promise<any>;
|
|
2
|
+
import { BasePdf } from '@pdfme/common';
|
|
3
|
+
import type { EmbedPdfBox } from './types';
|
|
8
4
|
export declare const getEmbeddedPagesAndEmbedPdfBoxes: (arg: {
|
|
9
5
|
pdfDoc: PDFDocument;
|
|
10
6
|
basePdf: BasePdf;
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import type { Schema, PDFRenderProps } from '@pdfme/common';
|
|
2
|
+
export type PDFRender = (arg: PDFRenderProps<Schema>) => Promise<void>;
|
|
3
|
+
export interface PDFRenderer {
|
|
4
|
+
[key: string]: PDFRender | undefined;
|
|
5
|
+
}
|
|
4
6
|
export type EmbedPdfBox = {
|
|
5
7
|
mediaBox: {
|
|
6
8
|
x: number;
|
|
@@ -21,16 +23,3 @@ export type EmbedPdfBox = {
|
|
|
21
23
|
height: number;
|
|
22
24
|
};
|
|
23
25
|
};
|
|
24
|
-
export interface RenderProps {
|
|
25
|
-
value: string;
|
|
26
|
-
schema: Schema;
|
|
27
|
-
pdfDoc: PDFDocument;
|
|
28
|
-
page: PDFPage;
|
|
29
|
-
options: GeneratorOptions;
|
|
30
|
-
_cache: Map<string, PDFImage>;
|
|
31
|
-
}
|
|
32
|
-
export interface Renderer {
|
|
33
|
-
[key: string]: {
|
|
34
|
-
render: (arg: RenderProps) => Promise<void>;
|
|
35
|
-
} | undefined;
|
|
36
|
-
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pdfme/generator",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0-beta.2",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"author": "hand-dot",
|
|
6
6
|
"license": "MIT",
|
|
@@ -43,7 +43,8 @@
|
|
|
43
43
|
"test": "jest",
|
|
44
44
|
"lint:watch": "tsc -w --noEmit",
|
|
45
45
|
"test:watch": "jest --coverage --watch",
|
|
46
|
-
"prune": "ts-prune src"
|
|
46
|
+
"prune": "ts-prune src",
|
|
47
|
+
"prettier": "prettier --write 'src/**/*.ts'"
|
|
47
48
|
},
|
|
48
49
|
"dependencies": {
|
|
49
50
|
"@pdfme/pdf-lib": "^1.17.3",
|
|
@@ -52,10 +53,11 @@
|
|
|
52
53
|
},
|
|
53
54
|
"devDependencies": {
|
|
54
55
|
"@pdfme/common": "file:../common",
|
|
56
|
+
"@pdfme/schemas": "file:../schemas",
|
|
55
57
|
"pdf2json": "^2.0.0"
|
|
56
58
|
},
|
|
57
59
|
"peerDependencies": {
|
|
58
|
-
"@pdfme/common": "
|
|
60
|
+
"@pdfme/common": "latest"
|
|
59
61
|
},
|
|
60
62
|
"jest": {
|
|
61
63
|
"resolver": "ts-jest-resolver",
|
package/src/builtInRenderer.ts
CHANGED
|
@@ -1,14 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
import type {
|
|
3
|
-
import { renderText } from './renders/text';
|
|
4
|
-
import { renderImage } from './renders/image';
|
|
5
|
-
import { renderBarcode } from './renders/barcodes';
|
|
1
|
+
import { text } from '@pdfme/schemas';
|
|
2
|
+
import type { PDFRenderer, PDFRender } from './types';
|
|
6
3
|
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
const renderer: Renderer = {
|
|
10
|
-
text: { render: renderText, },
|
|
11
|
-
image: { render: renderImage, },
|
|
12
|
-
...barCodeTypes.reduce((acc, barcodeType) => Object.assign(acc, { [barcodeType]: { render: renderBarcode } }), {}),
|
|
13
|
-
}
|
|
14
|
-
export default renderer
|
|
4
|
+
const renderer: PDFRenderer = { text: text.pdf as PDFRender };
|
|
5
|
+
export default renderer;
|
package/src/generate.ts
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as pdfLib from '@pdfme/pdf-lib';
|
|
2
2
|
import * as fontkit from 'fontkit';
|
|
3
|
-
import type { GenerateProps, Template
|
|
4
|
-
import { checkGenerateProps
|
|
3
|
+
import type { GenerateProps, Template } from '@pdfme/common';
|
|
4
|
+
import { checkGenerateProps } from '@pdfme/common';
|
|
5
|
+
import type { PDFRenderer } from './types';
|
|
5
6
|
import builtInRenderer from './builtInRenderer';
|
|
6
|
-
import { drawEmbeddedPage, getEmbeddedPagesAndEmbedPdfBoxes
|
|
7
|
+
import { drawEmbeddedPage, getEmbeddedPagesAndEmbedPdfBoxes } from './pdfUtils';
|
|
7
8
|
import { TOOL_NAME } from './constants';
|
|
8
9
|
|
|
9
|
-
const preprocessing = async ({ template }: { template: Template
|
|
10
|
+
const preprocessing = async ({ template }: { template: Template }) => {
|
|
10
11
|
const { basePdf } = template;
|
|
11
12
|
|
|
12
|
-
const pdfDoc = await PDFDocument.create();
|
|
13
|
+
const pdfDoc = await pdfLib.PDFDocument.create();
|
|
13
14
|
// @ts-ignore
|
|
14
15
|
pdfDoc.registerFontkit(fontkit);
|
|
15
16
|
|
|
@@ -19,19 +20,25 @@ const preprocessing = async ({ template }: { template: Template; }) => {
|
|
|
19
20
|
return { pdfDoc, embeddedPages, embedPdfBoxes };
|
|
20
21
|
};
|
|
21
22
|
|
|
22
|
-
const postProcessing = ({ pdfDoc }: { pdfDoc: PDFDocument }) => {
|
|
23
|
+
const postProcessing = ({ pdfDoc }: { pdfDoc: pdfLib.PDFDocument }) => {
|
|
23
24
|
pdfDoc.setProducer(TOOL_NAME);
|
|
24
25
|
pdfDoc.setCreator(TOOL_NAME);
|
|
25
26
|
};
|
|
26
27
|
|
|
27
28
|
const generate = async (props: GenerateProps) => {
|
|
28
29
|
checkGenerateProps(props);
|
|
29
|
-
const { inputs, template, options = {} } = props;
|
|
30
|
+
const { inputs, template, options = {}, plugins = {} } = props;
|
|
30
31
|
|
|
31
32
|
const { pdfDoc, embeddedPages, embedPdfBoxes } = await preprocessing({ template });
|
|
32
33
|
|
|
33
|
-
|
|
34
|
-
|
|
34
|
+
const customRenderer = Object.entries(plugins).reduce(
|
|
35
|
+
(acc, [key, { pdf }]) => Object.assign(acc, { [key]: pdf }),
|
|
36
|
+
{} as PDFRenderer
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
const rendererRegistry: PDFRenderer =
|
|
40
|
+
Object.keys(customRenderer).length > 0 ? customRenderer : builtInRenderer;
|
|
41
|
+
|
|
35
42
|
const _cache = new Map();
|
|
36
43
|
|
|
37
44
|
for (let i = 0; i < inputs.length; i += 1) {
|
|
@@ -55,11 +62,12 @@ const generate = async (props: GenerateProps) => {
|
|
|
55
62
|
continue;
|
|
56
63
|
}
|
|
57
64
|
|
|
58
|
-
const
|
|
59
|
-
if (!
|
|
60
|
-
throw new Error(`Renderer for type ${schema.type} not found
|
|
65
|
+
const render = rendererRegistry[schema.type];
|
|
66
|
+
if (!render) {
|
|
67
|
+
throw new Error(`[@pdfme/generator] Renderer for type ${schema.type} not found.
|
|
68
|
+
Check this document: https://pdfme.com/docs/custom-schemas`);
|
|
61
69
|
}
|
|
62
|
-
await
|
|
70
|
+
await render({ value, schema, pdfLib, pdfDoc, page, options, _cache });
|
|
63
71
|
}
|
|
64
72
|
}
|
|
65
73
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,73 +1,3 @@
|
|
|
1
1
|
import generate from './generate.js';
|
|
2
|
-
import {
|
|
3
|
-
BLANK_PDF,
|
|
4
|
-
DEFAULT_FONT_VALUE,
|
|
5
|
-
isTextSchema,
|
|
6
|
-
isImageSchema,
|
|
7
|
-
isBarcodeSchema,
|
|
8
|
-
checkTemplate,
|
|
9
|
-
checkUIProps,
|
|
10
|
-
checkPreviewProps,
|
|
11
|
-
checkDesignerProps,
|
|
12
|
-
checkGenerateProps,
|
|
13
|
-
validateBarcodeInput,
|
|
14
|
-
} from '@pdfme/common';
|
|
15
|
-
import type {
|
|
16
|
-
Lang,
|
|
17
|
-
Size,
|
|
18
|
-
Alignment,
|
|
19
|
-
SchemaType,
|
|
20
|
-
BarCodeType,
|
|
21
|
-
TextSchema,
|
|
22
|
-
ImageSchema,
|
|
23
|
-
BarcodeSchema,
|
|
24
|
-
Schema,
|
|
25
|
-
SchemaForUI,
|
|
26
|
-
Font,
|
|
27
|
-
BasePdf,
|
|
28
|
-
Template,
|
|
29
|
-
CommonProps,
|
|
30
|
-
GeneratorOptions,
|
|
31
|
-
GenerateProps,
|
|
32
|
-
UIOptions,
|
|
33
|
-
UIProps,
|
|
34
|
-
PreviewProps,
|
|
35
|
-
DesignerProps,
|
|
36
|
-
} from '@pdfme/common';
|
|
37
2
|
|
|
38
|
-
export {
|
|
39
|
-
generate,
|
|
40
|
-
BLANK_PDF,
|
|
41
|
-
DEFAULT_FONT_VALUE,
|
|
42
|
-
isTextSchema,
|
|
43
|
-
isImageSchema,
|
|
44
|
-
isBarcodeSchema,
|
|
45
|
-
checkTemplate,
|
|
46
|
-
checkUIProps,
|
|
47
|
-
checkPreviewProps,
|
|
48
|
-
checkDesignerProps,
|
|
49
|
-
checkGenerateProps,
|
|
50
|
-
validateBarcodeInput,
|
|
51
|
-
};
|
|
52
|
-
export type {
|
|
53
|
-
Lang,
|
|
54
|
-
Size,
|
|
55
|
-
Alignment,
|
|
56
|
-
SchemaType,
|
|
57
|
-
BarCodeType,
|
|
58
|
-
TextSchema,
|
|
59
|
-
ImageSchema,
|
|
60
|
-
BarcodeSchema,
|
|
61
|
-
Schema,
|
|
62
|
-
SchemaForUI,
|
|
63
|
-
Font,
|
|
64
|
-
BasePdf,
|
|
65
|
-
Template,
|
|
66
|
-
CommonProps,
|
|
67
|
-
GeneratorOptions,
|
|
68
|
-
GenerateProps,
|
|
69
|
-
UIOptions,
|
|
70
|
-
UIProps,
|
|
71
|
-
PreviewProps,
|
|
72
|
-
DesignerProps,
|
|
73
|
-
};
|
|
3
|
+
export { generate };
|
package/src/pdfUtils.ts
CHANGED
|
@@ -1,76 +1,48 @@
|
|
|
1
|
-
import { PDFPage,
|
|
2
|
-
import { getB64BasePdf,
|
|
3
|
-
import type { EmbedPdfBox } from
|
|
4
|
-
|
|
5
|
-
const embedAndGetFontObjCache = new WeakMap();
|
|
6
|
-
export const embedAndGetFontObj = async (arg: { pdfDoc: PDFDocument; font: Font }) => {
|
|
7
|
-
const { pdfDoc, font } = arg;
|
|
8
|
-
if (embedAndGetFontObjCache.has(pdfDoc)) {
|
|
9
|
-
return embedAndGetFontObjCache.get(pdfDoc);
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
const fontValues = await Promise.all(
|
|
13
|
-
Object.values(font).map(async (v) => {
|
|
14
|
-
let fontData = v.data;
|
|
15
|
-
if (typeof fontData === 'string' && fontData.startsWith('http')) {
|
|
16
|
-
fontData = await fetch(fontData).then((res) => res.arrayBuffer());
|
|
17
|
-
}
|
|
18
|
-
return pdfDoc.embedFont(fontData, {
|
|
19
|
-
subset: typeof v.subset === 'undefined' ? true : v.subset,
|
|
20
|
-
});
|
|
21
|
-
})
|
|
22
|
-
);
|
|
23
|
-
|
|
24
|
-
const fontObj = Object.keys(font).reduce(
|
|
25
|
-
(acc, cur, i) => Object.assign(acc, { [cur]: fontValues[i] }),
|
|
26
|
-
{} as { [key: string]: PDFFont }
|
|
27
|
-
)
|
|
28
|
-
|
|
29
|
-
embedAndGetFontObjCache.set(pdfDoc, fontObj);
|
|
30
|
-
return fontObj;
|
|
31
|
-
};
|
|
1
|
+
import { PDFPage, PDFDocument, PDFEmbeddedPage, TransformationMatrix } from '@pdfme/pdf-lib';
|
|
2
|
+
import { getB64BasePdf, BasePdf } from '@pdfme/common';
|
|
3
|
+
import type { EmbedPdfBox } from './types';
|
|
32
4
|
|
|
33
5
|
export const getEmbeddedPagesAndEmbedPdfBoxes = async (arg: {
|
|
34
|
-
|
|
35
|
-
|
|
6
|
+
pdfDoc: PDFDocument;
|
|
7
|
+
basePdf: BasePdf;
|
|
36
8
|
}) => {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
9
|
+
const { pdfDoc, basePdf } = arg;
|
|
10
|
+
let embeddedPages: PDFEmbeddedPage[] = [];
|
|
11
|
+
let embedPdfBoxes: EmbedPdfBox[] = [];
|
|
12
|
+
const willLoadPdf = typeof basePdf === 'string' ? await getB64BasePdf(basePdf) : basePdf;
|
|
13
|
+
const embedPdf = await PDFDocument.load(willLoadPdf);
|
|
14
|
+
const embedPdfPages = embedPdf.getPages();
|
|
43
15
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
16
|
+
embedPdfBoxes = embedPdfPages.map((p) => ({
|
|
17
|
+
mediaBox: p.getMediaBox(),
|
|
18
|
+
bleedBox: p.getBleedBox(),
|
|
19
|
+
trimBox: p.getTrimBox(),
|
|
20
|
+
}));
|
|
49
21
|
|
|
50
|
-
|
|
51
|
-
|
|
22
|
+
const boundingBoxes = embedPdfPages.map((p) => {
|
|
23
|
+
const { x, y, width, height } = p.getMediaBox();
|
|
52
24
|
|
|
53
|
-
|
|
54
|
-
|
|
25
|
+
return { left: x, bottom: y, right: width, top: height + y };
|
|
26
|
+
});
|
|
55
27
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
28
|
+
const transformationMatrices = embedPdfPages.map(
|
|
29
|
+
() => [1, 0, 0, 1, 0, 0] as TransformationMatrix
|
|
30
|
+
);
|
|
59
31
|
|
|
60
|
-
|
|
32
|
+
embeddedPages = await pdfDoc.embedPages(embedPdfPages, boundingBoxes, transformationMatrices);
|
|
61
33
|
|
|
62
|
-
|
|
34
|
+
return { embeddedPages, embedPdfBoxes };
|
|
63
35
|
};
|
|
64
36
|
|
|
65
37
|
export const drawEmbeddedPage = (arg: {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
38
|
+
page: PDFPage;
|
|
39
|
+
embeddedPage: PDFEmbeddedPage;
|
|
40
|
+
embedPdfBox: EmbedPdfBox;
|
|
69
41
|
}) => {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
42
|
+
const { page, embeddedPage, embedPdfBox } = arg;
|
|
43
|
+
page.drawPage(embeddedPage);
|
|
44
|
+
const { mediaBox: mb, bleedBox: bb, trimBox: tb } = embedPdfBox;
|
|
45
|
+
page.setMediaBox(mb.x, mb.y, mb.width, mb.height);
|
|
46
|
+
page.setBleedBox(bb.x, bb.y, bb.width, bb.height);
|
|
47
|
+
page.setTrimBox(tb.x, tb.y, tb.width, tb.height);
|
|
76
48
|
};
|
package/src/types.ts
CHANGED
|
@@ -1,23 +1,13 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type { GeneratorOptions, Schema, } from '@pdfme/common';
|
|
3
|
-
import type { PDFPage, PDFDocument, } from '@pdfme/pdf-lib';
|
|
1
|
+
import type { Schema, PDFRenderProps } from '@pdfme/common';
|
|
4
2
|
|
|
5
|
-
export type
|
|
6
|
-
mediaBox: { x: number; y: number; width: number; height: number };
|
|
7
|
-
bleedBox: { x: number; y: number; width: number; height: number };
|
|
8
|
-
trimBox: { x: number; y: number; width: number; height: number };
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
export interface RenderProps {
|
|
12
|
-
value: string;
|
|
13
|
-
schema: Schema;
|
|
14
|
-
pdfDoc: PDFDocument;
|
|
15
|
-
page: PDFPage;
|
|
16
|
-
options: GeneratorOptions;
|
|
3
|
+
export type PDFRender = (arg: PDFRenderProps<Schema>) => Promise<void>;
|
|
17
4
|
|
|
18
|
-
|
|
5
|
+
export interface PDFRenderer {
|
|
6
|
+
[key: string]: PDFRender | undefined;
|
|
19
7
|
}
|
|
20
8
|
|
|
21
|
-
export
|
|
22
|
-
|
|
23
|
-
}
|
|
9
|
+
export type EmbedPdfBox = {
|
|
10
|
+
mediaBox: { x: number; y: number; width: number; height: number };
|
|
11
|
+
bleedBox: { x: number; y: number; width: number; height: number };
|
|
12
|
+
trimBox: { x: number; y: number; width: number; height: number };
|
|
13
|
+
};
|
package/tsconfig.cjs.json
CHANGED
package/tsconfig.esm.json
CHANGED
package/tsconfig.json
CHANGED
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getCacheKey = exports.convertSchemaDimensionsToPt = exports.renderBackgroundColor = exports.calcY = exports.calcX = exports.hex2RgbColor = void 0;
|
|
4
|
-
const pdf_lib_1 = require("@pdfme/pdf-lib");
|
|
5
|
-
const common_1 = require("@pdfme/common");
|
|
6
|
-
const hex2rgb = (hex) => {
|
|
7
|
-
if (hex.slice(0, 1) === '#')
|
|
8
|
-
hex = hex.slice(1);
|
|
9
|
-
if (hex.length === 3)
|
|
10
|
-
hex =
|
|
11
|
-
hex.slice(0, 1) +
|
|
12
|
-
hex.slice(0, 1) +
|
|
13
|
-
hex.slice(1, 2) +
|
|
14
|
-
hex.slice(1, 2) +
|
|
15
|
-
hex.slice(2, 3) +
|
|
16
|
-
hex.slice(2, 3);
|
|
17
|
-
return [hex.slice(0, 2), hex.slice(2, 4), hex.slice(4, 6)].map((str) => parseInt(str, 16));
|
|
18
|
-
};
|
|
19
|
-
const hex2RgbColor = (hexString) => {
|
|
20
|
-
if (hexString) {
|
|
21
|
-
const [r, g, b] = hex2rgb(hexString);
|
|
22
|
-
return (0, pdf_lib_1.rgb)(r / 255, g / 255, b / 255);
|
|
23
|
-
}
|
|
24
|
-
// eslint-disable-next-line no-undefined
|
|
25
|
-
return undefined;
|
|
26
|
-
};
|
|
27
|
-
exports.hex2RgbColor = hex2RgbColor;
|
|
28
|
-
const calcX = (x, alignment, boxWidth, textWidth) => {
|
|
29
|
-
let addition = 0;
|
|
30
|
-
if (alignment === 'center') {
|
|
31
|
-
addition = (boxWidth - textWidth) / 2;
|
|
32
|
-
}
|
|
33
|
-
else if (alignment === 'right') {
|
|
34
|
-
addition = boxWidth - textWidth;
|
|
35
|
-
}
|
|
36
|
-
return (0, common_1.mm2pt)(x) + addition;
|
|
37
|
-
};
|
|
38
|
-
exports.calcX = calcX;
|
|
39
|
-
const calcY = (y, pageHeight, itemHeight) => pageHeight - (0, common_1.mm2pt)(y) - itemHeight;
|
|
40
|
-
exports.calcY = calcY;
|
|
41
|
-
const renderBackgroundColor = (arg) => {
|
|
42
|
-
const { schema, page, pageHeight } = arg;
|
|
43
|
-
if (!schema.backgroundColor)
|
|
44
|
-
return;
|
|
45
|
-
const { width, height } = (0, exports.convertSchemaDimensionsToPt)(schema);
|
|
46
|
-
const color = (0, exports.hex2RgbColor)(schema.backgroundColor);
|
|
47
|
-
page.drawRectangle({
|
|
48
|
-
x: (0, exports.calcX)(schema.position.x, 'left', width, width),
|
|
49
|
-
y: (0, exports.calcY)(schema.position.y, pageHeight, height),
|
|
50
|
-
width,
|
|
51
|
-
height,
|
|
52
|
-
color,
|
|
53
|
-
});
|
|
54
|
-
};
|
|
55
|
-
exports.renderBackgroundColor = renderBackgroundColor;
|
|
56
|
-
const convertSchemaDimensionsToPt = (schema) => {
|
|
57
|
-
const width = (0, common_1.mm2pt)(schema.width);
|
|
58
|
-
const height = (0, common_1.mm2pt)(schema.height);
|
|
59
|
-
const rotate = (0, pdf_lib_1.degrees)(schema.rotate ? schema.rotate : 0);
|
|
60
|
-
return { width, height, rotate };
|
|
61
|
-
};
|
|
62
|
-
exports.convertSchemaDimensionsToPt = convertSchemaDimensionsToPt;
|
|
63
|
-
const getCacheKey = (schema, input) => `${schema.type}${input}`;
|
|
64
|
-
exports.getCacheKey = getCacheKey;
|
|
65
|
-
//# sourceMappingURL=renderUtils.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"renderUtils.js","sourceRoot":"","sources":["../../../src/renderUtils.ts"],"names":[],"mappings":";;;AAAA,4CAAwD;AACxD,0CAAqE;AAErE,MAAM,OAAO,GAAG,CAAC,GAAW,EAAE,EAAE;IAC9B,IAAI,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,KAAK,GAAG;QAAE,GAAG,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IAChD,IAAI,GAAG,CAAC,MAAM,KAAK,CAAC;QAClB,GAAG;YACD,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC;gBACf,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC;gBACf,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC;gBACf,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC;gBACf,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC;gBACf,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IAEpB,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,QAAQ,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,CAAC;AAC7F,CAAC,CAAC;AAEK,MAAM,YAAY,GAAG,CAAC,SAA6B,EAAE,EAAE;IAC5D,IAAI,SAAS,EAAE;QACb,MAAM,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC;QAErC,OAAO,IAA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,KAAK,CAAC;QACjD,CAAC,EAAE,IAAA,aAAK,EAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,EAAE,UAAU,EAAE,MAAM,CAAC;QAC/C,KAAK;QACL,MAAM;QACN,KAAK;KACN,CAAC,CAAC;AACL,CAAC,CAAC;AAhBW,QAAA,qBAAqB,yBAgBhC;AAEK,MAAM,2BAA2B,GAAG,CAAC,MAAc,EAAE,EAAE;IAC5D,MAAM,KAAK,GAAG,IAAA,cAAK,EAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAClC,MAAM,MAAM,GAAG,IAAA,cAAK,EAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IACpC,MAAM,MAAM,GAAG,IAAA,iBAAO,EAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAE1D,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC;AACnC,CAAC,CAAC;AANW,QAAA,2BAA2B,+BAMtC;AAEK,MAAM,WAAW,GAAG,CAAC,MAAc,EAAE,KAAa,EAAE,EAAE,CAAC,GAAG,MAAM,CAAC,IAAI,GAAG,KAAK,EAAE,CAAC;AAA1E,QAAA,WAAW,eAA+D"}
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.renderBarcode = void 0;
|
|
13
|
-
const common_1 = require("@pdfme/common");
|
|
14
|
-
const renderUtils_1 = require("../renderUtils");
|
|
15
|
-
const renderBarcode = (arg) => __awaiter(void 0, void 0, void 0, function* () {
|
|
16
|
-
const { value, schema, pdfDoc, page, _cache } = arg;
|
|
17
|
-
if (!(0, common_1.validateBarcodeInput)(schema.type, value))
|
|
18
|
-
return;
|
|
19
|
-
const { width, height, rotate } = (0, renderUtils_1.convertSchemaDimensionsToPt)(schema);
|
|
20
|
-
const opt = {
|
|
21
|
-
x: (0, renderUtils_1.calcX)(schema.position.x, 'left', width, width),
|
|
22
|
-
y: (0, renderUtils_1.calcY)(schema.position.y, page.getHeight(), height),
|
|
23
|
-
rotate,
|
|
24
|
-
width,
|
|
25
|
-
height,
|
|
26
|
-
};
|
|
27
|
-
const inputBarcodeCacheKey = (0, renderUtils_1.getCacheKey)(schema, value);
|
|
28
|
-
let image = _cache.get(inputBarcodeCacheKey);
|
|
29
|
-
if (!image) {
|
|
30
|
-
const imageBuf = yield (0, common_1.createBarCode)(Object.assign(schema, { type: schema.type, input: value }));
|
|
31
|
-
image = yield pdfDoc.embedPng(imageBuf);
|
|
32
|
-
_cache.set(inputBarcodeCacheKey, image);
|
|
33
|
-
}
|
|
34
|
-
page.drawImage(image, opt);
|
|
35
|
-
});
|
|
36
|
-
exports.renderBarcode = renderBarcode;
|
|
37
|
-
//# sourceMappingURL=barcodes.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"barcodes.js","sourceRoot":"","sources":["../../../../src/renders/barcodes.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,0CAIuB;AAEvB,gDAAuF;AAEhF,MAAM,aAAa,GAAG,CAAO,GAAgB,EAAE,EAAE;IACpD,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,GAAG,CAAC;IACpD,IAAI,CAAC,IAAA,6BAAoB,EAAC,MAAM,CAAC,IAAmB,EAAE,KAAK,CAAC;QAAE,OAAO;IAErE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,IAAA,yCAA2B,EAAC,MAAM,CAAC,CAAC;IACtE,MAAM,GAAG,GAAG;QACR,CAAC,EAAE,IAAA,mBAAK,EAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,CAAC;QACjD,CAAC,EAAE,IAAA,mBAAK,EAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,EAAE,IAAI,CAAC,SAAS,EAAE,EAAE,MAAM,CAAC;QACrD,MAAM;QACN,KAAK;QACL,MAAM;KACT,CAAC;IACF,MAAM,oBAAoB,GAAG,IAAA,yBAAW,EAAC,MAAM,EAAE,KAAK,CAAC,CAAC;IACxD,IAAI,KAAK,GAAG,MAAM,CAAC,GAAG,CAAC,oBAAoB,CAAC,CAAC;IAC7C,IAAI,CAAC,KAAK,EAAE;QACR,MAAM,QAAQ,GAAG,MAAM,IAAA,sBAAa,EAChC,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,EAAE,IAAI,EAAE,MAAM,CAAC,IAAmB,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAC5E,CAAC;QACF,KAAK,GAAG,MAAM,MAAM,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;QACxC,MAAM,CAAC,GAAG,CAAC,oBAAoB,EAAE,KAAK,CAAC,CAAA;KAC1C;IAED,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;AAC/B,CAAC,CAAA,CAAA;AAvBY,QAAA,aAAa,iBAuBzB"}
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.renderImage = void 0;
|
|
13
|
-
const renderUtils_1 = require("../renderUtils");
|
|
14
|
-
const renderImage = (arg) => __awaiter(void 0, void 0, void 0, function* () {
|
|
15
|
-
const { value, schema, pdfDoc, page, _cache } = arg;
|
|
16
|
-
const { width, height, rotate } = (0, renderUtils_1.convertSchemaDimensionsToPt)(schema);
|
|
17
|
-
const opt = {
|
|
18
|
-
x: (0, renderUtils_1.calcX)(schema.position.x, 'left', width, width),
|
|
19
|
-
y: (0, renderUtils_1.calcY)(schema.position.y, page.getHeight(), height),
|
|
20
|
-
rotate,
|
|
21
|
-
width,
|
|
22
|
-
height,
|
|
23
|
-
};
|
|
24
|
-
const inputImageCacheKey = (0, renderUtils_1.getCacheKey)(schema, value);
|
|
25
|
-
let image = _cache.get(inputImageCacheKey);
|
|
26
|
-
if (!image) {
|
|
27
|
-
const isPng = value.startsWith('data:image/png;');
|
|
28
|
-
image = yield (isPng ? pdfDoc.embedPng(value) : pdfDoc.embedJpg(value));
|
|
29
|
-
_cache.set(inputImageCacheKey, image);
|
|
30
|
-
}
|
|
31
|
-
page.drawImage(image, opt);
|
|
32
|
-
});
|
|
33
|
-
exports.renderImage = renderImage;
|
|
34
|
-
//# sourceMappingURL=image.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"image.js","sourceRoot":"","sources":["../../../../src/renders/image.ts"],"names":[],"m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