@bhanquier/template-pdf-forms 0.1.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/dist/index.cjs +172 -0
- package/dist/index.js +142 -0
- package/package.json +40 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
PdfmeFormRenderer: () => PdfmeFormRenderer,
|
|
24
|
+
StubPdfFormRenderer: () => StubPdfFormRenderer,
|
|
25
|
+
convertSchemasToObjectPages: () => convertSchemasToObjectPages,
|
|
26
|
+
fromKontraktOptions: () => fromKontraktOptions,
|
|
27
|
+
generatePdfBufferCompatible: () => generatePdfBufferCompatible,
|
|
28
|
+
generatePdfCompatible: () => generatePdfCompatible
|
|
29
|
+
});
|
|
30
|
+
module.exports = __toCommonJS(index_exports);
|
|
31
|
+
var import_node_module = require("module");
|
|
32
|
+
var import_node_url = require("url");
|
|
33
|
+
|
|
34
|
+
// src/kontrakt-compat.ts
|
|
35
|
+
function fromKontraktOptions(options) {
|
|
36
|
+
const template = {
|
|
37
|
+
id: options.template.id,
|
|
38
|
+
name: options.template.name,
|
|
39
|
+
basePdf: options.template.basePdf,
|
|
40
|
+
schemas: options.template.schemas
|
|
41
|
+
};
|
|
42
|
+
return {
|
|
43
|
+
template,
|
|
44
|
+
inputs: options.inputs
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
async function generatePdfCompatible(options, renderer) {
|
|
48
|
+
const activeRenderer = renderer ?? new PdfmeFormRenderer();
|
|
49
|
+
const result = await activeRenderer.render(fromKontraktOptions(options));
|
|
50
|
+
return {
|
|
51
|
+
pdf: result.buffer,
|
|
52
|
+
pageCount: result.pageCount,
|
|
53
|
+
generationTimeMs: result.renderTimeMs
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
async function generatePdfBufferCompatible(options, renderer) {
|
|
57
|
+
const result = await generatePdfCompatible(options, renderer);
|
|
58
|
+
return Buffer.from(result.pdf);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// src/index.ts
|
|
62
|
+
function convertSchemasToObjectPages(schemas) {
|
|
63
|
+
return schemas.map((page) => {
|
|
64
|
+
const out = {};
|
|
65
|
+
for (const field of page) {
|
|
66
|
+
const { name, ...rest } = field;
|
|
67
|
+
out[name] = normalizeField(rest);
|
|
68
|
+
}
|
|
69
|
+
return out;
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
function normalizeField(field) {
|
|
73
|
+
if (field.type !== "table") {
|
|
74
|
+
return field;
|
|
75
|
+
}
|
|
76
|
+
const tableField = field;
|
|
77
|
+
const tableStyles = tableField.tableStyles ?? {};
|
|
78
|
+
const numCols = tableField.head?.length || 1;
|
|
79
|
+
const evenWidth = Math.round(1e4 / numCols) / 100;
|
|
80
|
+
return {
|
|
81
|
+
...tableField,
|
|
82
|
+
content: "",
|
|
83
|
+
head: tableField.head ?? [],
|
|
84
|
+
showHead: true,
|
|
85
|
+
headWidthPercentages: Array(numCols).fill(evenWidth),
|
|
86
|
+
tableStyles: {
|
|
87
|
+
borderWidth: tableStyles.borderWidth ?? 0.3,
|
|
88
|
+
borderColor: tableStyles.borderColor ?? "#666666"
|
|
89
|
+
},
|
|
90
|
+
headStyles: {
|
|
91
|
+
fontName: "Helvetica",
|
|
92
|
+
fontSize: tableStyles.headerFontSize ?? 9,
|
|
93
|
+
fontColor: tableStyles.headerFontColor ?? "#ffffff",
|
|
94
|
+
backgroundColor: tableStyles.headerBackgroundColor ?? "#1e293b",
|
|
95
|
+
alignment: "left"
|
|
96
|
+
},
|
|
97
|
+
bodyStyles: {
|
|
98
|
+
fontName: "Helvetica",
|
|
99
|
+
fontSize: tableStyles.bodyFontSize ?? 8,
|
|
100
|
+
fontColor: tableStyles.bodyFontColor ?? "#333333",
|
|
101
|
+
alternateBackgroundColor: "#f8fafc",
|
|
102
|
+
alignment: "left"
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
var StubPdfFormRenderer = class {
|
|
107
|
+
name = "pdfme-stub";
|
|
108
|
+
async render(request) {
|
|
109
|
+
return {
|
|
110
|
+
buffer: new Uint8Array(),
|
|
111
|
+
pageCount: request.inputs.length,
|
|
112
|
+
renderTimeMs: 0
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
var PdfmeFormRenderer = class {
|
|
117
|
+
name = "pdfme";
|
|
118
|
+
async render(request) {
|
|
119
|
+
const start = Date.now();
|
|
120
|
+
const generatorModule = await importRuntime("@pdfme/generator");
|
|
121
|
+
const schemasModule = await importRuntime("@pdfme/schemas");
|
|
122
|
+
const plugins = {
|
|
123
|
+
text: schemasModule.text,
|
|
124
|
+
image: schemasModule.image,
|
|
125
|
+
...schemasModule.barcodes ?? {},
|
|
126
|
+
table: schemasModule.table,
|
|
127
|
+
line: schemasModule.line,
|
|
128
|
+
rectangle: schemasModule.rectangle,
|
|
129
|
+
ellipse: schemasModule.ellipse
|
|
130
|
+
};
|
|
131
|
+
const template = {
|
|
132
|
+
basePdf: request.template.basePdf,
|
|
133
|
+
schemas: convertSchemasToObjectPages(request.template.schemas)
|
|
134
|
+
};
|
|
135
|
+
const pdf = await generatorModule.generate({
|
|
136
|
+
template,
|
|
137
|
+
inputs: request.inputs,
|
|
138
|
+
plugins
|
|
139
|
+
});
|
|
140
|
+
return {
|
|
141
|
+
buffer: pdf,
|
|
142
|
+
pageCount: request.inputs.length,
|
|
143
|
+
renderTimeMs: Date.now() - start
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
async function importRuntime(moduleName) {
|
|
148
|
+
try {
|
|
149
|
+
const loaded = await import(moduleName);
|
|
150
|
+
return loaded;
|
|
151
|
+
} catch (error) {
|
|
152
|
+
try {
|
|
153
|
+
const consumerRequire = (0, import_node_module.createRequire)(`${process.cwd()}/package.json`);
|
|
154
|
+
const resolved = consumerRequire.resolve(moduleName);
|
|
155
|
+
const loadedFromConsumer = await import((0, import_node_url.pathToFileURL)(resolved).href);
|
|
156
|
+
return loadedFromConsumer;
|
|
157
|
+
} catch {
|
|
158
|
+
throw new Error(
|
|
159
|
+
`Runtime dependency missing for ${moduleName}. Install @pdfme/* packages before using PdfmeFormRenderer. ${error.message}`
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
165
|
+
0 && (module.exports = {
|
|
166
|
+
PdfmeFormRenderer,
|
|
167
|
+
StubPdfFormRenderer,
|
|
168
|
+
convertSchemasToObjectPages,
|
|
169
|
+
fromKontraktOptions,
|
|
170
|
+
generatePdfBufferCompatible,
|
|
171
|
+
generatePdfCompatible
|
|
172
|
+
});
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { createRequire } from "module";
|
|
3
|
+
import { pathToFileURL } from "url";
|
|
4
|
+
|
|
5
|
+
// src/kontrakt-compat.ts
|
|
6
|
+
function fromKontraktOptions(options) {
|
|
7
|
+
const template = {
|
|
8
|
+
id: options.template.id,
|
|
9
|
+
name: options.template.name,
|
|
10
|
+
basePdf: options.template.basePdf,
|
|
11
|
+
schemas: options.template.schemas
|
|
12
|
+
};
|
|
13
|
+
return {
|
|
14
|
+
template,
|
|
15
|
+
inputs: options.inputs
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
async function generatePdfCompatible(options, renderer) {
|
|
19
|
+
const activeRenderer = renderer ?? new PdfmeFormRenderer();
|
|
20
|
+
const result = await activeRenderer.render(fromKontraktOptions(options));
|
|
21
|
+
return {
|
|
22
|
+
pdf: result.buffer,
|
|
23
|
+
pageCount: result.pageCount,
|
|
24
|
+
generationTimeMs: result.renderTimeMs
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
async function generatePdfBufferCompatible(options, renderer) {
|
|
28
|
+
const result = await generatePdfCompatible(options, renderer);
|
|
29
|
+
return Buffer.from(result.pdf);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// src/index.ts
|
|
33
|
+
function convertSchemasToObjectPages(schemas) {
|
|
34
|
+
return schemas.map((page) => {
|
|
35
|
+
const out = {};
|
|
36
|
+
for (const field of page) {
|
|
37
|
+
const { name, ...rest } = field;
|
|
38
|
+
out[name] = normalizeField(rest);
|
|
39
|
+
}
|
|
40
|
+
return out;
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
function normalizeField(field) {
|
|
44
|
+
if (field.type !== "table") {
|
|
45
|
+
return field;
|
|
46
|
+
}
|
|
47
|
+
const tableField = field;
|
|
48
|
+
const tableStyles = tableField.tableStyles ?? {};
|
|
49
|
+
const numCols = tableField.head?.length || 1;
|
|
50
|
+
const evenWidth = Math.round(1e4 / numCols) / 100;
|
|
51
|
+
return {
|
|
52
|
+
...tableField,
|
|
53
|
+
content: "",
|
|
54
|
+
head: tableField.head ?? [],
|
|
55
|
+
showHead: true,
|
|
56
|
+
headWidthPercentages: Array(numCols).fill(evenWidth),
|
|
57
|
+
tableStyles: {
|
|
58
|
+
borderWidth: tableStyles.borderWidth ?? 0.3,
|
|
59
|
+
borderColor: tableStyles.borderColor ?? "#666666"
|
|
60
|
+
},
|
|
61
|
+
headStyles: {
|
|
62
|
+
fontName: "Helvetica",
|
|
63
|
+
fontSize: tableStyles.headerFontSize ?? 9,
|
|
64
|
+
fontColor: tableStyles.headerFontColor ?? "#ffffff",
|
|
65
|
+
backgroundColor: tableStyles.headerBackgroundColor ?? "#1e293b",
|
|
66
|
+
alignment: "left"
|
|
67
|
+
},
|
|
68
|
+
bodyStyles: {
|
|
69
|
+
fontName: "Helvetica",
|
|
70
|
+
fontSize: tableStyles.bodyFontSize ?? 8,
|
|
71
|
+
fontColor: tableStyles.bodyFontColor ?? "#333333",
|
|
72
|
+
alternateBackgroundColor: "#f8fafc",
|
|
73
|
+
alignment: "left"
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
var StubPdfFormRenderer = class {
|
|
78
|
+
name = "pdfme-stub";
|
|
79
|
+
async render(request) {
|
|
80
|
+
return {
|
|
81
|
+
buffer: new Uint8Array(),
|
|
82
|
+
pageCount: request.inputs.length,
|
|
83
|
+
renderTimeMs: 0
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
var PdfmeFormRenderer = class {
|
|
88
|
+
name = "pdfme";
|
|
89
|
+
async render(request) {
|
|
90
|
+
const start = Date.now();
|
|
91
|
+
const generatorModule = await importRuntime("@pdfme/generator");
|
|
92
|
+
const schemasModule = await importRuntime("@pdfme/schemas");
|
|
93
|
+
const plugins = {
|
|
94
|
+
text: schemasModule.text,
|
|
95
|
+
image: schemasModule.image,
|
|
96
|
+
...schemasModule.barcodes ?? {},
|
|
97
|
+
table: schemasModule.table,
|
|
98
|
+
line: schemasModule.line,
|
|
99
|
+
rectangle: schemasModule.rectangle,
|
|
100
|
+
ellipse: schemasModule.ellipse
|
|
101
|
+
};
|
|
102
|
+
const template = {
|
|
103
|
+
basePdf: request.template.basePdf,
|
|
104
|
+
schemas: convertSchemasToObjectPages(request.template.schemas)
|
|
105
|
+
};
|
|
106
|
+
const pdf = await generatorModule.generate({
|
|
107
|
+
template,
|
|
108
|
+
inputs: request.inputs,
|
|
109
|
+
plugins
|
|
110
|
+
});
|
|
111
|
+
return {
|
|
112
|
+
buffer: pdf,
|
|
113
|
+
pageCount: request.inputs.length,
|
|
114
|
+
renderTimeMs: Date.now() - start
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
async function importRuntime(moduleName) {
|
|
119
|
+
try {
|
|
120
|
+
const loaded = await import(moduleName);
|
|
121
|
+
return loaded;
|
|
122
|
+
} catch (error) {
|
|
123
|
+
try {
|
|
124
|
+
const consumerRequire = createRequire(`${process.cwd()}/package.json`);
|
|
125
|
+
const resolved = consumerRequire.resolve(moduleName);
|
|
126
|
+
const loadedFromConsumer = await import(pathToFileURL(resolved).href);
|
|
127
|
+
return loadedFromConsumer;
|
|
128
|
+
} catch {
|
|
129
|
+
throw new Error(
|
|
130
|
+
`Runtime dependency missing for ${moduleName}. Install @pdfme/* packages before using PdfmeFormRenderer. ${error.message}`
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
export {
|
|
136
|
+
PdfmeFormRenderer,
|
|
137
|
+
StubPdfFormRenderer,
|
|
138
|
+
convertSchemasToObjectPages,
|
|
139
|
+
fromKontraktOptions,
|
|
140
|
+
generatePdfBufferCompatible,
|
|
141
|
+
generatePdfCompatible
|
|
142
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@bhanquier/template-pdf-forms",
|
|
3
|
+
"version": "0.1.2",
|
|
4
|
+
"description": "Shared contracts and helpers for pdfme-based form rendering",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"sideEffects": false,
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"module": "./dist/index.js",
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"import": "./dist/index.js",
|
|
14
|
+
"require": "./dist/index.cjs"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist"
|
|
19
|
+
],
|
|
20
|
+
"publishConfig": {
|
|
21
|
+
"access": "public"
|
|
22
|
+
},
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "https://github.com/TrustAkt/trustakt-templating.git"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@types/node": "^20.10.0",
|
|
29
|
+
"tsup": "^8.5.1",
|
|
30
|
+
"typescript": "^5.3.0",
|
|
31
|
+
"vitest": "^4.0.15"
|
|
32
|
+
},
|
|
33
|
+
"license": "UNLICENSED",
|
|
34
|
+
"scripts": {
|
|
35
|
+
"build": "tsup src/index.ts --format esm,cjs --out-dir dist --clean && tsc --noEmit",
|
|
36
|
+
"dev": "tsup src/index.ts --format esm,cjs --out-dir dist --watch",
|
|
37
|
+
"test": "vitest run",
|
|
38
|
+
"typecheck": "tsc --noEmit"
|
|
39
|
+
}
|
|
40
|
+
}
|