emilsoftware-utilities 1.3.67 → 1.3.69
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/ContractGenerator.d.ts +12 -119
- package/dist/ContractGenerator.js +68 -128
- package/dist/tests/contract/test.js +10 -0
- package/package.json +3 -3
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Interface representing the contract parties.
|
|
3
|
-
*/
|
|
4
1
|
export interface IPartiContratto {
|
|
5
2
|
fornitore: {
|
|
6
3
|
denominazione: string;
|
|
@@ -13,9 +10,6 @@ export interface IPartiContratto {
|
|
|
13
10
|
indirizzoCompleto: string;
|
|
14
11
|
};
|
|
15
12
|
}
|
|
16
|
-
/**
|
|
17
|
-
* Parameters passed by the user when generating a document.
|
|
18
|
-
*/
|
|
19
13
|
export interface DocumentParams {
|
|
20
14
|
parti: IPartiContratto;
|
|
21
15
|
tipOutput?: 'f' | 'd' | 'u';
|
|
@@ -26,20 +20,15 @@ export interface DocumentParams {
|
|
|
26
20
|
[placeholder: string]: DynamicElement;
|
|
27
21
|
};
|
|
28
22
|
}
|
|
29
|
-
/**
|
|
30
|
-
* DynamicElement supports different types, currently "table".
|
|
31
|
-
*/
|
|
32
23
|
export type DynamicElement = {
|
|
33
24
|
type: 'table';
|
|
34
25
|
config: {
|
|
35
26
|
head: string[];
|
|
36
27
|
body: string[][];
|
|
37
28
|
options?: any;
|
|
29
|
+
styles?: any;
|
|
38
30
|
};
|
|
39
31
|
};
|
|
40
|
-
/**
|
|
41
|
-
* A simplified type for the configuration file.
|
|
42
|
-
*/
|
|
43
32
|
interface DocumentConfig {
|
|
44
33
|
fontTitolo: {
|
|
45
34
|
nome: string;
|
|
@@ -89,12 +78,15 @@ interface DocumentConfig {
|
|
|
89
78
|
backgrund: string;
|
|
90
79
|
raggio: number;
|
|
91
80
|
};
|
|
81
|
+
tableStyle?: {
|
|
82
|
+
lineColor?: string;
|
|
83
|
+
lineWidth?: number;
|
|
84
|
+
font?: string;
|
|
85
|
+
fontSize?: number;
|
|
86
|
+
cellPadding?: number;
|
|
87
|
+
fillColor?: string;
|
|
88
|
+
};
|
|
92
89
|
}
|
|
93
|
-
/**
|
|
94
|
-
* DocumentGenerator encapsulates the PDF generation logic.
|
|
95
|
-
* It handles text wrapping, dynamic placeholders, image insertion, custom font installation,
|
|
96
|
-
* and rendering text inside a rounded rectangle box or as bold text.
|
|
97
|
-
*/
|
|
98
90
|
export declare class DocumentGenerator {
|
|
99
91
|
private configPath?;
|
|
100
92
|
private config;
|
|
@@ -102,127 +94,28 @@ export declare class DocumentGenerator {
|
|
|
102
94
|
private curX;
|
|
103
95
|
private curY;
|
|
104
96
|
private configLoaded;
|
|
105
|
-
/**
|
|
106
|
-
* Constructor for DocumentGenerator.
|
|
107
|
-
* @param configPath - Optional path to the JSON configuration file.
|
|
108
|
-
*/
|
|
109
97
|
constructor(configPath?: string);
|
|
110
|
-
/**
|
|
111
|
-
* Sets the configuration directly.
|
|
112
|
-
* @param config - The DocumentConfig object.
|
|
113
|
-
*/
|
|
114
98
|
setConfig(config: DocumentConfig): void;
|
|
115
|
-
/**
|
|
116
|
-
* Applies dynamic text templating by replacing markers like $KEY$ with their values.
|
|
117
|
-
* @param template - The template string containing markers.
|
|
118
|
-
* @param dynamicFields - An object mapping marker keys to replacement values.
|
|
119
|
-
* @returns The processed string with replacements.
|
|
120
|
-
*/
|
|
121
99
|
private applyTemplate;
|
|
122
|
-
/**
|
|
123
|
-
* Replaces party-specific placeholders (e.g., $cliente:denominazione$) with corresponding values.
|
|
124
|
-
* @param text - The text containing party placeholders.
|
|
125
|
-
* @param parti - The contract parties.
|
|
126
|
-
* @returns The processed string with party-specific values.
|
|
127
|
-
*/
|
|
128
100
|
private applyPartiPlaceholders;
|
|
129
|
-
/**
|
|
130
|
-
* Loads the configuration JSON from the local filesystem.
|
|
131
|
-
* @throws Error if the configuration file cannot be read.
|
|
132
|
-
*/
|
|
133
101
|
private loadConfig;
|
|
134
|
-
/**
|
|
135
|
-
* Initializes the jsPDF document and sets the cursor to the top-left margin.
|
|
136
|
-
*/
|
|
137
102
|
private initDoc;
|
|
138
|
-
/**
|
|
139
|
-
* Extracts a dynamic element placeholder from text if it is in the form "$PLACEHOLDER$".
|
|
140
|
-
* @param text - The text to check.
|
|
141
|
-
* @returns The placeholder key or null if not found.
|
|
142
|
-
*/
|
|
143
103
|
private extractPlaceholder;
|
|
144
|
-
/**
|
|
145
|
-
* Installs a custom font into jsPDF by reading the font file and adding it to the Virtual File System.
|
|
146
|
-
* @param fontPath - Path to the TTF font file.
|
|
147
|
-
* @param fontName - Name of the font to be used in jsPDF.
|
|
148
|
-
*/
|
|
149
104
|
private installFont;
|
|
150
|
-
/**
|
|
151
|
-
* Core method to write wrapped text with a specified font.
|
|
152
|
-
* Handles text splitting and page-breaks.
|
|
153
|
-
* @param fontName - The name of the font to use.
|
|
154
|
-
* @param fontSize - The font size in points.
|
|
155
|
-
* @param color - The text color.
|
|
156
|
-
* @param text - The text to write.
|
|
157
|
-
* @param x - The X coordinate.
|
|
158
|
-
* @param y - The starting Y coordinate.
|
|
159
|
-
* @param maxWidth - Maximum width for text wrapping.
|
|
160
|
-
* @param lineSpacingFactor - Factor for line spacing.
|
|
161
|
-
* @returns The updated Y position after writing the text.
|
|
162
|
-
*/
|
|
163
105
|
private writeWrappedTextCore;
|
|
164
|
-
|
|
165
|
-
* Writes wrapped text using a custom font.
|
|
166
|
-
* Ensures the font is installed before writing.
|
|
167
|
-
* @param fontConf - Font configuration object.
|
|
168
|
-
* @param text - The text to write.
|
|
169
|
-
* @param x - The X coordinate.
|
|
170
|
-
* @param y - The starting Y coordinate.
|
|
171
|
-
* @param maxWidth - Maximum width for text wrapping.
|
|
172
|
-
* @param lineSpacingFactor - Factor for line spacing.
|
|
173
|
-
*/
|
|
106
|
+
private writeLineWithInlineBold;
|
|
174
107
|
private writeWrappedTextWithFont;
|
|
175
|
-
|
|
176
|
-
* Renders text inside a rounded rectangle box.
|
|
177
|
-
* This method is invoked when text is wrapped in '^' markers.
|
|
178
|
-
* @param text - The text to render (without '^' markers).
|
|
179
|
-
* @param fontConf - Font configuration for the text.
|
|
180
|
-
* @param padding - Padding inside the box.
|
|
181
|
-
* @param borderRadius - Corner radius of the box.
|
|
182
|
-
* @param borderWidth - Border thickness.
|
|
183
|
-
* @param boxColor - Optional background color.
|
|
184
|
-
*/
|
|
185
|
-
scriveTestoBox(text: string, fontConf: {
|
|
108
|
+
writeBoxedText(text: string, fontConf: {
|
|
186
109
|
nome: string;
|
|
187
110
|
dimensione: number;
|
|
188
111
|
colore: string;
|
|
189
112
|
installPath?: string;
|
|
190
113
|
}, padding?: number, borderRadius?: number, borderWidth?: number, boxColor?: string): Promise<void>;
|
|
191
|
-
/**
|
|
192
|
-
* Checks if a given text is wrapped in '^' markers, indicating it should be rendered in a box.
|
|
193
|
-
* @param text - The text to check.
|
|
194
|
-
* @returns True if text is boxed.
|
|
195
|
-
*/
|
|
196
114
|
private isBoxedText;
|
|
197
|
-
/**
|
|
198
|
-
* Removes the '^' markers from the text.
|
|
199
|
-
* @param text - The boxed text.
|
|
200
|
-
* @returns The text without '^' markers.
|
|
201
|
-
*/
|
|
202
115
|
private stripBoxMarkers;
|
|
203
|
-
/**
|
|
204
|
-
* Checks if a given text is wrapped in '**' markers, indicating bold formatting.
|
|
205
|
-
* @param text - The text to check.
|
|
206
|
-
* @returns True if text is marked as bold.
|
|
207
|
-
*/
|
|
208
116
|
private isBoldText;
|
|
209
|
-
/**
|
|
210
|
-
* Removes the '**' markers from bold text.
|
|
211
|
-
* @param text - The bold text.
|
|
212
|
-
* @returns The text without '**' markers.
|
|
213
|
-
*/
|
|
214
117
|
private stripBoldMarkers;
|
|
215
|
-
|
|
216
|
-
* Inserts images as defined in the configuration.
|
|
217
|
-
* Uses the current cursor position for placement.
|
|
218
|
-
*/
|
|
219
|
-
private inserisciImmagini;
|
|
220
|
-
/**
|
|
221
|
-
* Generates the PDF document based on provided parameters.
|
|
222
|
-
* Processes dynamic fields, images, tables, and text (with optional boxed or bold formatting).
|
|
223
|
-
* Returns the file name if saved or an ArrayBuffer.
|
|
224
|
-
* @param params - The DocumentParams object.
|
|
225
|
-
*/
|
|
118
|
+
private insertLogo;
|
|
226
119
|
generateDocument(params: DocumentParams): Promise<string | ArrayBuffer>;
|
|
227
120
|
}
|
|
228
121
|
export {};
|
|
@@ -17,11 +17,6 @@ const jspdf_1 = require("jspdf");
|
|
|
17
17
|
const jspdf_autotable_1 = __importDefault(require("jspdf-autotable"));
|
|
18
18
|
const fs_1 = require("fs");
|
|
19
19
|
const path_1 = __importDefault(require("path"));
|
|
20
|
-
/**
|
|
21
|
-
* Reads an image file from the filesystem and returns a Base64-encoded data URL.
|
|
22
|
-
* @param imagePath - Path to the image file.
|
|
23
|
-
* @returns The Base64-encoded data URL of the image.
|
|
24
|
-
*/
|
|
25
20
|
function loadImageAsBase64(imagePath) {
|
|
26
21
|
return __awaiter(this, void 0, void 0, function* () {
|
|
27
22
|
const absolutePath = path_1.default.resolve(imagePath);
|
|
@@ -31,62 +26,31 @@ function loadImageAsBase64(imagePath) {
|
|
|
31
26
|
return `data:image/${ext};base64,${base64}`;
|
|
32
27
|
});
|
|
33
28
|
}
|
|
34
|
-
/**
|
|
35
|
-
* DocumentGenerator encapsulates the PDF generation logic.
|
|
36
|
-
* It handles text wrapping, dynamic placeholders, image insertion, custom font installation,
|
|
37
|
-
* and rendering text inside a rounded rectangle box or as bold text.
|
|
38
|
-
*/
|
|
39
29
|
class DocumentGenerator {
|
|
40
|
-
/**
|
|
41
|
-
* Constructor for DocumentGenerator.
|
|
42
|
-
* @param configPath - Optional path to the JSON configuration file.
|
|
43
|
-
*/
|
|
44
30
|
constructor(configPath) {
|
|
45
31
|
this.configPath = configPath;
|
|
46
32
|
this.curX = 0;
|
|
47
33
|
this.curY = 0;
|
|
48
34
|
this.configLoaded = false;
|
|
49
35
|
}
|
|
50
|
-
/**
|
|
51
|
-
* Sets the configuration directly.
|
|
52
|
-
* @param config - The DocumentConfig object.
|
|
53
|
-
*/
|
|
54
36
|
setConfig(config) {
|
|
55
37
|
this.config = config;
|
|
56
38
|
this.configLoaded = true;
|
|
57
39
|
}
|
|
58
|
-
/**
|
|
59
|
-
* Applies dynamic text templating by replacing markers like $KEY$ with their values.
|
|
60
|
-
* @param template - The template string containing markers.
|
|
61
|
-
* @param dynamicFields - An object mapping marker keys to replacement values.
|
|
62
|
-
* @returns The processed string with replacements.
|
|
63
|
-
*/
|
|
64
40
|
applyTemplate(template, dynamicFields) {
|
|
65
41
|
if (!dynamicFields)
|
|
66
42
|
return template;
|
|
67
43
|
return template.replace(/\$(\w+)\$/g, (match, key) => dynamicFields[key] !== undefined ? dynamicFields[key] : match);
|
|
68
44
|
}
|
|
69
|
-
/**
|
|
70
|
-
* Replaces party-specific placeholders (e.g., $cliente:denominazione$) with corresponding values.
|
|
71
|
-
* @param text - The text containing party placeholders.
|
|
72
|
-
* @param parti - The contract parties.
|
|
73
|
-
* @returns The processed string with party-specific values.
|
|
74
|
-
*/
|
|
75
45
|
applyPartiPlaceholders(text, parti) {
|
|
76
|
-
return text.replace(/\$(fornitore|cliente):(\w+)\$/g, (match, party, field) =>
|
|
77
|
-
return (parti[party] && parti[party][field]) ? parti[party][field] : match;
|
|
78
|
-
});
|
|
46
|
+
return text.replace(/\$(fornitore|cliente):(\w+)\$/g, (match, party, field) => (parti[party] && parti[party][field]) ? parti[party][field] : match);
|
|
79
47
|
}
|
|
80
|
-
/**
|
|
81
|
-
* Loads the configuration JSON from the local filesystem.
|
|
82
|
-
* @throws Error if the configuration file cannot be read.
|
|
83
|
-
*/
|
|
84
48
|
loadConfig() {
|
|
85
49
|
return __awaiter(this, void 0, void 0, function* () {
|
|
86
50
|
if (this.configLoaded)
|
|
87
51
|
return;
|
|
88
52
|
if (!this.configPath)
|
|
89
|
-
throw new Error("No configuration provided.
|
|
53
|
+
throw new Error("No configuration provided.");
|
|
90
54
|
try {
|
|
91
55
|
const data = yield fs_1.promises.readFile(this.configPath, 'utf8');
|
|
92
56
|
this.config = JSON.parse(data);
|
|
@@ -97,29 +61,16 @@ class DocumentGenerator {
|
|
|
97
61
|
}
|
|
98
62
|
});
|
|
99
63
|
}
|
|
100
|
-
/**
|
|
101
|
-
* Initializes the jsPDF document and sets the cursor to the top-left margin.
|
|
102
|
-
*/
|
|
103
64
|
initDoc() {
|
|
104
65
|
this.doc = new jspdf_1.jsPDF();
|
|
105
66
|
const margins = this.config.margini;
|
|
106
67
|
this.curX = margins.sx;
|
|
107
68
|
this.curY = margins.alto;
|
|
108
69
|
}
|
|
109
|
-
/**
|
|
110
|
-
* Extracts a dynamic element placeholder from text if it is in the form "$PLACEHOLDER$".
|
|
111
|
-
* @param text - The text to check.
|
|
112
|
-
* @returns The placeholder key or null if not found.
|
|
113
|
-
*/
|
|
114
70
|
extractPlaceholder(text) {
|
|
115
71
|
const match = text.match(/^\$(\w+)\$$/);
|
|
116
72
|
return match ? match[1] : null;
|
|
117
73
|
}
|
|
118
|
-
/**
|
|
119
|
-
* Installs a custom font into jsPDF by reading the font file and adding it to the Virtual File System.
|
|
120
|
-
* @param fontPath - Path to the TTF font file.
|
|
121
|
-
* @param fontName - Name of the font to be used in jsPDF.
|
|
122
|
-
*/
|
|
123
74
|
installFont(fontPath, fontName) {
|
|
124
75
|
return __awaiter(this, void 0, void 0, function* () {
|
|
125
76
|
const absolutePath = path_1.default.resolve(fontPath);
|
|
@@ -131,19 +82,7 @@ class DocumentGenerator {
|
|
|
131
82
|
this.doc.setFont(fontName);
|
|
132
83
|
});
|
|
133
84
|
}
|
|
134
|
-
|
|
135
|
-
* Core method to write wrapped text with a specified font.
|
|
136
|
-
* Handles text splitting and page-breaks.
|
|
137
|
-
* @param fontName - The name of the font to use.
|
|
138
|
-
* @param fontSize - The font size in points.
|
|
139
|
-
* @param color - The text color.
|
|
140
|
-
* @param text - The text to write.
|
|
141
|
-
* @param x - The X coordinate.
|
|
142
|
-
* @param y - The starting Y coordinate.
|
|
143
|
-
* @param maxWidth - Maximum width for text wrapping.
|
|
144
|
-
* @param lineSpacingFactor - Factor for line spacing.
|
|
145
|
-
* @returns The updated Y position after writing the text.
|
|
146
|
-
*/
|
|
85
|
+
// Writes wrapped text, handling inline bold segments.
|
|
147
86
|
writeWrappedTextCore(fontName_1, fontSize_1, color_1, text_1, x_1, y_1, maxWidth_1) {
|
|
148
87
|
return __awaiter(this, arguments, void 0, function* (fontName, fontSize, color, text, x, y, maxWidth, lineSpacingFactor = 1.15) {
|
|
149
88
|
this.doc.setFont(fontName);
|
|
@@ -156,22 +95,48 @@ class DocumentGenerator {
|
|
|
156
95
|
this.doc.addPage();
|
|
157
96
|
y = this.config.margini.alto;
|
|
158
97
|
}
|
|
159
|
-
|
|
160
|
-
|
|
98
|
+
// If the line contains inline bold markers, process them.
|
|
99
|
+
if (line.includes("**")) {
|
|
100
|
+
y = yield this.writeLineWithInlineBold(line, x, y, fontName, fontSize, color, maxWidth, lineSpacingFactor);
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
this.doc.text(line, x, y);
|
|
104
|
+
y += lineHeight;
|
|
105
|
+
}
|
|
161
106
|
}
|
|
162
107
|
return y;
|
|
163
108
|
});
|
|
164
109
|
}
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
110
|
+
// Writes a single line with inline bold processing.
|
|
111
|
+
writeLineWithInlineBold(line_1, x_1, y_1, normalFont_1, fontSize_1, color_1, maxWidth_1) {
|
|
112
|
+
return __awaiter(this, arguments, void 0, function* (line, x, y, normalFont, fontSize, color, maxWidth, lineSpacingFactor = 1.15) {
|
|
113
|
+
let currentX = x;
|
|
114
|
+
// Split the line by bold segments. The regex keeps the markers.
|
|
115
|
+
const segments = line.split(/(\*\*.*?\*\*)/);
|
|
116
|
+
const lineHeight = (fontSize * lineSpacingFactor / 72) * 25.4;
|
|
117
|
+
for (const seg of segments) {
|
|
118
|
+
let segText = seg;
|
|
119
|
+
let useBold = false;
|
|
120
|
+
if (seg.startsWith("**") && seg.endsWith("**")) {
|
|
121
|
+
useBold = true;
|
|
122
|
+
segText = seg.substring(2, seg.length - 2);
|
|
123
|
+
}
|
|
124
|
+
if (useBold) {
|
|
125
|
+
this.doc.setFont("Montserrat-Bold");
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
this.doc.setFont(normalFont);
|
|
129
|
+
}
|
|
130
|
+
this.doc.setFontSize(fontSize);
|
|
131
|
+
this.doc.setTextColor(color);
|
|
132
|
+
const segWidth = this.doc.getTextWidth(segText);
|
|
133
|
+
// Write segment
|
|
134
|
+
this.doc.text(segText, currentX, y);
|
|
135
|
+
currentX += segWidth;
|
|
136
|
+
}
|
|
137
|
+
return y + lineHeight;
|
|
138
|
+
});
|
|
139
|
+
}
|
|
175
140
|
writeWrappedTextWithFont(fontConf_1, text_1, x_1, y_1, maxWidth_1) {
|
|
176
141
|
return __awaiter(this, arguments, void 0, function* (fontConf, text, x, y, maxWidth, lineSpacingFactor = 1.15) {
|
|
177
142
|
const fontList = this.doc.getFontList();
|
|
@@ -187,17 +152,7 @@ class DocumentGenerator {
|
|
|
187
152
|
this.curY = yield this.writeWrappedTextCore(fontConf.nome, fontConf.dimensione, fontConf.colore, text, x, y, maxWidth, lineSpacingFactor);
|
|
188
153
|
});
|
|
189
154
|
}
|
|
190
|
-
|
|
191
|
-
* Renders text inside a rounded rectangle box.
|
|
192
|
-
* This method is invoked when text is wrapped in '^' markers.
|
|
193
|
-
* @param text - The text to render (without '^' markers).
|
|
194
|
-
* @param fontConf - Font configuration for the text.
|
|
195
|
-
* @param padding - Padding inside the box.
|
|
196
|
-
* @param borderRadius - Corner radius of the box.
|
|
197
|
-
* @param borderWidth - Border thickness.
|
|
198
|
-
* @param boxColor - Optional background color.
|
|
199
|
-
*/
|
|
200
|
-
scriveTestoBox(text_1, fontConf_1) {
|
|
155
|
+
writeBoxedText(text_1, fontConf_1) {
|
|
201
156
|
return __awaiter(this, arguments, void 0, function* (text, fontConf, padding = 5, borderRadius = 5, borderWidth = 1, boxColor) {
|
|
202
157
|
const fontList = this.doc.getFontList();
|
|
203
158
|
if (!fontList[fontConf.nome]) {
|
|
@@ -222,56 +177,43 @@ class DocumentGenerator {
|
|
|
222
177
|
const boxHeight = textHeight + 2 * padding;
|
|
223
178
|
if (boxColor) {
|
|
224
179
|
this.doc.setFillColor(boxColor);
|
|
225
|
-
this.doc.roundedRect(boxX, boxY, boxWidth, boxHeight, borderRadius, borderRadius,
|
|
180
|
+
this.doc.roundedRect(boxX, boxY, boxWidth, boxHeight, borderRadius, borderRadius, "F");
|
|
226
181
|
}
|
|
227
182
|
this.doc.setLineWidth(borderWidth);
|
|
228
183
|
this.doc.roundedRect(boxX, boxY, boxWidth, boxHeight, borderRadius, borderRadius);
|
|
229
184
|
const textX = boxX + padding;
|
|
230
185
|
let textY = boxY + padding + lineHeight;
|
|
231
186
|
for (const line of lines) {
|
|
232
|
-
|
|
233
|
-
|
|
187
|
+
if (line.includes("**")) {
|
|
188
|
+
textY = yield this.writeLineWithInlineBold(line, textX, textY, fontConf.nome, fontConf.dimensione, fontConf.colore, maxTextWidth, 1.15);
|
|
189
|
+
}
|
|
190
|
+
else {
|
|
191
|
+
this.doc.text(line, textX, textY);
|
|
192
|
+
textY += lineHeight;
|
|
193
|
+
}
|
|
234
194
|
}
|
|
235
195
|
this.curY = boxY + boxHeight + this.config.staccoriga;
|
|
236
196
|
});
|
|
237
197
|
}
|
|
238
|
-
|
|
239
|
-
* Checks if a given text is wrapped in '^' markers, indicating it should be rendered in a box.
|
|
240
|
-
* @param text - The text to check.
|
|
241
|
-
* @returns True if text is boxed.
|
|
242
|
-
*/
|
|
198
|
+
// Checks if text is wrapped in '^' markers for boxed text.
|
|
243
199
|
isBoxedText(text) {
|
|
244
200
|
return text.startsWith('^') && text.endsWith('^');
|
|
245
201
|
}
|
|
246
|
-
|
|
247
|
-
* Removes the '^' markers from the text.
|
|
248
|
-
* @param text - The boxed text.
|
|
249
|
-
* @returns The text without '^' markers.
|
|
250
|
-
*/
|
|
202
|
+
// Removes '^' markers.
|
|
251
203
|
stripBoxMarkers(text) {
|
|
252
204
|
return text.substring(1, text.length - 1);
|
|
253
205
|
}
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
* @param text - The text to check.
|
|
257
|
-
* @returns True if text is marked as bold.
|
|
258
|
-
*/
|
|
206
|
+
// Checks if text is entirely wrapped in '**' markers (not inline).
|
|
207
|
+
// (This function is kept for backward compatibility.)
|
|
259
208
|
isBoldText(text) {
|
|
260
209
|
return text.startsWith('**') && text.endsWith('**');
|
|
261
210
|
}
|
|
262
|
-
|
|
263
|
-
* Removes the '**' markers from bold text.
|
|
264
|
-
* @param text - The bold text.
|
|
265
|
-
* @returns The text without '**' markers.
|
|
266
|
-
*/
|
|
211
|
+
// Removes '**' markers.
|
|
267
212
|
stripBoldMarkers(text) {
|
|
268
213
|
return text.substring(2, text.length - 2);
|
|
269
214
|
}
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
* Uses the current cursor position for placement.
|
|
273
|
-
*/
|
|
274
|
-
inserisciImmagini() {
|
|
215
|
+
// Inserts images using the current cursor position.
|
|
216
|
+
insertLogo() {
|
|
275
217
|
return __awaiter(this, void 0, void 0, function* () {
|
|
276
218
|
var _a;
|
|
277
219
|
if (this.config.immagini && this.config.immagini.length > 0) {
|
|
@@ -291,17 +233,13 @@ class DocumentGenerator {
|
|
|
291
233
|
}
|
|
292
234
|
});
|
|
293
235
|
}
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
* Processes dynamic fields, images, tables, and text (with optional boxed or bold formatting).
|
|
297
|
-
* Returns the file name if saved or an ArrayBuffer.
|
|
298
|
-
* @param params - The DocumentParams object.
|
|
299
|
-
*/
|
|
236
|
+
// Generates the PDF document by processing images, dynamic fields, tables, and text.
|
|
237
|
+
// Supports inline bold formatting and boxed text.
|
|
300
238
|
generateDocument(params) {
|
|
301
239
|
return __awaiter(this, void 0, void 0, function* () {
|
|
302
240
|
yield this.loadConfig();
|
|
303
241
|
this.initDoc();
|
|
304
|
-
yield this.
|
|
242
|
+
yield this.insertLogo();
|
|
305
243
|
const dynamicFields = params.dynamicFields || {};
|
|
306
244
|
const dynamicElements = params.dynamicElements || {};
|
|
307
245
|
const pageWidth = this.doc.internal.pageSize.getWidth();
|
|
@@ -321,9 +259,10 @@ class DocumentGenerator {
|
|
|
321
259
|
this.curY += this.config.staccoriga;
|
|
322
260
|
for (const sub of punto.Sottopunti) {
|
|
323
261
|
const placeholder = this.extractPlaceholder(sub.titolo);
|
|
324
|
-
if (placeholder && dynamicElements[placeholder] && dynamicElements[placeholder].type === 'table') {
|
|
262
|
+
if (placeholder && dynamicElements[placeholder] && dynamicElements[placeholder].type === 'table' && dynamicElements[placeholder].config) {
|
|
325
263
|
const tableConfig = dynamicElements[placeholder].config;
|
|
326
|
-
|
|
264
|
+
const defaultTableOptions = this.config.tableStyle || {};
|
|
265
|
+
(0, jspdf_autotable_1.default)(this.doc, Object.assign(Object.assign(Object.assign({ startY: this.curY, head: [tableConfig.head], body: tableConfig.body }, defaultTableOptions), tableConfig.options), tableConfig.styles));
|
|
327
266
|
this.curY = this.doc.lastAutoTable.finalY + this.config.staccoriga;
|
|
328
267
|
}
|
|
329
268
|
else {
|
|
@@ -332,7 +271,7 @@ class DocumentGenerator {
|
|
|
332
271
|
titleText = this.applyPartiPlaceholders(titleText, params.parti);
|
|
333
272
|
if (this.isBoxedText(titleText)) {
|
|
334
273
|
const boxText = this.stripBoxMarkers(titleText);
|
|
335
|
-
yield this.
|
|
274
|
+
yield this.writeBoxedText(boxText, this.config.fontSottotitolo);
|
|
336
275
|
}
|
|
337
276
|
else if (this.isBoldText(titleText)) {
|
|
338
277
|
const boldText = this.stripBoldMarkers(titleText);
|
|
@@ -345,9 +284,10 @@ class DocumentGenerator {
|
|
|
345
284
|
}
|
|
346
285
|
}
|
|
347
286
|
const contentPlaceholder = this.extractPlaceholder(sub.contenuto);
|
|
348
|
-
if (contentPlaceholder && dynamicElements[contentPlaceholder] && dynamicElements[contentPlaceholder].type === 'table') {
|
|
287
|
+
if (contentPlaceholder && dynamicElements[contentPlaceholder] && dynamicElements[contentPlaceholder].type === 'table' && dynamicElements[contentPlaceholder].config) {
|
|
349
288
|
const tableConfig = dynamicElements[contentPlaceholder].config;
|
|
350
|
-
|
|
289
|
+
const defaultTableOptions = this.config.tableStyle || {};
|
|
290
|
+
(0, jspdf_autotable_1.default)(this.doc, Object.assign(Object.assign(Object.assign({ startY: this.curY, head: [tableConfig.head], body: tableConfig.body }, defaultTableOptions), tableConfig.options), tableConfig.styles));
|
|
351
291
|
this.curY = this.doc.lastAutoTable.finalY + this.config.staccoriga;
|
|
352
292
|
}
|
|
353
293
|
else if (sub.contenuto) {
|
|
@@ -355,7 +295,7 @@ class DocumentGenerator {
|
|
|
355
295
|
contenuto = this.applyPartiPlaceholders(contenuto, params.parti);
|
|
356
296
|
if (this.isBoxedText(contenuto)) {
|
|
357
297
|
const boxText = this.stripBoxMarkers(contenuto);
|
|
358
|
-
yield this.
|
|
298
|
+
yield this.writeBoxedText(boxText, this.config.fontTesto);
|
|
359
299
|
}
|
|
360
300
|
else if (this.isBoldText(contenuto)) {
|
|
361
301
|
const boldText = this.stripBoldMarkers(contenuto);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "emilsoftware-utilities",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.69",
|
|
4
4
|
"description": "Utilities for EmilSoftware",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.js",
|
|
@@ -27,8 +27,8 @@
|
|
|
27
27
|
"es-node-firebird": "^1.2.6",
|
|
28
28
|
"inversify": "^6.2.2",
|
|
29
29
|
"jsonwebtoken": "^9.0.2",
|
|
30
|
-
"jspdf": "^
|
|
31
|
-
"jspdf-autotable": "^
|
|
30
|
+
"jspdf": "^3.0.0",
|
|
31
|
+
"jspdf-autotable": "^5.0.2",
|
|
32
32
|
"node-fetch": "^3.3.2",
|
|
33
33
|
"nodemailer": "^6.10.0",
|
|
34
34
|
"reflect-metadata": "^0.2.2",
|