@sme.up/doc-alchemist 1.4.0 → 1.5.0-20251209090900
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 +39 -48
- package/dist/converters/excel/commons.d.ts +2 -2
- package/dist/converters/excel/commons.js +19 -13
- package/dist/converters/excel/commons.js.map +1 -1
- package/dist/converters/excel/excel-converter.types.d.ts +2 -0
- package/dist/converters/excel/excel-converter.types.js +3 -1
- package/dist/converters/excel/excel-converter.types.js.map +1 -1
- package/dist/converters/excel/matrix-converter.d.ts +1 -1
- package/dist/converters/excel/matrix-converter.js +33 -11
- package/dist/converters/excel/matrix-converter.js.map +1 -1
- package/dist/converters/excel/tree-converter.d.ts +1 -1
- package/dist/converters/excel/tree-converter.js +3 -3
- package/dist/converters/excel/tree-converter.js.map +1 -1
- package/dist/converters/excel-converter.d.ts +1 -1
- package/dist/converters/excel-converter.js +2 -2
- package/dist/converters/excel-converter.js.map +1 -1
- package/dist/converters/pdf/form-filler.d.ts +24 -0
- package/dist/converters/pdf/form-filler.js +187 -0
- package/dist/converters/pdf/form-filler.js.map +1 -0
- package/dist/converters/pdf/pdfmake/adapter-processor.d.ts +12 -0
- package/dist/converters/pdf/pdfmake/adapter-processor.js +113 -0
- package/dist/converters/pdf/pdfmake/adapter-processor.js.map +1 -0
- package/dist/converters/pdf/pdfmake/adapter-registry.d.ts +22 -0
- package/dist/converters/pdf/pdfmake/adapter-registry.js +48 -0
- package/dist/converters/pdf/pdfmake/adapter-registry.js.map +1 -0
- package/dist/converters/pdf/pdfmake/image-adapter.d.ts +19 -0
- package/dist/converters/pdf/pdfmake/image-adapter.js +150 -0
- package/dist/converters/pdf/pdfmake/image-adapter.js.map +1 -0
- package/dist/converters/pdf/pdfmake/page-element-adapter.d.ts +8 -0
- package/dist/converters/pdf/pdfmake/page-element-adapter.js +83 -0
- package/dist/converters/pdf/pdfmake/page-element-adapter.js.map +1 -0
- package/dist/converters/pdf/pdfmake/pdfmake.types.d.ts +60 -0
- package/dist/converters/pdf/pdfmake/pdfmake.types.js +13 -0
- package/dist/converters/pdf/pdfmake/pdfmake.types.js.map +1 -0
- package/dist/converters/pdf/pdfmake/table-adapter.d.ts +31 -0
- package/dist/converters/pdf/pdfmake/table-adapter.js +126 -0
- package/dist/converters/pdf/pdfmake/table-adapter.js.map +1 -0
- package/dist/converters/pdf/pdfmake-renderer.d.ts +21 -0
- package/dist/converters/pdf/pdfmake-renderer.js +87 -0
- package/dist/converters/pdf/pdfmake-renderer.js.map +1 -0
- package/dist/converters/pdf-converter.d.ts +6 -0
- package/dist/converters/pdf-converter.js +24 -1
- package/dist/converters/pdf-converter.js.map +1 -1
- package/dist/index.d.ts +7 -1
- package/dist/index.js +12 -1
- package/dist/index.js.map +1 -1
- package/dist/utils/commons-utility.d.ts +3 -2
- package/dist/utils/commons-utility.js +23 -79
- package/dist/utils/commons-utility.js.map +1 -1
- package/dist/utils/dates-utility.d.ts +7 -0
- package/dist/utils/dates-utility.js +25 -0
- package/dist/utils/dates-utility.js.map +1 -1
- package/dist/utils/filters-utility.d.ts +6 -0
- package/dist/utils/filters-utility.js +189 -0
- package/dist/utils/filters-utility.js.map +1 -0
- package/package.json +5 -3
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.imageAdapter = exports.objectToUrlAdapter = exports.urlToImageAdapter = void 0;
|
|
4
|
+
const hasSmeupUrl = (element) => {
|
|
5
|
+
const image = element
|
|
6
|
+
.image;
|
|
7
|
+
const url = image?.smeup?.url;
|
|
8
|
+
return typeof url === "string" && url.length > 0;
|
|
9
|
+
};
|
|
10
|
+
function hasSmeupObject(element) {
|
|
11
|
+
const image = element
|
|
12
|
+
.image;
|
|
13
|
+
const obj = image?.smeup?.obj;
|
|
14
|
+
return typeof obj === "object" && obj !== null;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Downloads an image from a URL and converts it to a data URL format.
|
|
18
|
+
* Validates that the content-type is an image MIME type.
|
|
19
|
+
*
|
|
20
|
+
* @param url - The URL of the image to download
|
|
21
|
+
* @param fetchData - Callback function to fetch the image data
|
|
22
|
+
* @returns A data URL string in the format: data:image/jpeg;base64,<base64>
|
|
23
|
+
* @throws If the download fails, content-type is not an image, or network error occurs
|
|
24
|
+
*/
|
|
25
|
+
const downloadImageAsDataUrl = async (url, fetchData) => {
|
|
26
|
+
const response = await fetchData(url);
|
|
27
|
+
if (!response.ok) {
|
|
28
|
+
throw new Error(`Failed to download image from ${url}: ${response.status} ${response.statusText}`);
|
|
29
|
+
}
|
|
30
|
+
// Extract content-type from headers
|
|
31
|
+
const contentType = response.headers.get("content-type") || "image/jpeg";
|
|
32
|
+
// Validate that content-type is an image
|
|
33
|
+
if (!contentType.startsWith("image/")) {
|
|
34
|
+
throw new Error(`Invalid content-type for image: expected image/*, got ${contentType}`);
|
|
35
|
+
}
|
|
36
|
+
// Get image data and convert to base64
|
|
37
|
+
const arrayBuffer = await response.arrayBuffer();
|
|
38
|
+
const base64 = Buffer.from(arrayBuffer).toString("base64");
|
|
39
|
+
return `data:${contentType};base64,${base64}`;
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Adapts a Smeup image structure to the PDFMake image format.
|
|
43
|
+
*
|
|
44
|
+
* This adapter function checks if the provided element contains an image with a smeup URL.
|
|
45
|
+
* If so, it downloads the image using the fetchData callback from context, validates the
|
|
46
|
+
* content-type, and transforms it into a data URL format that PDFMake can use.
|
|
47
|
+
* The data URL is stored in context.pdfDocument.images to avoid redundancy when the same
|
|
48
|
+
* image is referenced multiple times.
|
|
49
|
+
* All properties of the original image except `smeup` are preserved.
|
|
50
|
+
*
|
|
51
|
+
* @returns An async function that takes an element and returns the adapted element for PDFMake,
|
|
52
|
+
* or the original element if it does not contain a valid smeup image.
|
|
53
|
+
* @throws If the context does not provide a fetchData function when the smeup image is used.
|
|
54
|
+
* @throws If the image download fails or the content-type is not a valid image MIME type.
|
|
55
|
+
*/
|
|
56
|
+
const urlToImageAdapter = () => {
|
|
57
|
+
return async (element, context) => {
|
|
58
|
+
if (!hasSmeupUrl(element)) {
|
|
59
|
+
return element;
|
|
60
|
+
}
|
|
61
|
+
if (!context.fetchData) {
|
|
62
|
+
throw new Error("fetchData function is required in context when the smeup image property is used");
|
|
63
|
+
}
|
|
64
|
+
const elementImage = element;
|
|
65
|
+
const url = elementImage.image.smeup.url;
|
|
66
|
+
// Ensure pdfDocument exists in context
|
|
67
|
+
if (!context.pdfDocument) {
|
|
68
|
+
throw new Error("pdfDocument is required in context when the smeup image property is used");
|
|
69
|
+
}
|
|
70
|
+
// Initialize images dictionary if not present
|
|
71
|
+
if (!context.pdfDocument.images) {
|
|
72
|
+
context.pdfDocument.images = {};
|
|
73
|
+
}
|
|
74
|
+
// Download and cache the image only if not already present
|
|
75
|
+
if (!context.pdfDocument.images[url]) {
|
|
76
|
+
const dataUrl = await downloadImageAsDataUrl(url, context.fetchData);
|
|
77
|
+
context.pdfDocument.images[url] = dataUrl;
|
|
78
|
+
}
|
|
79
|
+
// Extract all properties except 'smeup' from the image object
|
|
80
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
81
|
+
const { smeup, ...imageRest } = elementImage.image;
|
|
82
|
+
return {
|
|
83
|
+
...elementImage,
|
|
84
|
+
image: url,
|
|
85
|
+
...imageRest,
|
|
86
|
+
};
|
|
87
|
+
};
|
|
88
|
+
};
|
|
89
|
+
exports.urlToImageAdapter = urlToImageAdapter;
|
|
90
|
+
const objectToUrlAdapter = () => {
|
|
91
|
+
return async (element, context) => {
|
|
92
|
+
if (!hasSmeupObject(element)) {
|
|
93
|
+
return element;
|
|
94
|
+
}
|
|
95
|
+
if (!context.getSmeupDataStructure || !context.damSvcEndpoint) {
|
|
96
|
+
throw new Error("getSmeupDataStructure function and damSvcEndpoint prop are required in context when the smeup image obj property is used");
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
const elementImage = element;
|
|
100
|
+
const obj = elementImage.image.smeup?.obj;
|
|
101
|
+
if (!obj || !obj.t || !obj.p || !obj.k) {
|
|
102
|
+
throw new Error("Smeup object must have t, p, and k properties to build GET.PATH function");
|
|
103
|
+
}
|
|
104
|
+
const funToGetPATH = `F(TRE;JASER_12W;GET.PATH) 1(${obj.t};${obj.p};${obj.k})`;
|
|
105
|
+
const dataTree = (await context.getSmeupDataStructure(funToGetPATH));
|
|
106
|
+
if (dataTree.children?.length === 0) {
|
|
107
|
+
throw new Error(`No data found for function ${funToGetPATH}`);
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
const pathNode = dataTree.children[0].value.replace(/\\/g, "/");
|
|
111
|
+
const pathEncoded = encodeURIComponent(pathNode);
|
|
112
|
+
const url = context.damSvcEndpoint + "/" + pathEncoded;
|
|
113
|
+
// Extract all properties except 'smeup' from the image object
|
|
114
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
115
|
+
const { smeup, ...imageRest } = elementImage.image;
|
|
116
|
+
return {
|
|
117
|
+
...elementImage,
|
|
118
|
+
image: {
|
|
119
|
+
smeup: {
|
|
120
|
+
url,
|
|
121
|
+
},
|
|
122
|
+
...imageRest,
|
|
123
|
+
},
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
};
|
|
129
|
+
exports.objectToUrlAdapter = objectToUrlAdapter;
|
|
130
|
+
const imageAdapter = () => {
|
|
131
|
+
return async (element, context) => {
|
|
132
|
+
try {
|
|
133
|
+
const adaptedElement = await (0, exports.objectToUrlAdapter)()(element, context);
|
|
134
|
+
return await (0, exports.urlToImageAdapter)()(adaptedElement, context);
|
|
135
|
+
}
|
|
136
|
+
catch (error) {
|
|
137
|
+
console.error(error);
|
|
138
|
+
const errorMessage = error.message;
|
|
139
|
+
const cause = error.cause;
|
|
140
|
+
const causeMessage = cause ? `\nCause: ${cause.message || cause}` : "";
|
|
141
|
+
return {
|
|
142
|
+
text: `Error loading image: ${errorMessage}${causeMessage}`,
|
|
143
|
+
fontSize: 10,
|
|
144
|
+
color: "#8B0000",
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
};
|
|
149
|
+
exports.imageAdapter = imageAdapter;
|
|
150
|
+
//# sourceMappingURL=image-adapter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"image-adapter.js","sourceRoot":"","sources":["../../../../src/converters/pdf/pdfmake/image-adapter.ts"],"names":[],"mappings":";;;AAOA,MAAM,WAAW,GAAG,CAAC,OAAgB,EAAW,EAAE;IAChD,MAAM,KAAK,GAAI,OAAmC;SAC/C,KAA8B,CAAC;IAClC,MAAM,GAAG,GAAG,KAAK,EAAE,KAAK,EAAE,GAAG,CAAC;IAC9B,OAAO,OAAO,GAAG,KAAK,QAAQ,IAAI,GAAG,CAAC,MAAM,GAAG,CAAC,CAAC;AACnD,CAAC,CAAC;AAEF,SAAS,cAAc,CAAC,OAAgB;IACtC,MAAM,KAAK,GAAI,OAAmC;SAC/C,KAA8B,CAAC;IAClC,MAAM,GAAG,GAAG,KAAK,EAAE,KAAK,EAAE,GAAG,CAAC;IAC9B,OAAO,OAAO,GAAG,KAAK,QAAQ,IAAI,GAAG,KAAK,IAAI,CAAC;AACjD,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,sBAAsB,GAAG,KAAK,EAClC,GAAW,EACX,SAA6C,EAC5B,EAAE;IACnB,MAAM,QAAQ,GAAG,MAAM,SAAS,CAAC,GAAG,CAAC,CAAC;IAEtC,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;QACjB,MAAM,IAAI,KAAK,CACb,iCAAiC,GAAG,KAAK,QAAQ,CAAC,MAAM,IAAI,QAAQ,CAAC,UAAU,EAAE,CAClF,CAAC;IACJ,CAAC;IAED,oCAAoC;IACpC,MAAM,WAAW,GAAG,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,cAAc,CAA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AAG,KAAK,CAAC,CAAC,CAAC,YAAY,KAAK,CAAC,OAAO,IAAI,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;YACvE,OAAO;gBACL,IAAI,EAAE,wBAAwB,YAAY,GAAG,YAAY,EAAE;gBAC3D,QAAQ,EAAE,EAAE;gBACZ,KAAK,EAAE,SAAS;aACmB,CAAC;QACxC,CAAC;IACH,CAAC,CAAC;AACJ,CAAC,CAAC;AApBW,QAAA,YAAY,gBAoBvB","sourcesContent":["import { SmeupDataTree } from \"../../../types/data-structures/smeupDataTree\";\nimport {\n ContentImageExtension,\n ElementWithImageExtension,\n PdfMakeConverterContext,\n} from \"./pdfmake.types\";\n\nconst hasSmeupUrl = (element: unknown): boolean => {\n const image = (element as Record<string, unknown>)\n .image as ContentImageExtension;\n const url = image?.smeup?.url;\n return typeof url === \"string\" && url.length > 0;\n};\n\nfunction hasSmeupObject(element: unknown): boolean {\n const image = (element as Record<string, unknown>)\n .image as ContentImageExtension;\n const obj = image?.smeup?.obj;\n return typeof obj === \"object\" && obj !== null;\n}\n\n/**\n * Downloads an image from a URL and converts it to a data URL format.\n * Validates that the content-type is an image MIME type.\n *\n * @param url - The URL of the image to download\n * @param fetchData - Callback function to fetch the image data\n * @returns A data URL string in the format: data:image/jpeg;base64,<base64>\n * @throws If the download fails, content-type is not an image, or network error occurs\n */\nconst downloadImageAsDataUrl = async (\n url: string,\n fetchData: (url: string) => Promise<Response>,\n): Promise<string> => {\n const response = await fetchData(url);\n\n if (!response.ok) {\n throw new Error(\n `Failed to download image from ${url}: ${response.status} ${response.statusText}`,\n );\n }\n\n // Extract content-type from headers\n const contentType = response.headers.get(\"content-type\") || \"image/jpeg\";\n\n // Validate that content-type is an image\n if (!contentType.startsWith(\"image/\")) {\n throw new Error(\n `Invalid content-type for image: expected image/*, got ${contentType}`,\n );\n }\n\n // Get image data and convert to base64\n const arrayBuffer = await response.arrayBuffer();\n const base64 = Buffer.from(arrayBuffer).toString(\"base64\");\n\n return `data:${contentType};base64,${base64}`;\n};\n\n/**\n * Adapts a Smeup image structure to the PDFMake image format.\n *\n * This adapter function checks if the provided element contains an image with a smeup URL.\n * If so, it downloads the image using the fetchData callback from context, validates the\n * content-type, and transforms it into a data URL format that PDFMake can use.\n * The data URL is stored in context.pdfDocument.images to avoid redundancy when the same\n * image is referenced multiple times.\n * All properties of the original image except `smeup` are preserved.\n *\n * @returns An async function that takes an element and returns the adapted element for PDFMake,\n * or the original element if it does not contain a valid smeup image.\n * @throws If the context does not provide a fetchData function when the smeup image is used.\n * @throws If the image download fails or the content-type is not a valid image MIME type.\n */\nexport const urlToImageAdapter = () => {\n return async (\n element: unknown,\n context: PdfMakeConverterContext,\n ): Promise<ContentImageExtension> => {\n if (!hasSmeupUrl(element)) {\n return element as ContentImageExtension;\n }\n\n if (!context.fetchData) {\n throw new Error(\n \"fetchData function is required in context when the smeup image property is used\",\n );\n }\n\n const elementImage = element as { image: { smeup: { url: string } } };\n const url = elementImage.image.smeup.url;\n\n // Ensure pdfDocument exists in context\n if (!context.pdfDocument) {\n throw new Error(\n \"pdfDocument is required in context when the smeup image property is used\",\n );\n }\n\n // Initialize images dictionary if not present\n if (!context.pdfDocument.images) {\n context.pdfDocument.images = {};\n }\n\n // Download and cache the image only if not already present\n if (!context.pdfDocument.images[url]) {\n const dataUrl = await downloadImageAsDataUrl(url, context.fetchData);\n context.pdfDocument.images[url] = dataUrl;\n }\n\n // Extract all properties except 'smeup' from the image object\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n const { smeup, ...imageRest } = elementImage.image;\n\n return {\n ...elementImage,\n image: url,\n ...imageRest,\n };\n };\n};\n\nexport const objectToUrlAdapter = () => {\n return async (\n element: unknown,\n context: PdfMakeConverterContext,\n ): Promise<ElementWithImageExtension> => {\n if (!hasSmeupObject(element)) {\n return element as ElementWithImageExtension;\n }\n if (!context.getSmeupDataStructure || !context.damSvcEndpoint) {\n throw new Error(\n \"getSmeupDataStructure function and damSvcEndpoint prop are required in context when the smeup image obj property is used\",\n );\n } else {\n const elementImage = element as ElementWithImageExtension;\n const obj = elementImage.image.smeup?.obj;\n if (!obj || !obj.t || !obj.p || !obj.k) {\n throw new Error(\n \"Smeup object must have t, p, and k properties to build GET.PATH function\",\n );\n }\n const funToGetPATH = `F(TRE;JASER_12W;GET.PATH) 1(${obj.t};${obj.p};${obj.k})`;\n const dataTree = (await context.getSmeupDataStructure(\n funToGetPATH,\n )) as SmeupDataTree;\n if (dataTree.children?.length === 0) {\n throw new Error(`No data found for function ${funToGetPATH}`);\n } else {\n const pathNode = dataTree.children[0].value.replace(/\\\\/g, \"/\");\n const pathEncoded = encodeURIComponent(pathNode);\n const url = context.damSvcEndpoint + \"/\" + pathEncoded;\n\n // Extract all properties except 'smeup' from the image object\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n const { smeup, ...imageRest } = elementImage.image;\n\n return {\n ...elementImage,\n image: {\n smeup: {\n url,\n },\n ...imageRest,\n },\n };\n }\n }\n };\n};\n\nexport const imageAdapter = () => {\n return async (\n element: unknown,\n context: PdfMakeConverterContext,\n ): Promise<ContentImageExtension> => {\n try {\n const adaptedElement = await objectToUrlAdapter()(element, context);\n return await urlToImageAdapter()(adaptedElement, context);\n } catch (error) {\n console.error(error);\n const errorMessage = (error as Error).message;\n const cause = (error as Error & { cause?: Error }).cause;\n const causeMessage = cause ? `\\nCause: ${cause.message || cause}` : \"\";\n return {\n text: `Error loading image: ${errorMessage}${causeMessage}`,\n fontSize: 10,\n color: \"#8B0000\",\n } as unknown as ContentImageExtension;\n }\n };\n};\n"]}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Adapter for header elements with page number placeholder support.
|
|
3
|
+
*/
|
|
4
|
+
export declare const headerAdapter: () => (element: unknown) => unknown;
|
|
5
|
+
/**
|
|
6
|
+
* Adapter for footer elements with page number placeholder support.
|
|
7
|
+
*/
|
|
8
|
+
export declare const footerAdapter: () => (element: unknown) => unknown;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.footerAdapter = exports.headerAdapter = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Type guard to check if an element has a specific property
|
|
6
|
+
*/
|
|
7
|
+
const hasProperty = (element, propertyName) => {
|
|
8
|
+
return (element !== null &&
|
|
9
|
+
typeof element === "object" &&
|
|
10
|
+
propertyName in element &&
|
|
11
|
+
element[propertyName] !== undefined);
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Recursively searches for [CurrentPage] and [PageCount] placeholders in an object
|
|
15
|
+
* and replaces them with the actual values.
|
|
16
|
+
*
|
|
17
|
+
* @param obj - The object to process
|
|
18
|
+
* @param currentPage - The current page number
|
|
19
|
+
* @param pageCount - The total page count
|
|
20
|
+
* @returns The processed object with replaced placeholders
|
|
21
|
+
*/
|
|
22
|
+
const replacePlaceholders = (obj, currentPage, pageCount) => {
|
|
23
|
+
if (typeof obj === "string") {
|
|
24
|
+
return obj
|
|
25
|
+
.replace(/\[CurrentPage\]/g, String(currentPage))
|
|
26
|
+
.replace(/\[PageCount\]/g, String(pageCount));
|
|
27
|
+
}
|
|
28
|
+
if (Array.isArray(obj)) {
|
|
29
|
+
return obj.map(item => replacePlaceholders(item, currentPage, pageCount));
|
|
30
|
+
}
|
|
31
|
+
if (obj !== null && typeof obj === "object") {
|
|
32
|
+
const result = {};
|
|
33
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
34
|
+
result[key] = replacePlaceholders(value, currentPage, pageCount);
|
|
35
|
+
}
|
|
36
|
+
return result;
|
|
37
|
+
}
|
|
38
|
+
return obj;
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* Adapts a header or footer to support dynamic page number placeholders.
|
|
42
|
+
*
|
|
43
|
+
* This adapter function checks if the provided element contains the specified property (header or footer).
|
|
44
|
+
* - If the property is already a function, it leaves it unchanged.
|
|
45
|
+
* - If the property is an object, it wraps it in a function that replaces
|
|
46
|
+
* [CurrentPage] with the current page number and [PageCount] with the total
|
|
47
|
+
* page count at rendering time.
|
|
48
|
+
*
|
|
49
|
+
* @param propertyName - The name of the property to adapt ('header' or 'footer')
|
|
50
|
+
* @returns A function that takes an element and returns the adapted element for PDFMake,
|
|
51
|
+
* or the original element if it does not contain the specified property.
|
|
52
|
+
*/
|
|
53
|
+
const createPageElementAdapter = (propertyName) => {
|
|
54
|
+
return (element) => {
|
|
55
|
+
if (!hasProperty(element, propertyName)) {
|
|
56
|
+
return element;
|
|
57
|
+
}
|
|
58
|
+
const propertyValue = element[propertyName];
|
|
59
|
+
// If property is already a function, leave it as is
|
|
60
|
+
if (typeof propertyValue === "function") {
|
|
61
|
+
return element;
|
|
62
|
+
}
|
|
63
|
+
// If property is an object, wrap it in a function that replaces placeholders
|
|
64
|
+
const dynamicContent = (currentPage, pageCount) => {
|
|
65
|
+
return replacePlaceholders(propertyValue, currentPage, pageCount);
|
|
66
|
+
};
|
|
67
|
+
return {
|
|
68
|
+
...element,
|
|
69
|
+
[propertyName]: dynamicContent,
|
|
70
|
+
};
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* Adapter for header elements with page number placeholder support.
|
|
75
|
+
*/
|
|
76
|
+
const headerAdapter = () => createPageElementAdapter("header");
|
|
77
|
+
exports.headerAdapter = headerAdapter;
|
|
78
|
+
/**
|
|
79
|
+
* Adapter for footer elements with page number placeholder support.
|
|
80
|
+
*/
|
|
81
|
+
const footerAdapter = () => createPageElementAdapter("footer");
|
|
82
|
+
exports.footerAdapter = footerAdapter;
|
|
83
|
+
//# sourceMappingURL=page-element-adapter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"page-element-adapter.js","sourceRoot":"","sources":["../../../../src/converters/pdf/pdfmake/page-element-adapter.ts"],"names":[],"mappings":";;;AAEA;;GAEG;AACH,MAAM,WAAW,GAAG,CAClB,OAAgB,EAChB,YAAe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Y,CAAC,EAAE,CAAC;YACxC,OAAO,OAAO,CAAC;QACjB,CAAC;QAED,MAAM,aAAa,GAAG,OAAO,CAAC,YAAY,CAAC,CAAC;QAE5C,oDAAoD;QACpD,IAAI,OAAO,aAAa,KAAK,UAAU,EAAE,CAAC;YACxC,OAAO,OAAO,CAAC;QACjB,CAAC;QAED,6EAA6E;QAC7E,MAAM,cAAc,GAAmB,CAAC,WAAW,EAAE,SAAS,EAAE,EAAE;YAChE,OAAO,mBAAmB,CACxB,aAAa,EACb,WAAW,EACX,SAAS,CACC,CAAC;QACf,CAAC,CAAC;QAEF,OAAO;YACL,GAAG,OAAO;YACV,CAAC,YAAY,CAAC,EAAE,cAAc;SAC/B,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC,CAAC;AAEF;;GAEG;AACI,MAAM,aAAa,GAAG,GAAG,EAAE,CAAC,wBAAwB,CAAC,QAAQ,CAAC,CAAC;AAAzD,QAAA,aAAa,iBAA4C;AAEtE;;GAEG;AACI,MAAM,aAAa,GAAG,GAAG,EAAE,CAAC,wBAAwB,CAAC,QAAQ,CAAC,CAAC;AAAzD,QAAA,aAAa,iBAA4C","sourcesContent":["import { Content, DynamicContent } from \"pdfmake/interfaces\";\n\n/**\n * Type guard to check if an element has a specific property\n */\nconst hasProperty = <T extends string>(\n element: unknown,\n propertyName: T,\n): element is Record<T, Content | DynamicContent> => {\n return (\n element !== null &&\n typeof element === \"object\" &&\n propertyName in element &&\n (element as Record<string, unknown>)[propertyName] !== undefined\n );\n};\n\n/**\n * Recursively searches for [CurrentPage] and [PageCount] placeholders in an object\n * and replaces them with the actual values.\n *\n * @param obj - The object to process\n * @param currentPage - The current page number\n * @param pageCount - The total page count\n * @returns The processed object with replaced placeholders\n */\nconst replacePlaceholders = (\n obj: unknown,\n currentPage: number,\n pageCount: number,\n): unknown => {\n if (typeof obj === \"string\") {\n return obj\n .replace(/\\[CurrentPage\\]/g, String(currentPage))\n .replace(/\\[PageCount\\]/g, String(pageCount));\n }\n\n if (Array.isArray(obj)) {\n return obj.map(item => replacePlaceholders(item, currentPage, pageCount));\n }\n\n if (obj !== null && typeof obj === \"object\") {\n const result: Record<string, unknown> = {};\n for (const [key, value] of Object.entries(obj)) {\n result[key] = replacePlaceholders(value, currentPage, pageCount);\n }\n return result;\n }\n\n return obj;\n};\n\n/**\n * Adapts a header or footer to support dynamic page number placeholders.\n *\n * This adapter function checks if the provided element contains the specified property (header or footer).\n * - If the property is already a function, it leaves it unchanged.\n * - If the property is an object, it wraps it in a function that replaces\n * [CurrentPage] with the current page number and [PageCount] with the total\n * page count at rendering time.\n *\n * @param propertyName - The name of the property to adapt ('header' or 'footer')\n * @returns A function that takes an element and returns the adapted element for PDFMake,\n * or the original element if it does not contain the specified property.\n */\nconst createPageElementAdapter = (propertyName: \"header\" | \"footer\") => {\n return (element: unknown): unknown => {\n if (!hasProperty(element, propertyName)) {\n return element;\n }\n\n const propertyValue = element[propertyName];\n\n // If property is already a function, leave it as is\n if (typeof propertyValue === \"function\") {\n return element;\n }\n\n // If property is an object, wrap it in a function that replaces placeholders\n const dynamicContent: DynamicContent = (currentPage, pageCount) => {\n return replacePlaceholders(\n propertyValue,\n currentPage,\n pageCount,\n ) as Content;\n };\n\n return {\n ...element,\n [propertyName]: dynamicContent,\n };\n };\n};\n\n/**\n * Adapter for header elements with page number placeholder support.\n */\nexport const headerAdapter = () => createPageElementAdapter(\"header\");\n\n/**\n * Adapter for footer elements with page number placeholder support.\n */\nexport const footerAdapter = () => createPageElementAdapter(\"footer\");\n"]}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { ContentImage, Table, TDocumentDefinitions } from "pdfmake/interfaces";
|
|
2
|
+
import { SmeupDataObj, WebupManagerData } from "../../../types";
|
|
3
|
+
import { SmeupDataStructure } from "../../../types/data-structures/smeupDataStructure";
|
|
4
|
+
import { SmeupDataTable } from "../../../types/data-structures/smeupDataTable";
|
|
5
|
+
/**
|
|
6
|
+
* Context passed to adapter functions during document processing.
|
|
7
|
+
*/
|
|
8
|
+
export interface PdfMakeConverterContext<T extends SmeupDataStructure = SmeupDataStructure> {
|
|
9
|
+
webupManagerData: WebupManagerData;
|
|
10
|
+
pdfDocument?: TDocumentDefinitions;
|
|
11
|
+
damSvcEndpoint?: string;
|
|
12
|
+
getSmeupDataStructure?: (fun: string) => Promise<T>;
|
|
13
|
+
fetchData?: (url: string) => Promise<Response>;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Function that transforms an element from custom format to pdfmake standard format.
|
|
17
|
+
* Adapters are pure functions that receive an element and context, and return the transformed element.
|
|
18
|
+
* Can be synchronous or asynchronous.
|
|
19
|
+
*/
|
|
20
|
+
export type PdfmakeAdapter = (element: unknown, context: PdfMakeConverterContext) => unknown | Promise<unknown>;
|
|
21
|
+
/**
|
|
22
|
+
* Registry interface for managing pdfmake adapters at runtime.
|
|
23
|
+
*/
|
|
24
|
+
export interface AdapterRegistry {
|
|
25
|
+
/** Register an adapter for a specific element type */
|
|
26
|
+
register: (elementType: string, adapter: PdfmakeAdapter) => void;
|
|
27
|
+
/** Unregister an adapter for a specific element type */
|
|
28
|
+
unregister: (elementType: string) => void;
|
|
29
|
+
/** Process a document by applying registered adapters */
|
|
30
|
+
process: (document: unknown, context: Partial<PdfMakeConverterContext>) => Promise<unknown>;
|
|
31
|
+
}
|
|
32
|
+
export declare class AdapterError extends Error {
|
|
33
|
+
element: unknown;
|
|
34
|
+
constructor(message: string, element: unknown);
|
|
35
|
+
}
|
|
36
|
+
interface SmeupExtensionImageMetadata {
|
|
37
|
+
url?: string;
|
|
38
|
+
obj?: SmeupDataObj;
|
|
39
|
+
}
|
|
40
|
+
interface SmeupExtensionTableMetadata {
|
|
41
|
+
fun?: string;
|
|
42
|
+
data?: SmeupDataTable;
|
|
43
|
+
}
|
|
44
|
+
export interface ContentImageExtension extends ContentImage {
|
|
45
|
+
smeup?: SmeupExtensionImageMetadata;
|
|
46
|
+
}
|
|
47
|
+
export interface TableExtension extends Table {
|
|
48
|
+
smeup?: SmeupExtensionTableMetadata;
|
|
49
|
+
}
|
|
50
|
+
export interface ElementWithImageExtension {
|
|
51
|
+
image: ContentImage & {
|
|
52
|
+
smeup?: SmeupExtensionImageMetadata;
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
export interface ElementWithTableExtension {
|
|
56
|
+
table: Table & {
|
|
57
|
+
smeup?: SmeupExtensionTableMetadata;
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
export {};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AdapterError = void 0;
|
|
4
|
+
class AdapterError extends Error {
|
|
5
|
+
element;
|
|
6
|
+
constructor(message, element) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.element = element;
|
|
9
|
+
Object.setPrototypeOf(this, AdapterError.prototype);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
exports.AdapterError = AdapterError;
|
|
13
|
+
//# sourceMappingURL=pdfmake.types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pdfmake.types.js","sourceRoot":"","sources":["../../../../src/converters/pdf/pdfmake/pdfmake.types.ts"],"names":[],"mappings":";;;AA2CA,MAAa,YAAa,SAAQ,KAAK;IACrC,OAAO,CAAU;IACjB,YAAY,OAAe,EAAE,OAAgB;QAC3C,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,YAAY,CAAC,SAAS,CAAC,CAAC;IACtD,CAAC;CACF;AAPD,oCAOC","sourcesContent":["import { ContentImage, Table, TDocumentDefinitions } from \"pdfmake/interfaces\";\nimport { SmeupDataObj, WebupManagerData } from \"../../../types\";\nimport { SmeupDataStructure } from \"../../../types/data-structures/smeupDataStructure\";\nimport { SmeupDataTable } from \"../../../types/data-structures/smeupDataTable\";\n\n/**\n * Context passed to adapter functions during document processing.\n */\nexport interface PdfMakeConverterContext<\n T extends SmeupDataStructure = SmeupDataStructure,\n> {\n webupManagerData: WebupManagerData;\n pdfDocument?: TDocumentDefinitions;\n damSvcEndpoint?: string;\n getSmeupDataStructure?: (fun: string) => Promise<T>;\n fetchData?: (url: string) => Promise<Response>;\n}\n\n/**\n * Function that transforms an element from custom format to pdfmake standard format.\n * Adapters are pure functions that receive an element and context, and return the transformed element.\n * Can be synchronous or asynchronous.\n */\nexport type PdfmakeAdapter = (\n element: unknown,\n context: PdfMakeConverterContext,\n) => unknown | Promise<unknown>;\n\n/**\n * Registry interface for managing pdfmake adapters at runtime.\n */\nexport interface AdapterRegistry {\n /** Register an adapter for a specific element type */\n register: (elementType: string, adapter: PdfmakeAdapter) => void;\n /** Unregister an adapter for a specific element type */\n unregister: (elementType: string) => void;\n /** Process a document by applying registered adapters */\n process: (\n document: unknown,\n context: Partial<PdfMakeConverterContext>,\n ) => Promise<unknown>;\n}\n\nexport class AdapterError extends Error {\n element: unknown;\n constructor(message: string, element: unknown) {\n super(message);\n this.element = element;\n Object.setPrototypeOf(this, AdapterError.prototype);\n }\n}\n\ninterface SmeupExtensionImageMetadata {\n url?: string;\n obj?: SmeupDataObj;\n}\n\ninterface SmeupExtensionTableMetadata {\n fun?: string;\n data?: SmeupDataTable;\n}\n\nexport interface ContentImageExtension extends ContentImage {\n smeup?: SmeupExtensionImageMetadata;\n}\n\nexport interface TableExtension extends Table {\n smeup?: SmeupExtensionTableMetadata;\n}\n\nexport interface ElementWithImageExtension {\n image: ContentImage & { smeup?: SmeupExtensionImageMetadata };\n}\n\nexport interface ElementWithTableExtension {\n table: Table & { smeup?: SmeupExtensionTableMetadata };\n}\n"]}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { ElementWithTableExtension, PdfMakeConverterContext } from "./pdfmake.types";
|
|
2
|
+
/**
|
|
3
|
+
* Adapts a Smeup table structure to the PDFMake table format.
|
|
4
|
+
*
|
|
5
|
+
* This adapter function checks if the provided element contains a table with data.
|
|
6
|
+
* If so, it transforms the table's columns and rows into the PDFMake `body` format,
|
|
7
|
+
* where the first row is the header (column titles) and subsequent rows are the data.
|
|
8
|
+
* All properties of the original table except `data` are preserved.
|
|
9
|
+
*
|
|
10
|
+
* @returns A function that takes an element and returns the adapted element for PDFMake,
|
|
11
|
+
* or the original element if it does not contain a valid table with data.
|
|
12
|
+
*/
|
|
13
|
+
export declare const smeupTableToPdfMakeTableAdapter: () => (element: unknown, context: PdfMakeConverterContext) => ElementWithTableExtension;
|
|
14
|
+
/**
|
|
15
|
+
* Adapter function for converting a table element with a `fun` property to a Smeup table structure.
|
|
16
|
+
*
|
|
17
|
+
* This function returns an asynchronous converter that checks if the provided element contains
|
|
18
|
+
* a table with a `fun` property. If present, it uses the `getSmeupDataStructure` function from
|
|
19
|
+
* the context to fetch the table data corresponding to the `fun` string, and replaces the original
|
|
20
|
+
* table's `fun` property with the fetched data under the `data` property. All other properties
|
|
21
|
+
* of the table are preserved.
|
|
22
|
+
*
|
|
23
|
+
* @returns An asynchronous function that takes an element and a context, and returns the adapted element.
|
|
24
|
+
*
|
|
25
|
+
* @throws If the context does not provide a `getSmeupDataStructure` function when the `fun` property is used.
|
|
26
|
+
*
|
|
27
|
+
* @param element - The input element to be adapted.
|
|
28
|
+
* @param context - The conversion context, which must provide `getSmeupDataStructure` if the `fun` property is present.
|
|
29
|
+
*/
|
|
30
|
+
export declare const funToSmeupTableAdapter: () => (element: unknown, context: PdfMakeConverterContext) => Promise<ElementWithTableExtension>;
|
|
31
|
+
export declare const dataTableAdapter: () => (element: unknown, context: PdfMakeConverterContext) => Promise<ElementWithTableExtension>;
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.dataTableAdapter = exports.funToSmeupTableAdapter = exports.smeupTableToPdfMakeTableAdapter = void 0;
|
|
4
|
+
const types_1 = require("../../../types");
|
|
5
|
+
const smeupDataStructure_1 = require("../../../types/data-structures/smeupDataStructure");
|
|
6
|
+
const commons_utility_1 = require("../../../utils/commons-utility");
|
|
7
|
+
const hasTableWithDataTable = (element) => {
|
|
8
|
+
const table = element?.table;
|
|
9
|
+
const data = table?.smeup?.data;
|
|
10
|
+
return (data?.type === smeupDataStructure_1.SmeupDataStructureType.SmeupDataTable &&
|
|
11
|
+
data?.rows !== undefined &&
|
|
12
|
+
data?.columns !== undefined);
|
|
13
|
+
};
|
|
14
|
+
const hasTableWithFun = (element) => {
|
|
15
|
+
const table = element?.table;
|
|
16
|
+
const fun = table?.smeup?.fun;
|
|
17
|
+
return typeof fun === "string" && fun.length > 0;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Adapts a Smeup table structure to the PDFMake table format.
|
|
21
|
+
*
|
|
22
|
+
* This adapter function checks if the provided element contains a table with data.
|
|
23
|
+
* If so, it transforms the table's columns and rows into the PDFMake `body` format,
|
|
24
|
+
* where the first row is the header (column titles) and subsequent rows are the data.
|
|
25
|
+
* All properties of the original table except `data` are preserved.
|
|
26
|
+
*
|
|
27
|
+
* @returns A function that takes an element and returns the adapted element for PDFMake,
|
|
28
|
+
* or the original element if it does not contain a valid table with data.
|
|
29
|
+
*/
|
|
30
|
+
const smeupTableToPdfMakeTableAdapter = () => {
|
|
31
|
+
return (element, context) => {
|
|
32
|
+
if (!hasTableWithDataTable(element)) {
|
|
33
|
+
return element;
|
|
34
|
+
}
|
|
35
|
+
const { columns, rows } = element.table.smeup.data;
|
|
36
|
+
// Build header row from column titles
|
|
37
|
+
const headerRow = columns.map(col => col.title);
|
|
38
|
+
// Build data rows by extracting values in column order
|
|
39
|
+
const dataRows = rows.map(row => columns.map(col => {
|
|
40
|
+
const cell = row.cells?.[col.name];
|
|
41
|
+
const value = (0, commons_utility_1.calculateCellValue)(cell, types_1.SupportedExportFormats.PDF, context.webupManagerData);
|
|
42
|
+
return {
|
|
43
|
+
text: value != null ? String(value) : "",
|
|
44
|
+
alignment: col.obj?.t === "NR" ? "right" : "left",
|
|
45
|
+
};
|
|
46
|
+
}));
|
|
47
|
+
// Combine header and data rows
|
|
48
|
+
const body = [headerRow, ...dataRows];
|
|
49
|
+
// Extract all properties except 'data' from the smeup object
|
|
50
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
51
|
+
const { data, ...smeupRest } = element.table.smeup;
|
|
52
|
+
// Extract all properties except 'smeup' from the table object
|
|
53
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
54
|
+
const { smeup, ...tableRest } = element.table;
|
|
55
|
+
return {
|
|
56
|
+
...element,
|
|
57
|
+
table: {
|
|
58
|
+
...tableRest,
|
|
59
|
+
smeup: {
|
|
60
|
+
...smeupRest,
|
|
61
|
+
},
|
|
62
|
+
body,
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
};
|
|
66
|
+
};
|
|
67
|
+
exports.smeupTableToPdfMakeTableAdapter = smeupTableToPdfMakeTableAdapter;
|
|
68
|
+
/**
|
|
69
|
+
* Adapter function for converting a table element with a `fun` property to a Smeup table structure.
|
|
70
|
+
*
|
|
71
|
+
* This function returns an asynchronous converter that checks if the provided element contains
|
|
72
|
+
* a table with a `fun` property. If present, it uses the `getSmeupDataStructure` function from
|
|
73
|
+
* the context to fetch the table data corresponding to the `fun` string, and replaces the original
|
|
74
|
+
* table's `fun` property with the fetched data under the `data` property. All other properties
|
|
75
|
+
* of the table are preserved.
|
|
76
|
+
*
|
|
77
|
+
* @returns An asynchronous function that takes an element and a context, and returns the adapted element.
|
|
78
|
+
*
|
|
79
|
+
* @throws If the context does not provide a `getSmeupDataStructure` function when the `fun` property is used.
|
|
80
|
+
*
|
|
81
|
+
* @param element - The input element to be adapted.
|
|
82
|
+
* @param context - The conversion context, which must provide `getSmeupDataStructure` if the `fun` property is present.
|
|
83
|
+
*/
|
|
84
|
+
const funToSmeupTableAdapter = () => {
|
|
85
|
+
return async (element, context) => {
|
|
86
|
+
if (!hasTableWithFun(element)) {
|
|
87
|
+
return element;
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
if (!context.getSmeupDataStructure) {
|
|
91
|
+
throw new Error("getSmeupDataStructure function is required in context when the fun property is used");
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
const elementWithTable = element;
|
|
95
|
+
const funStr = elementWithTable.table.smeup.fun;
|
|
96
|
+
const dataTable = (await context.getSmeupDataStructure(funStr));
|
|
97
|
+
// Extract all properties except 'fun' from the smeup object
|
|
98
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
99
|
+
const { fun, ...smeupRest } = elementWithTable.table.smeup;
|
|
100
|
+
// Extract all properties except 'smeup' from the table object
|
|
101
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
102
|
+
const { smeup, ...tableRest } = elementWithTable.table;
|
|
103
|
+
return {
|
|
104
|
+
...elementWithTable,
|
|
105
|
+
table: {
|
|
106
|
+
...tableRest,
|
|
107
|
+
body: tableRest.body,
|
|
108
|
+
smeup: {
|
|
109
|
+
...smeupRest,
|
|
110
|
+
data: dataTable,
|
|
111
|
+
},
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
};
|
|
118
|
+
exports.funToSmeupTableAdapter = funToSmeupTableAdapter;
|
|
119
|
+
const dataTableAdapter = () => {
|
|
120
|
+
return async (element, context) => {
|
|
121
|
+
const adaptedElement = await (0, exports.funToSmeupTableAdapter)()(element, context);
|
|
122
|
+
return (await (0, exports.smeupTableToPdfMakeTableAdapter)()(adaptedElement, context));
|
|
123
|
+
};
|
|
124
|
+
};
|
|
125
|
+
exports.dataTableAdapter = dataTableAdapter;
|
|
126
|
+
//# sourceMappingURL=table-adapter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"table-adapter.js","sourceRoot":"","sources":["../../../../src/converters/pdf/pdfmake/table-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CAAC;gBACnC,MAAM,IAAI,KAAK,CACb,qFAAqF,CACtF,CAAC;YACJ,CAAC;iBAAM,CAAC;gBACN,MAAM,gBAAgB,GAAG,OAAoC,CAAC;gBAC9D,MAAM,MAAM,GAAG,gBAAgB,CAAC,KAAK,CAAC,KAAM,CAAC,GAAI,CAAC;gBAClD,MAAM,SAAS,GAAG,CAAC,MAAM,OAAO,CAAC,qBAAqB,CACpD,MAAM,CACP,CAAmB,CAAC;gBACrB,4DAA4D;gBAC5D,6DAA6D;gBAC7D,MAAM,EAAE,GAAG,EAAE,GAAG,SAAS,EAAE,GAAG,gBAAgB,CAAC,KAAK,CAAC,KAAM,CAAC;gBAC5D,8DAA8D;gBAC9D,6DAA6D;gBAC7D,MAAM,EAAE,KAAK,EAAE,GAAG,SAAS,EAAE,GAAG,gBAAgB,CAAC,KAAK,CAAC;gBAEvD,OAAO;oBACL,GAAG,gBAAgB;oBACnB,KAAK,EAAE;wBACL,GAAG,SAAS;wBACZ,IAAI,EAAE,SAAS,CAAC,IAAI;wBACpB,KAAK,EAAE;4BACL,GAAG,SAAS;4BACZ,IAAI,EAAE,SAAS;yBAChB;qBACF;iBACF,CAAC;YACJ,CAAC;QACH,CAAC;IACH,CAAC,CAAC;AACJ,CAAC,CAAC;AAvCW,QAAA,sBAAsB,0BAuCjC;AAEK,MAAM,gBAAgB,GAAG,GAAG,EAAE;IACnC,OAAO,KAAK,EACV,OAAgB,EAChB,OAAgC,EACI,EAAE;QACtC,MAAM,cAAc,GAAG,MAAM,IAAA,8BAAsB,GAAE,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QACxE,OAAO,CAAC,MAAM,IAAA,uCAA+B,GAAE,CAC7C,cAAc,EACd,OAAO,CACR,CAA8B,CAAC;IAClC,CAAC,CAAC;AACJ,CAAC,CAAC;AAXW,QAAA,gBAAgB,oBAW3B","sourcesContent":["import { TableCell } from \"pdfmake/interfaces\";\nimport { SupportedExportFormats } from \"../../../types\";\nimport { SmeupDataStructureType } from \"../../../types/data-structures/smeupDataStructure\";\nimport {\n SmeupDataCell,\n SmeupDataTable,\n} from \"../../../types/data-structures/smeupDataTable\";\nimport { calculateCellValue } from \"../../../utils/commons-utility\";\nimport {\n ElementWithTableExtension,\n PdfMakeConverterContext,\n TableExtension,\n} from \"./pdfmake.types\";\n\nconst hasTableWithDataTable = (\n element: unknown,\n): element is ElementWithTableExtension => {\n const table = (element as Record<string, unknown>)?.table as TableExtension;\n const data = table?.smeup?.data;\n return (\n data?.type === SmeupDataStructureType.SmeupDataTable &&\n data?.rows !== undefined &&\n data?.columns !== undefined\n );\n};\n\nconst hasTableWithFun = (\n element: unknown,\n): element is ElementWithTableExtension => {\n const table = (element as Record<string, unknown>)?.table as TableExtension;\n const fun = table?.smeup?.fun;\n return typeof fun === \"string\" && fun.length > 0;\n};\n\n/**\n * Adapts a Smeup table structure to the PDFMake table format.\n *\n * This adapter function checks if the provided element contains a table with data.\n * If so, it transforms the table's columns and rows into the PDFMake `body` format,\n * where the first row is the header (column titles) and subsequent rows are the data.\n * All properties of the original table except `data` are preserved.\n *\n * @returns A function that takes an element and returns the adapted element for PDFMake,\n * or the original element if it does not contain a valid table with data.\n */\nexport const smeupTableToPdfMakeTableAdapter = () => {\n return (\n element: unknown,\n context: PdfMakeConverterContext,\n ): ElementWithTableExtension => {\n if (!hasTableWithDataTable(element)) {\n return element as ElementWithTableExtension;\n }\n\n const { columns, rows } = element.table.smeup!.data!;\n\n // Build header row from column titles\n const headerRow = columns.map(col => col.title);\n\n // Build data rows by extracting values in column order\n const dataRows = rows.map(row =>\n columns.map(col => {\n const cell = row.cells?.[col.name] as SmeupDataCell;\n const value = calculateCellValue(\n cell,\n SupportedExportFormats.PDF,\n context.webupManagerData,\n );\n return {\n text: value != null ? String(value) : \"\",\n alignment: col.obj?.t === \"NR\" ? \"right\" : \"left\",\n };\n }),\n );\n\n // Combine header and data rows\n const body = [headerRow, ...dataRows] as TableCell[][];\n\n // Extract all properties except 'data' from the smeup object\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n const { data, ...smeupRest } = element.table.smeup!;\n // Extract all properties except 'smeup' from the table object\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n const { smeup, ...tableRest } = element.table;\n\n return {\n ...element,\n table: {\n ...tableRest,\n smeup: {\n ...smeupRest,\n },\n body,\n },\n };\n };\n};\n\n/**\n * Adapter function for converting a table element with a `fun` property to a Smeup table structure.\n *\n * This function returns an asynchronous converter that checks if the provided element contains\n * a table with a `fun` property. If present, it uses the `getSmeupDataStructure` function from\n * the context to fetch the table data corresponding to the `fun` string, and replaces the original\n * table's `fun` property with the fetched data under the `data` property. All other properties\n * of the table are preserved.\n *\n * @returns An asynchronous function that takes an element and a context, and returns the adapted element.\n *\n * @throws If the context does not provide a `getSmeupDataStructure` function when the `fun` property is used.\n *\n * @param element - The input element to be adapted.\n * @param context - The conversion context, which must provide `getSmeupDataStructure` if the `fun` property is present.\n */\nexport const funToSmeupTableAdapter = () => {\n return async (\n element: unknown,\n context: PdfMakeConverterContext,\n ): Promise<ElementWithTableExtension> => {\n if (!hasTableWithFun(element)) {\n return element as ElementWithTableExtension;\n } else {\n if (!context.getSmeupDataStructure) {\n throw new Error(\n \"getSmeupDataStructure function is required in context when the fun property is used\",\n );\n } else {\n const elementWithTable = element as ElementWithTableExtension;\n const funStr = elementWithTable.table.smeup!.fun!;\n const dataTable = (await context.getSmeupDataStructure(\n funStr,\n )) as SmeupDataTable;\n // Extract all properties except 'fun' from the smeup object\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n const { fun, ...smeupRest } = elementWithTable.table.smeup!;\n // Extract all properties except 'smeup' from the table object\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n const { smeup, ...tableRest } = elementWithTable.table;\n\n return {\n ...elementWithTable,\n table: {\n ...tableRest,\n body: tableRest.body,\n smeup: {\n ...smeupRest,\n data: dataTable,\n },\n },\n };\n }\n }\n };\n};\n\nexport const dataTableAdapter = () => {\n return async (\n element: unknown,\n context: PdfMakeConverterContext,\n ): Promise<ElementWithTableExtension> => {\n const adaptedElement = await funToSmeupTableAdapter()(element, context);\n return (await smeupTableToPdfMakeTableAdapter()(\n adaptedElement,\n context,\n )) as ElementWithTableExtension;\n };\n};\n"]}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { TDocumentDefinitions } from "pdfmake/interfaces";
|
|
2
|
+
/**
|
|
3
|
+
* Renders a pdfmake document definition to a PDF Buffer.
|
|
4
|
+
*
|
|
5
|
+
* @param documentDefinition - The pdfmake document definition object
|
|
6
|
+
* @returns Promise that resolves to a Buffer containing the PDF data
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* const docDef = {
|
|
11
|
+
* content: [
|
|
12
|
+
* { text: 'Hello World', fontSize: 20 },
|
|
13
|
+
* { table: { body: [['Cell 1', 'Cell 2']] } }
|
|
14
|
+
* ]
|
|
15
|
+
* };
|
|
16
|
+
*
|
|
17
|
+
* const pdfBuffer = await renderPdfmakeDocument(docDef);
|
|
18
|
+
* await fs.writeFile('output.pdf', pdfBuffer);
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export declare function renderPdfMakeDocument(documentDefinition: TDocumentDefinitions): Promise<Buffer>;
|