@sme.up/doc-alchemist 1.5.0-SNAPSHOT-20251017143656 → 1.5.0-SNAPSHOT-20251118133453
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/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-converter.d.ts +3 -0
- package/dist/converters/pdf-converter.js +3 -1
- package/dist/converters/pdf-converter.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration options for PDF form filling
|
|
3
|
+
*/
|
|
4
|
+
export interface FillPdfFormOptions {
|
|
5
|
+
/**
|
|
6
|
+
* Font size to use for text fields (default: 10)
|
|
7
|
+
* Smaller values help prevent text truncation
|
|
8
|
+
*/
|
|
9
|
+
fontSize?: number;
|
|
10
|
+
/**
|
|
11
|
+
* Whether to flatten the form after filling (default: true)
|
|
12
|
+
* When true, form fields become non-editable
|
|
13
|
+
*/
|
|
14
|
+
flatten?: boolean;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Fills PDF form fields with provided data and flattens the form
|
|
18
|
+
* @param pdfBuffer - PDF file buffer containing form fields
|
|
19
|
+
* @param formData - Key-value pairs where keys match PDF field names
|
|
20
|
+
* @param options - Optional configuration for form filling
|
|
21
|
+
* @returns Modified PDF buffer with filled and flattened form fields
|
|
22
|
+
* @throws Error if PDF buffer is invalid or cannot be processed
|
|
23
|
+
*/
|
|
24
|
+
export declare function fillPdfForm(pdfBuffer: Buffer, formData: Record<string, string | number | boolean>, options?: FillPdfFormOptions): Promise<Buffer>;
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.fillPdfForm = fillPdfForm;
|
|
4
|
+
const pdf_lib_1 = require("pdf-lib");
|
|
5
|
+
/**
|
|
6
|
+
* Fills PDF form fields with provided data and flattens the form
|
|
7
|
+
* @param pdfBuffer - PDF file buffer containing form fields
|
|
8
|
+
* @param formData - Key-value pairs where keys match PDF field names
|
|
9
|
+
* @param options - Optional configuration for form filling
|
|
10
|
+
* @returns Modified PDF buffer with filled and flattened form fields
|
|
11
|
+
* @throws Error if PDF buffer is invalid or cannot be processed
|
|
12
|
+
*/
|
|
13
|
+
async function fillPdfForm(pdfBuffer, formData, options) {
|
|
14
|
+
// Set default options
|
|
15
|
+
const fontSize = options?.fontSize ?? 10;
|
|
16
|
+
const flatten = options?.flatten ?? true;
|
|
17
|
+
// Validate input
|
|
18
|
+
if (!pdfBuffer || pdfBuffer.length === 0) {
|
|
19
|
+
throw new Error("Invalid PDF buffer: buffer is empty or undefined");
|
|
20
|
+
}
|
|
21
|
+
if (!formData || typeof formData !== "object") {
|
|
22
|
+
throw new Error("Invalid form data: must be a non-null object");
|
|
23
|
+
}
|
|
24
|
+
try {
|
|
25
|
+
// Load the PDF document
|
|
26
|
+
const pdfDoc = await pdf_lib_1.PDFDocument.load(pdfBuffer);
|
|
27
|
+
// Get the form from the PDF
|
|
28
|
+
const form = pdfDoc.getForm();
|
|
29
|
+
// Get all fields in the form
|
|
30
|
+
const fields = form.getFields();
|
|
31
|
+
// Create a map of field names to fields for quick lookup
|
|
32
|
+
const fieldMap = new Map();
|
|
33
|
+
fields.forEach(field => {
|
|
34
|
+
const fieldName = field.getName();
|
|
35
|
+
fieldMap.set(fieldName, field);
|
|
36
|
+
});
|
|
37
|
+
// Process each entry in the form data
|
|
38
|
+
for (const [fieldName, fieldValue] of Object.entries(formData)) {
|
|
39
|
+
const field = fieldMap.get(fieldName);
|
|
40
|
+
// If field doesn't exist, log warning and continue
|
|
41
|
+
if (!field) {
|
|
42
|
+
console.warn(`Field "${fieldName}" not found in PDF form. Skipping.`);
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
// Handle null or undefined values
|
|
46
|
+
if (fieldValue === null || fieldValue === undefined) {
|
|
47
|
+
console.warn(`Skipping field "${fieldName}" with null or undefined value.`);
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
try {
|
|
51
|
+
// Fill the field based on its type
|
|
52
|
+
if (field instanceof pdf_lib_1.PDFTextField) {
|
|
53
|
+
fillTextField(field, fieldValue, fontSize);
|
|
54
|
+
}
|
|
55
|
+
else if (field instanceof pdf_lib_1.PDFCheckBox) {
|
|
56
|
+
fillCheckBox(field, fieldValue);
|
|
57
|
+
}
|
|
58
|
+
else if (field instanceof pdf_lib_1.PDFRadioGroup) {
|
|
59
|
+
fillRadioGroup(field, fieldValue);
|
|
60
|
+
}
|
|
61
|
+
else if (field instanceof pdf_lib_1.PDFDropdown) {
|
|
62
|
+
fillDropdown(field, fieldValue);
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
console.warn(`Unsupported field type for "${fieldName}". Skipping.`);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
console.warn(`Error filling field "${fieldName}": ${error instanceof Error ? error.message : String(error)}`);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
// Flatten the form to make fields non-editable (if requested)
|
|
73
|
+
if (flatten) {
|
|
74
|
+
form.flatten();
|
|
75
|
+
}
|
|
76
|
+
// Save and return the modified PDF
|
|
77
|
+
const pdfBytes = await pdfDoc.save();
|
|
78
|
+
return Buffer.from(pdfBytes);
|
|
79
|
+
}
|
|
80
|
+
catch (error) {
|
|
81
|
+
if (error instanceof Error) {
|
|
82
|
+
throw new Error(`Failed to process PDF form: ${error.message}`);
|
|
83
|
+
}
|
|
84
|
+
throw new Error("Failed to process PDF form: Unknown error");
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Fills a text field with the provided value
|
|
89
|
+
* @param field - PDF text field to fill
|
|
90
|
+
* @param value - Value to set (converted to string)
|
|
91
|
+
* @param fontSize - Font size to use for the text field
|
|
92
|
+
*/
|
|
93
|
+
function fillTextField(field, value, fontSize) {
|
|
94
|
+
const stringValue = String(value);
|
|
95
|
+
// Set text content
|
|
96
|
+
field.setText(stringValue);
|
|
97
|
+
// Set font size to ensure text fits and is readable
|
|
98
|
+
// Using a smaller font size helps prevent text truncation
|
|
99
|
+
try {
|
|
100
|
+
field.setFontSize(fontSize);
|
|
101
|
+
}
|
|
102
|
+
catch {
|
|
103
|
+
// If setting font size fails, continue without it
|
|
104
|
+
// This can happen if the field has specific formatting restrictions
|
|
105
|
+
}
|
|
106
|
+
// Enable multiline if the field supports it and text is long
|
|
107
|
+
try {
|
|
108
|
+
if (stringValue.length > 50 || stringValue.includes('\n')) {
|
|
109
|
+
field.enableMultiline();
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
catch {
|
|
113
|
+
// If enabling multiline fails, continue without it
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Fills a checkbox field with the provided boolean value
|
|
118
|
+
* @param field - PDF checkbox field to fill
|
|
119
|
+
* @param value - Boolean value or string representation
|
|
120
|
+
*/
|
|
121
|
+
function fillCheckBox(field, value) {
|
|
122
|
+
// Handle boolean values
|
|
123
|
+
if (typeof value === "boolean") {
|
|
124
|
+
if (value) {
|
|
125
|
+
field.check();
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
field.uncheck();
|
|
129
|
+
}
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
// Handle string representations of booleans
|
|
133
|
+
if (typeof value === "string") {
|
|
134
|
+
const lowerValue = value.toLowerCase();
|
|
135
|
+
if (lowerValue === "true" || lowerValue === "yes" || lowerValue === "1") {
|
|
136
|
+
field.check();
|
|
137
|
+
}
|
|
138
|
+
else if (lowerValue === "false" || lowerValue === "no" || lowerValue === "0") {
|
|
139
|
+
field.uncheck();
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
throw new Error(`Invalid checkbox value: "${value}". Expected boolean or "true"/"false".`);
|
|
143
|
+
}
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
// Handle numeric values (0 = unchecked, non-zero = checked)
|
|
147
|
+
if (typeof value === "number") {
|
|
148
|
+
if (value !== 0) {
|
|
149
|
+
field.check();
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
field.uncheck();
|
|
153
|
+
}
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Fills a radio button group with the provided option value
|
|
159
|
+
* @param field - PDF radio group field to fill
|
|
160
|
+
* @param value - Option value to select
|
|
161
|
+
*/
|
|
162
|
+
function fillRadioGroup(field, value) {
|
|
163
|
+
const stringValue = String(value);
|
|
164
|
+
// Get available options
|
|
165
|
+
const options = field.getOptions();
|
|
166
|
+
// Check if the value matches any available option
|
|
167
|
+
if (!options.includes(stringValue)) {
|
|
168
|
+
throw new Error(`Invalid radio group value: "${stringValue}". Available options: ${options.join(", ")}`);
|
|
169
|
+
}
|
|
170
|
+
field.select(stringValue);
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Fills a dropdown field with the provided option value
|
|
174
|
+
* @param field - PDF dropdown field to fill
|
|
175
|
+
* @param value - Option value to select
|
|
176
|
+
*/
|
|
177
|
+
function fillDropdown(field, value) {
|
|
178
|
+
const stringValue = String(value);
|
|
179
|
+
// Get available options
|
|
180
|
+
const options = field.getOptions();
|
|
181
|
+
// Check if the value matches any available option
|
|
182
|
+
if (!options.includes(stringValue)) {
|
|
183
|
+
throw new Error(`Invalid dropdown value: "${stringValue}". Available options: ${options.join(", ")}`);
|
|
184
|
+
}
|
|
185
|
+
field.select(stringValue);
|
|
186
|
+
}
|
|
187
|
+
//# sourceMappingURL=form-filler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"form-filler.js","sourceRoot":"","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AIG;AACH,SAAS,YAAY,CACnB,KAAkB,EAClB,KAAgC;IAEhC,MAAM,WAAW,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC;IAElC,wBAAwB;IACxB,MAAM,OAAO,GAAG,KAAK,CAAC,UAAU,EAAE,CAAC;IAEnC,kDAAkD;IAClD,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAC,EAAE,CAAC;QACnC,MAAM,IAAI,KAAK,CACb,4BAA4B,WAAW,yBAAyB,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CACrF,CAAC;IACJ,CAAC;IAED,KAAK,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;AAC5B,CAAC","sourcesContent":["import { PDFDocument, PDFTextField, PDFCheckBox, PDFRadioGroup, PDFDropdown } from \"pdf-lib\";\n\n/**\n * Configuration options for PDF form filling\n */\nexport interface FillPdfFormOptions {\n /**\n * Font size to use for text fields (default: 10)\n * Smaller values help prevent text truncation\n */\n fontSize?: number;\n\n /**\n * Whether to flatten the form after filling (default: true)\n * When true, form fields become non-editable\n */\n flatten?: boolean;\n}\n\n/**\n * Fills PDF form fields with provided data and flattens the form\n * @param pdfBuffer - PDF file buffer containing form fields\n * @param formData - Key-value pairs where keys match PDF field names\n * @param options - Optional configuration for form filling\n * @returns Modified PDF buffer with filled and flattened form fields\n * @throws Error if PDF buffer is invalid or cannot be processed\n */\nexport async function fillPdfForm(\n pdfBuffer: Buffer,\n formData: Record<string, string | number | boolean>,\n options?: FillPdfFormOptions,\n): Promise<Buffer> {\n // Set default options\n const fontSize = options?.fontSize ?? 10;\n const flatten = options?.flatten ?? true;\n // Validate input\n if (!pdfBuffer || pdfBuffer.length === 0) {\n throw new Error(\"Invalid PDF buffer: buffer is empty or undefined\");\n }\n\n if (!formData || typeof formData !== \"object\") {\n throw new Error(\"Invalid form data: must be a non-null object\");\n }\n\n try {\n // Load the PDF document\n const pdfDoc = await PDFDocument.load(pdfBuffer);\n\n // Get the form from the PDF\n const form = pdfDoc.getForm();\n\n // Get all fields in the form\n const fields = form.getFields();\n\n // Create a map of field names to fields for quick lookup\n const fieldMap = new Map<string, ReturnType<typeof form.getFields>[number]>();\n fields.forEach(field => {\n const fieldName = field.getName();\n fieldMap.set(fieldName, field);\n });\n\n // Process each entry in the form data\n for (const [fieldName, fieldValue] of Object.entries(formData)) {\n const field = fieldMap.get(fieldName);\n\n // If field doesn't exist, log warning and continue\n if (!field) {\n console.warn(`Field \"${fieldName}\" not found in PDF form. Skipping.`);\n continue;\n }\n\n // Handle null or undefined values\n if (fieldValue === null || fieldValue === undefined) {\n console.warn(`Skipping field \"${fieldName}\" with null or undefined value.`);\n continue;\n }\n\n try {\n // Fill the field based on its type\n if (field instanceof PDFTextField) {\n fillTextField(field, fieldValue, fontSize);\n } else if (field instanceof PDFCheckBox) {\n fillCheckBox(field, fieldValue);\n } else if (field instanceof PDFRadioGroup) {\n fillRadioGroup(field, fieldValue);\n } else if (field instanceof PDFDropdown) {\n fillDropdown(field, fieldValue);\n } else {\n console.warn(`Unsupported field type for \"${fieldName}\". Skipping.`);\n }\n } catch (error) {\n console.warn(`Error filling field \"${fieldName}\": ${error instanceof Error ? error.message : String(error)}`);\n }\n }\n\n // Flatten the form to make fields non-editable (if requested)\n if (flatten) {\n form.flatten();\n }\n\n // Save and return the modified PDF\n const pdfBytes = await pdfDoc.save();\n return Buffer.from(pdfBytes);\n } catch (error) {\n if (error instanceof Error) {\n throw new Error(`Failed to process PDF form: ${error.message}`);\n }\n throw new Error(\"Failed to process PDF form: Unknown error\");\n }\n}\n\n/**\n * Fills a text field with the provided value\n * @param field - PDF text field to fill\n * @param value - Value to set (converted to string)\n * @param fontSize - Font size to use for the text field\n */\nfunction fillTextField(\n field: PDFTextField,\n value: string | number | boolean,\n fontSize: number,\n): void {\n const stringValue = String(value);\n\n // Set text content\n field.setText(stringValue);\n\n // Set font size to ensure text fits and is readable\n // Using a smaller font size helps prevent text truncation\n try {\n field.setFontSize(fontSize);\n } catch {\n // If setting font size fails, continue without it\n // This can happen if the field has specific formatting restrictions\n }\n\n // Enable multiline if the field supports it and text is long\n try {\n if (stringValue.length > 50 || stringValue.includes('\\n')) {\n field.enableMultiline();\n }\n } catch {\n // If enabling multiline fails, continue without it\n }\n}\n\n/**\n * Fills a checkbox field with the provided boolean value\n * @param field - PDF checkbox field to fill\n * @param value - Boolean value or string representation\n */\nfunction fillCheckBox(\n field: PDFCheckBox,\n value: string | number | boolean,\n): void {\n // Handle boolean values\n if (typeof value === \"boolean\") {\n if (value) {\n field.check();\n } else {\n field.uncheck();\n }\n return;\n }\n\n // Handle string representations of booleans\n if (typeof value === \"string\") {\n const lowerValue = value.toLowerCase();\n if (lowerValue === \"true\" || lowerValue === \"yes\" || lowerValue === \"1\") {\n field.check();\n } else if (lowerValue === \"false\" || lowerValue === \"no\" || lowerValue === \"0\") {\n field.uncheck();\n } else {\n throw new Error(`Invalid checkbox value: \"${value}\". Expected boolean or \"true\"/\"false\".`);\n }\n return;\n }\n\n // Handle numeric values (0 = unchecked, non-zero = checked)\n if (typeof value === \"number\") {\n if (value !== 0) {\n field.check();\n } else {\n field.uncheck();\n }\n return;\n }\n}\n\n/**\n * Fills a radio button group with the provided option value\n * @param field - PDF radio group field to fill\n * @param value - Option value to select\n */\nfunction fillRadioGroup(\n field: PDFRadioGroup,\n value: string | number | boolean,\n): void {\n const stringValue = String(value);\n\n // Get available options\n const options = field.getOptions();\n\n // Check if the value matches any available option\n if (!options.includes(stringValue)) {\n throw new Error(\n `Invalid radio group value: \"${stringValue}\". Available options: ${options.join(\", \")}`,\n );\n }\n\n field.select(stringValue);\n}\n\n/**\n * Fills a dropdown field with the provided option value\n * @param field - PDF dropdown field to fill\n * @param value - Option value to select\n */\nfunction fillDropdown(\n field: PDFDropdown,\n value: string | number | boolean,\n): void {\n const stringValue = String(value);\n\n // Get available options\n const options = field.getOptions();\n\n // Check if the value matches any available option\n if (!options.includes(stringValue)) {\n throw new Error(\n `Invalid dropdown value: \"${stringValue}\". Available options: ${options.join(\", \")}`,\n );\n }\n\n field.select(stringValue);\n}\n"]}
|
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import { SmeupDataTable } from "../types/data-structures/smeupDataTable";
|
|
2
2
|
import { SmeupSch } from "../types/data-structures/smeupSch";
|
|
3
3
|
import { WebupManagerData, GenericObject } from "../types/index";
|
|
4
|
+
import { fillPdfForm, FillPdfFormOptions } from "./pdf/form-filler";
|
|
4
5
|
export declare const schedaToPdfData: (sch: SmeupSch, webupManagerData: WebupManagerData) => Promise<Buffer>;
|
|
5
6
|
export declare const dataTableToPdfData: (component: {
|
|
6
7
|
smeupDataTable: SmeupDataTable;
|
|
7
8
|
props: GenericObject;
|
|
8
9
|
}, webupManagerData: WebupManagerData) => Promise<Buffer | Uint8Array>;
|
|
10
|
+
export { fillPdfForm };
|
|
11
|
+
export type { FillPdfFormOptions };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.dataTableToPdfData = exports.schedaToPdfData = void 0;
|
|
4
|
+
exports.fillPdfForm = exports.dataTableToPdfData = exports.schedaToPdfData = void 0;
|
|
5
5
|
const index_1 = require("../types/index");
|
|
6
6
|
const commons_utility_1 = require("../utils/commons-utility");
|
|
7
7
|
const matrix_converter_1 = require("./excel/matrix-converter");
|
|
@@ -10,6 +10,8 @@ const sch_converter_1 = require("./pdf/sch-converter");
|
|
|
10
10
|
const pdf_lib_1 = require("pdf-lib");
|
|
11
11
|
const cover_renderer_1 = require("./pdf/cover-renderer");
|
|
12
12
|
const gfx_data_1 = require("../assets/gfx-data");
|
|
13
|
+
const form_filler_1 = require("./pdf/form-filler");
|
|
14
|
+
Object.defineProperty(exports, "fillPdfForm", { enumerable: true, get: function () { return form_filler_1.fillPdfForm; } });
|
|
13
15
|
const schedaToPdfData = async (sch, webupManagerData) => {
|
|
14
16
|
const doc = await (0, sch_converter_1.schedaToPdfDoc)(sch);
|
|
15
17
|
return Buffer.from(doc.output("arraybuffer"));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pdf-converter.js","sourceRoot":"","sources":["../../src/converters/pdf-converter.ts"],"names":[],"mappings":";AAAA,sDAAsD;;;AAKtD,0CAIwB;AACxB,8DAA2D;AAC3D,+DAAoE;AACpE,6DAA2D;AAC3D,uDAAqD;AACrD,qCAAsC;AACtC,yDAAsD;AACtD,iDAAgD;
|
|
1
|
+
{"version":3,"file":"pdf-converter.js","sourceRoot":"","sources":["../../src/converters/pdf-converter.ts"],"names":[],"mappings":";AAAA,sDAAsD;;;AAKtD,0CAIwB;AACxB,8DAA2D;AAC3D,+DAAoE;AACpE,6DAA2D;AAC3D,uDAAqD;AACrD,qCAAsC;AACtC,yDAAsD;AACtD,iDAAgD;AAChD,mDAAoE;AAsE3D,4FAtEA,yBAAW,OAsEA;AApEb,MAAM,eAAe,GAAG,KAAK,EAClC,GAAa,EACb,gBAAkC,EACjB,EAAE;IACnB,MAAM,GAAG,GAAG,MAAM,IAAA,8BAAc,EAAC,GAAG,CAAC,CAAC;IACtC,OAAO,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC,CAAC;AAChD,CAAC,CAAC;AANW,QAAA,eAAe,mBAM1B;AAEK,MAAM,kBAAkB,GAAG,KAAK,EACrC,SAGC,EACD,gBAAkC,EACJ,EAAE;IAChC,MAAM,QAAQ,GAAG,IAAA,2CAAwB,EACvC,SAAS,EACT,8BAAsB,CAAC,IAAI,EAC3B,gBAAgB,CACjB,CAAC;IAEF,MAAM,SAAS,GAAG,QAAQ,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC;IAC3C,IAAI,SAAS,EAAE,CAAC;QACd,MAAM,KAAK,GAAG,SAAS,CAAC,cAAc,CAAC,KAAK,EAAE,MAAM,EAAE,CAAC,KAAK,CAAC,IAAI,EAA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Q,IAAI,UAAU,EAAE,CAAC;QAClC,MAAM,MAAM,GAAG,MAAM,qBAAW,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;QAChD,MAAM,WAAW,GAAG,MAAM,SAAS,CAAC,SAAS,CAC3C,MAAM,EACN,MAAM,CAAC,cAAc,EAAE,CACxB,CAAC;QACF,WAAW,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC,SAAS,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC;IACvD,CAAC;IAED,MAAM,WAAW,GAAG,MAAM,SAAS,CAAC,IAAI,EAAE,CAAC;IAC3C,OAAO,WAAW,CAAC;AACrB,CAAC,CAAC","sourcesContent":["/* eslint-disable @typescript-eslint/no-unused-vars */\n\nimport { create } from \"domain\";\nimport { SmeupDataTable } from \"../types/data-structures/smeupDataTable\";\nimport { SmeupSch } from \"../types/data-structures/smeupSch\";\nimport {\n WebupManagerData,\n GenericObject,\n SupportedExportFormats,\n} from \"../types/index\";\nimport { convertToBuffer } from \"../utils/commons-utility\";\nimport { dataTableToExcelWorkbook } from \"./excel/matrix-converter\";\nimport { dataTableToPdfDoc } from \"./pdf/matrix-converter\";\nimport { schedaToPdfDoc } from \"./pdf/sch-converter\";\nimport { PDFDocument } from \"pdf-lib\";\nimport { createCoverPdf } from \"./pdf/cover-renderer\";\nimport { logoBase64 } from \"../assets/gfx-data\";\nimport { fillPdfForm, FillPdfFormOptions } from \"./pdf/form-filler\";\n\nexport const schedaToPdfData = async (\n sch: SmeupSch,\n webupManagerData: WebupManagerData,\n): Promise<Buffer> => {\n const doc = await schedaToPdfDoc(sch);\n return Buffer.from(doc.output(\"arraybuffer\"));\n};\n\nexport const dataTableToPdfData = async (\n component: {\n smeupDataTable: SmeupDataTable;\n props: GenericObject;\n },\n webupManagerData: WebupManagerData,\n): Promise<Buffer | Uint8Array> => {\n const workbook = dataTableToExcelWorkbook(\n component,\n SupportedExportFormats.XLSX,\n webupManagerData,\n );\n\n const worksheet = workbook.getWorksheet(1);\n if (worksheet) {\n const title = component.smeupDataTable.cover?.titles?.[\"T01\"] ?? \"\";\n const subtitle = component.smeupDataTable.cover?.titles?.[\"T02\"] ?? \"\";\n const subtitle2 = component.smeupDataTable.cover?.titles?.[\"T03\"] ?? \"\";\n const image = component.smeupDataTable.cover?.image ?? \"\";\n const logo = component.smeupDataTable.cover?.logo ?? logoBase64;\n const cover = createCoverPdf(image, logo, title, subtitle, subtitle2);\n const pdfDoc = await dataTableToPdfDoc(worksheet, webupManagerData, {\n logoBase64: logo,\n title: title,\n subtitle: subtitle,\n });\n const pdfBuffer = pdfDoc.output(\"arraybuffer\");\n return convertToBuffer(await appendPdfDocs([cover, pdfBuffer]));\n } else {\n throw new Error(\"Worksheet not found in the workbook\");\n }\n};\n\n/**\n * Unisce più PDF (in formato ArrayBuffer/Uint8Array/Buffer) in un unico PDF.\n * @param pdfBuffers Array di buffer PDF (es. ottenuti da jsPDF.output(\"arraybuffer\"))\n * @returns Buffer del PDF unito\n */\nconst appendPdfDocs = async (\n pdfBuffers: (Uint8Array | ArrayBuffer | Buffer)[],\n): Promise<Uint8Array> => {\n if (!pdfBuffers.length) throw new Error(\"No PDF buffers to append\");\n\n // Crea un nuovo documento PDF vuoto\n const mergedPdf = await PDFDocument.create();\n\n for (const pdfBytes of pdfBuffers) {\n const srcPdf = await PDFDocument.load(pdfBytes);\n const copiedPages = await mergedPdf.copyPages(\n srcPdf,\n srcPdf.getPageIndices(),\n );\n copiedPages.forEach(page => mergedPdf.addPage(page));\n }\n\n const mergedBytes = await mergedPdf.save();\n return mergedBytes;\n};\n\n// Re-export form filler function and types\nexport { fillPdfForm };\nexport type { FillPdfFormOptions };\n\n"]}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export { dataTableToExcelData, dataTreeToExcelData, } from "./converters/excel-converter";
|
|
2
|
-
export { schedaToPdfData, dataTableToPdfData, } from "./converters/pdf-converter";
|
|
2
|
+
export { schedaToPdfData, dataTableToPdfData, fillPdfForm, } from "./converters/pdf-converter";
|
|
3
|
+
export type { FillPdfFormOptions } from "./converters/pdf-converter";
|
|
3
4
|
export { dataTableToChart } from "./converters/images/charts-generator";
|
|
4
5
|
export type { WebupManagerData, GenericObject } from "./types/index";
|
|
5
6
|
export { SupportedExportFormats } from "./types/index";
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.SupportedExportFormats = exports.dataTableToChart = exports.dataTableToPdfData = exports.schedaToPdfData = exports.dataTreeToExcelData = exports.dataTableToExcelData = void 0;
|
|
3
|
+
exports.SupportedExportFormats = exports.dataTableToChart = exports.fillPdfForm = exports.dataTableToPdfData = exports.schedaToPdfData = exports.dataTreeToExcelData = exports.dataTableToExcelData = void 0;
|
|
4
4
|
// Public API exports
|
|
5
5
|
var excel_converter_1 = require("./converters/excel-converter");
|
|
6
6
|
Object.defineProperty(exports, "dataTableToExcelData", { enumerable: true, get: function () { return excel_converter_1.dataTableToExcelData; } });
|
|
@@ -8,6 +8,7 @@ Object.defineProperty(exports, "dataTreeToExcelData", { enumerable: true, get: f
|
|
|
8
8
|
var pdf_converter_1 = require("./converters/pdf-converter");
|
|
9
9
|
Object.defineProperty(exports, "schedaToPdfData", { enumerable: true, get: function () { return pdf_converter_1.schedaToPdfData; } });
|
|
10
10
|
Object.defineProperty(exports, "dataTableToPdfData", { enumerable: true, get: function () { return pdf_converter_1.dataTableToPdfData; } });
|
|
11
|
+
Object.defineProperty(exports, "fillPdfForm", { enumerable: true, get: function () { return pdf_converter_1.fillPdfForm; } });
|
|
11
12
|
var charts_generator_1 = require("./converters/images/charts-generator");
|
|
12
13
|
Object.defineProperty(exports, "dataTableToChart", { enumerable: true, get: function () { return charts_generator_1.dataTableToChart; } });
|
|
13
14
|
var index_1 = require("./types/index");
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,qBAAqB;AACrB,gEAGsC;AAFpC,uHAAA,oBAAoB,OAAA;AACpB,sHAAA,mBAAmB,OAAA;AAGrB,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,qBAAqB;AACrB,gEAGsC;AAFpC,uHAAA,oBAAoB,OAAA;AACpB,sHAAA,mBAAmB,OAAA;AAGrB,4DAIoC;AAHlC,gHAAA,eAAe,OAAA;AACf,mHAAA,kBAAkB,OAAA;AAClB,4GAAA,WAAW,OAAA;AAIb,yEAAwE;AAA/D,oHAAA,gBAAgB,OAAA;AAGzB,uCAAuD;AAA9C,+GAAA,sBAAsB,OAAA","sourcesContent":["// Public API exports\nexport {\n dataTableToExcelData,\n dataTreeToExcelData,\n} from \"./converters/excel-converter\";\n\nexport {\n schedaToPdfData,\n dataTableToPdfData,\n fillPdfForm,\n} from \"./converters/pdf-converter\";\nexport type { FillPdfFormOptions } from \"./converters/pdf-converter\";\n\nexport { dataTableToChart } from \"./converters/images/charts-generator\";\n// Export types that users might need\nexport type { WebupManagerData, GenericObject } from \"./types/index\";\nexport { SupportedExportFormats } from \"./types/index\";\nexport type { SmeupDataTable } from \"./types/data-structures/smeupDataTable\";\nexport type { SmeupDataTree } from \"./types/data-structures/smeupDataTree\";\n\nexport type ColumnGroup = {\n column: string;\n visible: boolean;\n};\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sme.up/doc-alchemist",
|
|
3
|
-
"version": "1.5.0-SNAPSHOT-
|
|
3
|
+
"version": "1.5.0-SNAPSHOT-20251118133453",
|
|
4
4
|
"description": "Library for generating documents in various formats, including Excel and PDF.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "Smeup LAB <info@smeup.com> (https://www.smeup.com/)",
|