dynamic-docx-generator 1.0.0
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/LICENSE +21 -0
- package/README.md +315 -0
- package/dist/DocxGenerator.d.ts +135 -0
- package/dist/DocxGenerator.d.ts.map +1 -0
- package/dist/DocxGenerator.js +365 -0
- package/dist/DocxGenerator.js.map +1 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +62 -0
- package/dist/index.js.map +1 -0
- package/dist/types/index.d.ts +81 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +3 -0
- package/dist/types/index.js.map +1 -0
- package/dist/utils/constants.d.ts +44 -0
- package/dist/utils/constants.d.ts.map +1 -0
- package/dist/utils/constants.js +196 -0
- package/dist/utils/constants.js.map +1 -0
- package/dist/utils/image.d.ts +59 -0
- package/dist/utils/image.d.ts.map +1 -0
- package/dist/utils/image.js +158 -0
- package/dist/utils/image.js.map +1 -0
- package/dist/utils/string.d.ts +49 -0
- package/dist/utils/string.d.ts.map +1 -0
- package/dist/utils/string.js +143 -0
- package/dist/utils/string.js.map +1 -0
- package/dist/utils/xml.d.ts +48 -0
- package/dist/utils/xml.d.ts.map +1 -0
- package/dist/utils/xml.js +129 -0
- package/dist/utils/xml.js.map +1 -0
- package/package.json +57 -0
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* XML template constants for Office Open XML (OOXML) DOCX format
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.escapeXml = exports.generateTable = exports.generateTableDataRow = exports.generateTableHeaderRow = exports.DEFAULT_TABLE_STYLE = exports.generateInlineImage = exports.generateImageRelationship = void 0;
|
|
7
|
+
/**
|
|
8
|
+
* Generate XML for image relationship entry in .rels file
|
|
9
|
+
*/
|
|
10
|
+
const generateImageRelationship = (imageId, imageName) => {
|
|
11
|
+
return `<Relationship Id="${imageId}" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/${imageName}" />`;
|
|
12
|
+
};
|
|
13
|
+
exports.generateImageRelationship = generateImageRelationship;
|
|
14
|
+
/**
|
|
15
|
+
* Generate XML for inline image in document
|
|
16
|
+
*/
|
|
17
|
+
const generateInlineImage = (imageId, width = 914400, height = 914400, name = 'Picture') => {
|
|
18
|
+
return `<w:drawing>
|
|
19
|
+
<wp:inline distT="0" distB="0" distL="0" distR="0">
|
|
20
|
+
<wp:extent cx="${width}" cy="${height}" />
|
|
21
|
+
<wp:effectExtent l="0" t="0" r="0" b="0" />
|
|
22
|
+
<wp:docPr id="1" name="${name}" />
|
|
23
|
+
<wp:cNvGraphicFramePr>
|
|
24
|
+
<a:graphicFrameLocks xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" noChangeAspect="1" />
|
|
25
|
+
</wp:cNvGraphicFramePr>
|
|
26
|
+
<a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
|
|
27
|
+
<a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
|
|
28
|
+
<pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
|
|
29
|
+
<pic:nvPicPr>
|
|
30
|
+
<pic:cNvPr id="1" name="${name}" />
|
|
31
|
+
<pic:cNvPicPr />
|
|
32
|
+
</pic:nvPicPr>
|
|
33
|
+
<pic:blipFill>
|
|
34
|
+
<a:blip r:embed="${imageId}" cstate="print" />
|
|
35
|
+
<a:stretch>
|
|
36
|
+
<a:fillRect />
|
|
37
|
+
</a:stretch>
|
|
38
|
+
</pic:blipFill>
|
|
39
|
+
<pic:spPr>
|
|
40
|
+
<a:xfrm>
|
|
41
|
+
<a:off x="0" y="0" />
|
|
42
|
+
<a:ext cx="${width}" cy="${height}" />
|
|
43
|
+
</a:xfrm>
|
|
44
|
+
<a:prstGeom prst="rect">
|
|
45
|
+
<a:avLst />
|
|
46
|
+
</a:prstGeom>
|
|
47
|
+
</pic:spPr>
|
|
48
|
+
</pic:pic>
|
|
49
|
+
</a:graphicData>
|
|
50
|
+
</a:graphic>
|
|
51
|
+
</wp:inline>
|
|
52
|
+
</w:drawing>`;
|
|
53
|
+
};
|
|
54
|
+
exports.generateInlineImage = generateInlineImage;
|
|
55
|
+
/**
|
|
56
|
+
* Default table style values
|
|
57
|
+
*/
|
|
58
|
+
exports.DEFAULT_TABLE_STYLE = {
|
|
59
|
+
headerBgColor: 'C9DAF8',
|
|
60
|
+
headerTextColor: '000000',
|
|
61
|
+
borderColor: 'auto',
|
|
62
|
+
fontSize: 24, // 12pt
|
|
63
|
+
fontFamily: 'Times New Roman'
|
|
64
|
+
};
|
|
65
|
+
/**
|
|
66
|
+
* Generate table header row XML
|
|
67
|
+
*/
|
|
68
|
+
const generateTableHeaderRow = (headers, style = exports.DEFAULT_TABLE_STYLE) => {
|
|
69
|
+
const cells = headers.map((header, index) => `
|
|
70
|
+
<w:tc>
|
|
71
|
+
<w:tcPr>
|
|
72
|
+
<w:tcW w:w="${header.width || 2000}" w:type="dxa" />
|
|
73
|
+
<w:shd w:val="clear" w:color="auto" w:fill="${style.headerBgColor}" />
|
|
74
|
+
<w:vAlign w:val="center" />
|
|
75
|
+
</w:tcPr>
|
|
76
|
+
<w:p>
|
|
77
|
+
<w:pPr>
|
|
78
|
+
<w:jc w:val="center" />
|
|
79
|
+
<w:rPr>
|
|
80
|
+
<w:rFonts w:ascii="${style.fontFamily}" w:hAnsi="${style.fontFamily}" />
|
|
81
|
+
<w:b />
|
|
82
|
+
<w:sz w:val="${style.fontSize}" />
|
|
83
|
+
<w:szCs w:val="${style.fontSize}" />
|
|
84
|
+
<w:color w:val="${style.headerTextColor}" />
|
|
85
|
+
</w:rPr>
|
|
86
|
+
</w:pPr>
|
|
87
|
+
<w:r>
|
|
88
|
+
<w:rPr>
|
|
89
|
+
<w:rFonts w:ascii="${style.fontFamily}" w:hAnsi="${style.fontFamily}" />
|
|
90
|
+
<w:b />
|
|
91
|
+
<w:sz w:val="${style.fontSize}" />
|
|
92
|
+
<w:szCs w:val="${style.fontSize}" />
|
|
93
|
+
<w:color w:val="${style.headerTextColor}" />
|
|
94
|
+
</w:rPr>
|
|
95
|
+
<w:t>${(0, exports.escapeXml)(header.name)}</w:t>
|
|
96
|
+
</w:r>
|
|
97
|
+
</w:p>
|
|
98
|
+
</w:tc>
|
|
99
|
+
`).join('');
|
|
100
|
+
return `
|
|
101
|
+
<w:tr>
|
|
102
|
+
<w:trPr>
|
|
103
|
+
<w:trHeight w:val="400" />
|
|
104
|
+
</w:trPr>
|
|
105
|
+
${cells}
|
|
106
|
+
</w:tr>
|
|
107
|
+
`;
|
|
108
|
+
};
|
|
109
|
+
exports.generateTableHeaderRow = generateTableHeaderRow;
|
|
110
|
+
/**
|
|
111
|
+
* Generate a single table data row XML
|
|
112
|
+
*/
|
|
113
|
+
const generateTableDataRow = (cells, widths, style = exports.DEFAULT_TABLE_STYLE) => {
|
|
114
|
+
const cellsXml = cells.map((cell, index) => `
|
|
115
|
+
<w:tc>
|
|
116
|
+
<w:tcPr>
|
|
117
|
+
<w:tcW w:w="${widths[index] || 2000}" w:type="dxa" />
|
|
118
|
+
<w:vAlign w:val="center" />
|
|
119
|
+
</w:tcPr>
|
|
120
|
+
<w:p>
|
|
121
|
+
<w:pPr>
|
|
122
|
+
<w:rPr>
|
|
123
|
+
<w:rFonts w:ascii="${style.fontFamily}" w:hAnsi="${style.fontFamily}" />
|
|
124
|
+
<w:sz w:val="${style.fontSize}" />
|
|
125
|
+
<w:szCs w:val="${style.fontSize}" />
|
|
126
|
+
</w:rPr>
|
|
127
|
+
</w:pPr>
|
|
128
|
+
<w:r>
|
|
129
|
+
<w:rPr>
|
|
130
|
+
<w:rFonts w:ascii="${style.fontFamily}" w:hAnsi="${style.fontFamily}" />
|
|
131
|
+
<w:sz w:val="${style.fontSize}" />
|
|
132
|
+
<w:szCs w:val="${style.fontSize}" />
|
|
133
|
+
</w:rPr>
|
|
134
|
+
<w:t>${(0, exports.escapeXml)(cell)}</w:t>
|
|
135
|
+
</w:r>
|
|
136
|
+
</w:p>
|
|
137
|
+
</w:tc>
|
|
138
|
+
`).join('');
|
|
139
|
+
return `
|
|
140
|
+
<w:tr>
|
|
141
|
+
<w:trPr>
|
|
142
|
+
<w:trHeight w:val="350" />
|
|
143
|
+
</w:trPr>
|
|
144
|
+
${cellsXml}
|
|
145
|
+
</w:tr>
|
|
146
|
+
`;
|
|
147
|
+
};
|
|
148
|
+
exports.generateTableDataRow = generateTableDataRow;
|
|
149
|
+
/**
|
|
150
|
+
* Generate complete table XML
|
|
151
|
+
*/
|
|
152
|
+
const generateTable = (headers, rows, style = exports.DEFAULT_TABLE_STYLE) => {
|
|
153
|
+
const widths = headers.map(h => h.width || 2000);
|
|
154
|
+
const gridCols = headers.map(h => `<w:gridCol w:w="${h.width || 2000}" />`).join('');
|
|
155
|
+
const headerRow = (0, exports.generateTableHeaderRow)(headers, style);
|
|
156
|
+
const dataRows = rows.map(row => (0, exports.generateTableDataRow)(row, widths, style)).join('');
|
|
157
|
+
return `
|
|
158
|
+
<w:tbl>
|
|
159
|
+
<w:tblPr>
|
|
160
|
+
<w:tblStyle w:val="TableGrid" />
|
|
161
|
+
<w:tblW w:w="5000" w:type="pct" />
|
|
162
|
+
<w:jc w:val="center" />
|
|
163
|
+
<w:tblBorders>
|
|
164
|
+
<w:top w:val="single" w:sz="4" w:space="0" w:color="${style.borderColor}" />
|
|
165
|
+
<w:left w:val="single" w:sz="4" w:space="0" w:color="${style.borderColor}" />
|
|
166
|
+
<w:bottom w:val="single" w:sz="4" w:space="0" w:color="${style.borderColor}" />
|
|
167
|
+
<w:right w:val="single" w:sz="4" w:space="0" w:color="${style.borderColor}" />
|
|
168
|
+
<w:insideH w:val="single" w:sz="4" w:space="0" w:color="${style.borderColor}" />
|
|
169
|
+
<w:insideV w:val="single" w:sz="4" w:space="0" w:color="${style.borderColor}" />
|
|
170
|
+
</w:tblBorders>
|
|
171
|
+
<w:tblLayout w:type="fixed" />
|
|
172
|
+
</w:tblPr>
|
|
173
|
+
<w:tblGrid>
|
|
174
|
+
${gridCols}
|
|
175
|
+
</w:tblGrid>
|
|
176
|
+
${headerRow}
|
|
177
|
+
${dataRows}
|
|
178
|
+
</w:tbl>
|
|
179
|
+
`;
|
|
180
|
+
};
|
|
181
|
+
exports.generateTable = generateTable;
|
|
182
|
+
/**
|
|
183
|
+
* Escape special XML characters
|
|
184
|
+
*/
|
|
185
|
+
const escapeXml = (text) => {
|
|
186
|
+
if (!text)
|
|
187
|
+
return '';
|
|
188
|
+
return text
|
|
189
|
+
.replace(/&/g, '&')
|
|
190
|
+
.replace(/</g, '<')
|
|
191
|
+
.replace(/>/g, '>')
|
|
192
|
+
.replace(/"/g, '"')
|
|
193
|
+
.replace(/'/g, ''');
|
|
194
|
+
};
|
|
195
|
+
exports.escapeXml = escapeXml;
|
|
196
|
+
//# sourceMappingURL=constants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/utils/constants.ts"],"names":[],"mappings":";AAAA;;GAEG;;;AAEH;;GAEG;AACI,MAAM,yBAAyB,GAAG,CAAC,OAAe,EAAE,SAAiB,EAAU,EAAE;IACpF,OAAO,qBAAqB,OAAO,oGAAoG,SAAS,MAAM,CAAC;AAC3J,CAAC,CAAC;AAFW,QAAA,yBAAyB,6BAEpC;AAEF;;GAEG;AACI,MAAM,mBAAmB,GAAG,CAC/B,OAAe,EACf,QAAgB,MAAM,EACtB,SAAiB,MAAM,EACvB,OAAe,SAAS,EAClB,EAAE;IACR,OAAO;;uBAEY,KAAK,SAAS,MAAM;;+BAEZ,IAAI;;;;;;;;wCAQK,IAAI;;;;iCAIX,OAAO;;;;;;;;6BAQX,KAAK,SAAS,MAAM;;;;;;;;;;eAUlC,CAAC;AAChB,CAAC,CAAC;AAzCW,QAAA,mBAAmB,uBAyC9B;AAEF;;GAEG;AAC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,EAAE;IAC9C,IAAI,CAAC,IAAI;QAAE,OAAO,EAAE,CAAC;IACrB,OAAO,IAAI;SACN,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC;SACtB,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC;SACrB,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC;SACrB,OAAO,CAAC,IAAI,EAAE,QAAQ,CAAC;SACvB,OAAO,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;AACjC,CAAC,CAAC;AARW,QAAA,SAAS,aAQpB"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Image processing utilities for DOCX
|
|
3
|
+
*/
|
|
4
|
+
import { ImageConfig } from '../types';
|
|
5
|
+
/**
|
|
6
|
+
* Load image from file path
|
|
7
|
+
*/
|
|
8
|
+
export declare const loadImageFromPath: (imagePath: string) => Promise<Buffer>;
|
|
9
|
+
/**
|
|
10
|
+
* Download image from URL
|
|
11
|
+
*/
|
|
12
|
+
export declare const downloadImage: (url: string) => Promise<Buffer>;
|
|
13
|
+
/**
|
|
14
|
+
* Get image buffer from ImageConfig
|
|
15
|
+
*/
|
|
16
|
+
export declare const getImageBuffer: (config: ImageConfig) => Promise<Buffer>;
|
|
17
|
+
/**
|
|
18
|
+
* Get image extension from path or URL
|
|
19
|
+
*/
|
|
20
|
+
export declare const getImageExtension: (config: ImageConfig) => string;
|
|
21
|
+
/**
|
|
22
|
+
* Generate image file name for media folder
|
|
23
|
+
*/
|
|
24
|
+
export declare const generateImageFileName: (index: number, extension: string) => string;
|
|
25
|
+
/**
|
|
26
|
+
* Prepare image for insertion into DOCX
|
|
27
|
+
*/
|
|
28
|
+
export interface PreparedImage {
|
|
29
|
+
id: string;
|
|
30
|
+
buffer: Buffer;
|
|
31
|
+
fileName: string;
|
|
32
|
+
extension: string;
|
|
33
|
+
relationshipXml: string;
|
|
34
|
+
inlineXml: string;
|
|
35
|
+
placeholder: string;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Prepare an image config for insertion
|
|
39
|
+
*/
|
|
40
|
+
export declare const prepareImage: (config: ImageConfig, index: number, existingIds: string[]) => Promise<PreparedImage>;
|
|
41
|
+
/**
|
|
42
|
+
* Default image dimensions in EMUs (914400 EMUs = 1 inch)
|
|
43
|
+
*/
|
|
44
|
+
export declare const EMU_PER_INCH = 914400;
|
|
45
|
+
export declare const EMU_PER_CM = 360000;
|
|
46
|
+
export declare const EMU_PER_PIXEL = 9525;
|
|
47
|
+
/**
|
|
48
|
+
* Convert pixels to EMUs
|
|
49
|
+
*/
|
|
50
|
+
export declare const pixelsToEmu: (pixels: number) => number;
|
|
51
|
+
/**
|
|
52
|
+
* Convert inches to EMUs
|
|
53
|
+
*/
|
|
54
|
+
export declare const inchesToEmu: (inches: number) => number;
|
|
55
|
+
/**
|
|
56
|
+
* Convert centimeters to EMUs
|
|
57
|
+
*/
|
|
58
|
+
export declare const cmToEmu: (cm: number) => number;
|
|
59
|
+
//# sourceMappingURL=image.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"image.d.ts","sourceRoot":"","sources":["../../src/utils/image.ts"],"names":[],"mappings":"AAAA;;GAEG;AAKH,OAAO,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAIvC;;GAEG;AACH,eAAO,MAAM,iBAAiB,GAAU,WAAW,MAAM,KAAG,OAAO,CAAC,MAAM,CAEzE,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,aAAa,GAAU,KAAK,MAAM,KAAG,OAAO,CAAC,MAAM,CAO/D,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,cAAc,GAAU,QAAQ,WAAW,KAAG,OAAO,CAAC,MAAM,CAcxE,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,iBAAiB,GAAI,QAAQ,WAAW,KAAG,MAWvD,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,qBAAqB,GAAI,OAAO,MAAM,EAAE,WAAW,MAAM,KAAG,MAExE,CAAC;AAEF;;GAEG;AACH,MAAM,WAAW,aAAa;IAC1B,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,MAAM,CAAC;IACjB,SAAS,EAAE,MAAM,CAAC;IAClB,eAAe,EAAE,MAAM,CAAC;IACxB,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,MAAM,CAAC;CACvB;AAED;;GAEG;AACH,eAAO,MAAM,YAAY,GACrB,QAAQ,WAAW,EACnB,OAAO,MAAM,EACb,aAAa,MAAM,EAAE,KACtB,OAAO,CAAC,aAAa,CA8BvB,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,YAAY,SAAS,CAAC;AACnC,eAAO,MAAM,UAAU,SAAS,CAAC;AACjC,eAAO,MAAM,aAAa,OAAO,CAAC;AAElC;;GAEG;AACH,eAAO,MAAM,WAAW,GAAI,QAAQ,MAAM,KAAG,MAE5C,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,WAAW,GAAI,QAAQ,MAAM,KAAG,MAE5C,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,OAAO,GAAI,IAAI,MAAM,KAAG,MAEpC,CAAC"}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Image processing utilities for DOCX
|
|
4
|
+
*/
|
|
5
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
8
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
9
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
10
|
+
}
|
|
11
|
+
Object.defineProperty(o, k2, desc);
|
|
12
|
+
}) : (function(o, m, k, k2) {
|
|
13
|
+
if (k2 === undefined) k2 = k;
|
|
14
|
+
o[k2] = m[k];
|
|
15
|
+
}));
|
|
16
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
17
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
18
|
+
}) : function(o, v) {
|
|
19
|
+
o["default"] = v;
|
|
20
|
+
});
|
|
21
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
22
|
+
var ownKeys = function(o) {
|
|
23
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
24
|
+
var ar = [];
|
|
25
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
26
|
+
return ar;
|
|
27
|
+
};
|
|
28
|
+
return ownKeys(o);
|
|
29
|
+
};
|
|
30
|
+
return function (mod) {
|
|
31
|
+
if (mod && mod.__esModule) return mod;
|
|
32
|
+
var result = {};
|
|
33
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
34
|
+
__setModuleDefault(result, mod);
|
|
35
|
+
return result;
|
|
36
|
+
};
|
|
37
|
+
})();
|
|
38
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
|
+
};
|
|
41
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
+
exports.cmToEmu = exports.inchesToEmu = exports.pixelsToEmu = exports.EMU_PER_PIXEL = exports.EMU_PER_CM = exports.EMU_PER_INCH = exports.prepareImage = exports.generateImageFileName = exports.getImageExtension = exports.getImageBuffer = exports.downloadImage = exports.loadImageFromPath = void 0;
|
|
43
|
+
const fs = __importStar(require("fs"));
|
|
44
|
+
const path = __importStar(require("path"));
|
|
45
|
+
const axios_1 = __importDefault(require("axios"));
|
|
46
|
+
const string_1 = require("./string");
|
|
47
|
+
const constants_1 = require("./constants");
|
|
48
|
+
/**
|
|
49
|
+
* Load image from file path
|
|
50
|
+
*/
|
|
51
|
+
const loadImageFromPath = async (imagePath) => {
|
|
52
|
+
return fs.promises.readFile(imagePath);
|
|
53
|
+
};
|
|
54
|
+
exports.loadImageFromPath = loadImageFromPath;
|
|
55
|
+
/**
|
|
56
|
+
* Download image from URL
|
|
57
|
+
*/
|
|
58
|
+
const downloadImage = async (url) => {
|
|
59
|
+
const response = await (0, axios_1.default)({
|
|
60
|
+
method: 'GET',
|
|
61
|
+
url: url,
|
|
62
|
+
responseType: 'arraybuffer'
|
|
63
|
+
});
|
|
64
|
+
return Buffer.from(response.data);
|
|
65
|
+
};
|
|
66
|
+
exports.downloadImage = downloadImage;
|
|
67
|
+
/**
|
|
68
|
+
* Get image buffer from ImageConfig
|
|
69
|
+
*/
|
|
70
|
+
const getImageBuffer = async (config) => {
|
|
71
|
+
if (config.buffer) {
|
|
72
|
+
return config.buffer;
|
|
73
|
+
}
|
|
74
|
+
if (config.path) {
|
|
75
|
+
return (0, exports.loadImageFromPath)(config.path);
|
|
76
|
+
}
|
|
77
|
+
if (config.url) {
|
|
78
|
+
return (0, exports.downloadImage)(config.url);
|
|
79
|
+
}
|
|
80
|
+
throw new Error(`No image source provided for placeholder: ${config.placeholder}`);
|
|
81
|
+
};
|
|
82
|
+
exports.getImageBuffer = getImageBuffer;
|
|
83
|
+
/**
|
|
84
|
+
* Get image extension from path or URL
|
|
85
|
+
*/
|
|
86
|
+
const getImageExtension = (config) => {
|
|
87
|
+
if (config.path) {
|
|
88
|
+
return path.extname(config.path).toLowerCase().replace('.', '') || 'png';
|
|
89
|
+
}
|
|
90
|
+
if (config.url) {
|
|
91
|
+
const urlPath = new URL(config.url).pathname;
|
|
92
|
+
return path.extname(urlPath).toLowerCase().replace('.', '') || 'png';
|
|
93
|
+
}
|
|
94
|
+
return 'png';
|
|
95
|
+
};
|
|
96
|
+
exports.getImageExtension = getImageExtension;
|
|
97
|
+
/**
|
|
98
|
+
* Generate image file name for media folder
|
|
99
|
+
*/
|
|
100
|
+
const generateImageFileName = (index, extension) => {
|
|
101
|
+
return `image${index + 1}.${extension}`;
|
|
102
|
+
};
|
|
103
|
+
exports.generateImageFileName = generateImageFileName;
|
|
104
|
+
/**
|
|
105
|
+
* Prepare an image config for insertion
|
|
106
|
+
*/
|
|
107
|
+
const prepareImage = async (config, index, existingIds) => {
|
|
108
|
+
const buffer = await (0, exports.getImageBuffer)(config);
|
|
109
|
+
const extension = (0, exports.getImageExtension)(config);
|
|
110
|
+
const fileName = (0, exports.generateImageFileName)(index, extension);
|
|
111
|
+
// Generate unique ID
|
|
112
|
+
let id = config.id || (0, string_1.generateImageId)();
|
|
113
|
+
let counter = 1;
|
|
114
|
+
while (existingIds.includes(id)) {
|
|
115
|
+
id = `rId${100 + index + counter}`;
|
|
116
|
+
counter++;
|
|
117
|
+
}
|
|
118
|
+
const relationshipXml = (0, constants_1.generateImageRelationship)(id, fileName);
|
|
119
|
+
const inlineXml = (0, constants_1.generateInlineImage)(id, config.width || 914400, config.height || config.width || 914400, `Image ${index + 1}`);
|
|
120
|
+
return {
|
|
121
|
+
id,
|
|
122
|
+
buffer,
|
|
123
|
+
fileName,
|
|
124
|
+
extension,
|
|
125
|
+
relationshipXml,
|
|
126
|
+
inlineXml,
|
|
127
|
+
placeholder: config.placeholder
|
|
128
|
+
};
|
|
129
|
+
};
|
|
130
|
+
exports.prepareImage = prepareImage;
|
|
131
|
+
/**
|
|
132
|
+
* Default image dimensions in EMUs (914400 EMUs = 1 inch)
|
|
133
|
+
*/
|
|
134
|
+
exports.EMU_PER_INCH = 914400;
|
|
135
|
+
exports.EMU_PER_CM = 360000;
|
|
136
|
+
exports.EMU_PER_PIXEL = 9525; // At 96 DPI
|
|
137
|
+
/**
|
|
138
|
+
* Convert pixels to EMUs
|
|
139
|
+
*/
|
|
140
|
+
const pixelsToEmu = (pixels) => {
|
|
141
|
+
return Math.round(pixels * exports.EMU_PER_PIXEL);
|
|
142
|
+
};
|
|
143
|
+
exports.pixelsToEmu = pixelsToEmu;
|
|
144
|
+
/**
|
|
145
|
+
* Convert inches to EMUs
|
|
146
|
+
*/
|
|
147
|
+
const inchesToEmu = (inches) => {
|
|
148
|
+
return Math.round(inches * exports.EMU_PER_INCH);
|
|
149
|
+
};
|
|
150
|
+
exports.inchesToEmu = inchesToEmu;
|
|
151
|
+
/**
|
|
152
|
+
* Convert centimeters to EMUs
|
|
153
|
+
*/
|
|
154
|
+
const cmToEmu = (cm) => {
|
|
155
|
+
return Math.round(cm * exports.EMU_PER_CM);
|
|
156
|
+
};
|
|
157
|
+
exports.cmToEmu = cmToEmu;
|
|
158
|
+
//# sourceMappingURL=image.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"image.js","sourceRoot":"","sources":["../../src/utils/image.ts"],"names":[],"mappings":";AAAA;;GAEG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEH,uCAAyB;AACzB,2CAA6B;AAC7B,kDAA0B;AAE1B,qCAA2C;AAC3C,2CAA6E;AAE7E;;GAEG;AACI,MAAM,iBAAiB,GAAG,KAAK,EAAE,SAAiB,EAAmB,EAAE;IAC1E,OAAO,EAAE,CAAC,QAAQ,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC;AAC3C,CAAC,CAAC;AAFW,QAAA,iBAAiB,qBAE5B;AAEF;;GAEG;AACI,MAAM,aAAa,GAAG,KAAK,EAAE,GAAW,EAAmB,EAAE;IAChE,MAAM,QAAQ,GAAG,MAAM,IAAA,eAAK,EAAC;QACzB,MAAM,EAAE,KAAK;QACb,GAAG,EAAE,GAAG;QACR,YAAY,EAAE,aAAa;KAC9B,CAAC,CAAC;IACH,OAAO,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;AACtC,CAAC,CAAC;AAPW,QAAA,aAAa,iBAOxB;AAEF;;GAEG;AACI,MAAM,cAAc,GAAG,KAAK,EAAE,MAAmB,EAAmB,EAAE;IACzE,IAAI,MAAM,CAAC,MAAM,EAAE,CAAC;QAChB,OAAO,MAAM,CAAC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AAA,WAAW,eAEtB;AAEF;;GAEG;AACI,MAAM,WAAW,GAAG,CAAC,MAAc,EAAU,EAAE;IAClD,OAAO,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,oBAAY,CAAC,CAAC;AAC7C,CAAC,CAAC;AAFW,QAAA,WAAW,eAEtB;AAEF;;GAEG;AACI,MAAM,OAAO,GAAG,CAAC,EAAU,EAAU,EAAE;IAC1C,OAAO,IAAI,CAAC,KAAK,CAAC,EAAE,GAAG,kBAAU,CAAC,CAAC;AACvC,CAAC,CAAC;AAFW,QAAA,OAAO,WAElB"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* String manipulation utilities for DOCX template processing
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Escape special characters for use in regular expressions
|
|
6
|
+
*/
|
|
7
|
+
export declare const escapeRegExp: (string: string) => string;
|
|
8
|
+
/**
|
|
9
|
+
* Replace all occurrences of a string (case-sensitive)
|
|
10
|
+
*/
|
|
11
|
+
export declare const replaceAll: (str: string, search: string, replacement: string) => string;
|
|
12
|
+
/**
|
|
13
|
+
* Replace placeholder with value, handling XML-safe escaping
|
|
14
|
+
* Supports both {{placeholder}} and <w:t>placeholder</w:t> formats
|
|
15
|
+
*/
|
|
16
|
+
export declare const replacePlaceholder: (content: string, placeholder: string, value: string) => string;
|
|
17
|
+
/**
|
|
18
|
+
* Escape special characters for XML content
|
|
19
|
+
*/
|
|
20
|
+
export declare const escapeForXml: (text: string) => string;
|
|
21
|
+
/**
|
|
22
|
+
* Unescape XML entities back to regular characters
|
|
23
|
+
*/
|
|
24
|
+
export declare const unescapeXml: (text: string) => string;
|
|
25
|
+
/**
|
|
26
|
+
* Fix double-escaped XML entities
|
|
27
|
+
*/
|
|
28
|
+
export declare const fixDoubleEscaping: (content: string) => string;
|
|
29
|
+
/**
|
|
30
|
+
* Strip HTML tags and return plain text
|
|
31
|
+
*/
|
|
32
|
+
export declare const stripHtml: (html: string) => string;
|
|
33
|
+
/**
|
|
34
|
+
* Generate a unique ID for image relationships
|
|
35
|
+
*/
|
|
36
|
+
export declare const generateImageId: () => string;
|
|
37
|
+
/**
|
|
38
|
+
* Check if a string contains any of the given placeholders
|
|
39
|
+
*/
|
|
40
|
+
export declare const containsPlaceholder: (content: string, placeholders: string[]) => boolean;
|
|
41
|
+
/**
|
|
42
|
+
* Extract all placeholders from content (both {{}} and raw formats)
|
|
43
|
+
*/
|
|
44
|
+
export declare const extractPlaceholders: (content: string) => string[];
|
|
45
|
+
/**
|
|
46
|
+
* Wrap text with XML preserve space attribute for proper whitespace handling
|
|
47
|
+
*/
|
|
48
|
+
export declare const wrapWithPreserveSpace: (text: string) => string;
|
|
49
|
+
//# sourceMappingURL=string.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"string.d.ts","sourceRoot":"","sources":["../../src/utils/string.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;GAEG;AACH,eAAO,MAAM,YAAY,GAAI,QAAQ,MAAM,KAAG,MAE7C,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,UAAU,GAAI,KAAK,MAAM,EAAE,QAAQ,MAAM,EAAE,aAAa,MAAM,KAAG,MAK7E,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,kBAAkB,GAC3B,SAAS,MAAM,EACf,aAAa,MAAM,EACnB,OAAO,MAAM,KACd,MAmBF,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,YAAY,GAAI,MAAM,MAAM,KAAG,MAS3C,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,WAAW,GAAI,MAAM,MAAM,KAAG,MAS1C,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,iBAAiB,GAAI,SAAS,MAAM,KAAG,MAQnD,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,SAAS,GAAI,MAAM,MAAM,KAAG,MAQxC,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,eAAe,QAAO,MAalC,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,mBAAmB,GAAI,SAAS,MAAM,EAAE,cAAc,MAAM,EAAE,KAAG,OAI7E,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,mBAAmB,GAAI,SAAS,MAAM,KAAG,MAAM,EAY3D,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,qBAAqB,GAAI,MAAM,MAAM,KAAG,MAEpD,CAAC"}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* String manipulation utilities for DOCX template processing
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.wrapWithPreserveSpace = exports.extractPlaceholders = exports.containsPlaceholder = exports.generateImageId = exports.stripHtml = exports.fixDoubleEscaping = exports.unescapeXml = exports.escapeForXml = exports.replacePlaceholder = exports.replaceAll = exports.escapeRegExp = void 0;
|
|
7
|
+
/**
|
|
8
|
+
* Escape special characters for use in regular expressions
|
|
9
|
+
*/
|
|
10
|
+
const escapeRegExp = (string) => {
|
|
11
|
+
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
12
|
+
};
|
|
13
|
+
exports.escapeRegExp = escapeRegExp;
|
|
14
|
+
/**
|
|
15
|
+
* Replace all occurrences of a string (case-sensitive)
|
|
16
|
+
*/
|
|
17
|
+
const replaceAll = (str, search, replacement) => {
|
|
18
|
+
if (typeof str !== 'string') {
|
|
19
|
+
return str;
|
|
20
|
+
}
|
|
21
|
+
return str.replace(new RegExp((0, exports.escapeRegExp)(search), 'g'), replacement);
|
|
22
|
+
};
|
|
23
|
+
exports.replaceAll = replaceAll;
|
|
24
|
+
/**
|
|
25
|
+
* Replace placeholder with value, handling XML-safe escaping
|
|
26
|
+
* Supports both {{placeholder}} and <w:t>placeholder</w:t> formats
|
|
27
|
+
*/
|
|
28
|
+
const replacePlaceholder = (content, placeholder, value) => {
|
|
29
|
+
let result = content;
|
|
30
|
+
// Escape the value for XML
|
|
31
|
+
const safeValue = (0, exports.escapeForXml)(value);
|
|
32
|
+
// Replace {{placeholder}} format
|
|
33
|
+
result = (0, exports.replaceAll)(result, `{{${placeholder}}}`, safeValue);
|
|
34
|
+
// Replace <w:t>placeholder</w:t> format (Word XML)
|
|
35
|
+
result = (0, exports.replaceAll)(result, `<w:t>${placeholder}</w:t>`, `<w:t>${safeValue}</w:t>`);
|
|
36
|
+
// Replace when placeholder is split across XML tags (common in Word)
|
|
37
|
+
// Pattern: <w:t>place</w:t></w:r><w:r><w:t>holder</w:t>
|
|
38
|
+
const placeholderPattern = placeholder.split('').join('[^a-zA-Z]*');
|
|
39
|
+
const regex = new RegExp(`<w:t[^>]*>\\s*${placeholderPattern}\\s*</w:t>`, 'gi');
|
|
40
|
+
result = result.replace(regex, `<w:t>${safeValue}</w:t>`);
|
|
41
|
+
return result;
|
|
42
|
+
};
|
|
43
|
+
exports.replacePlaceholder = replacePlaceholder;
|
|
44
|
+
/**
|
|
45
|
+
* Escape special characters for XML content
|
|
46
|
+
*/
|
|
47
|
+
const escapeForXml = (text) => {
|
|
48
|
+
if (!text || typeof text !== 'string')
|
|
49
|
+
return '';
|
|
50
|
+
return text
|
|
51
|
+
.replace(/&/g, '&')
|
|
52
|
+
.replace(/</g, '<')
|
|
53
|
+
.replace(/>/g, '>')
|
|
54
|
+
.replace(/"/g, '"')
|
|
55
|
+
.replace(/'/g, ''');
|
|
56
|
+
};
|
|
57
|
+
exports.escapeForXml = escapeForXml;
|
|
58
|
+
/**
|
|
59
|
+
* Unescape XML entities back to regular characters
|
|
60
|
+
*/
|
|
61
|
+
const unescapeXml = (text) => {
|
|
62
|
+
if (!text || typeof text !== 'string')
|
|
63
|
+
return '';
|
|
64
|
+
return text
|
|
65
|
+
.replace(/&/g, '&')
|
|
66
|
+
.replace(/</g, '<')
|
|
67
|
+
.replace(/>/g, '>')
|
|
68
|
+
.replace(/"/g, '"')
|
|
69
|
+
.replace(/'/g, "'");
|
|
70
|
+
};
|
|
71
|
+
exports.unescapeXml = unescapeXml;
|
|
72
|
+
/**
|
|
73
|
+
* Fix double-escaped XML entities
|
|
74
|
+
*/
|
|
75
|
+
const fixDoubleEscaping = (content) => {
|
|
76
|
+
let result = content;
|
|
77
|
+
result = (0, exports.replaceAll)(result, '&amp;', '&');
|
|
78
|
+
result = (0, exports.replaceAll)(result, '&lt;', '<');
|
|
79
|
+
result = (0, exports.replaceAll)(result, '&gt;', '>');
|
|
80
|
+
result = (0, exports.replaceAll)(result, '&quot;', '"');
|
|
81
|
+
result = (0, exports.replaceAll)(result, '&apos;', ''');
|
|
82
|
+
return result;
|
|
83
|
+
};
|
|
84
|
+
exports.fixDoubleEscaping = fixDoubleEscaping;
|
|
85
|
+
/**
|
|
86
|
+
* Strip HTML tags and return plain text
|
|
87
|
+
*/
|
|
88
|
+
const stripHtml = (html) => {
|
|
89
|
+
if (!html || typeof html !== 'string')
|
|
90
|
+
return '';
|
|
91
|
+
return html
|
|
92
|
+
.replace(/<[^>]*>/g, '')
|
|
93
|
+
.replace(/ /g, ' ')
|
|
94
|
+
.replace(/\s+/g, ' ')
|
|
95
|
+
.trim();
|
|
96
|
+
};
|
|
97
|
+
exports.stripHtml = stripHtml;
|
|
98
|
+
/**
|
|
99
|
+
* Generate a unique ID for image relationships
|
|
100
|
+
*/
|
|
101
|
+
const generateImageId = () => {
|
|
102
|
+
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
103
|
+
const nums = '0123456789';
|
|
104
|
+
let id = 'rId';
|
|
105
|
+
for (let i = 0; i < 2; i++) {
|
|
106
|
+
id += chars.charAt(Math.floor(Math.random() * chars.length));
|
|
107
|
+
}
|
|
108
|
+
for (let i = 0; i < 4; i++) {
|
|
109
|
+
id += nums.charAt(Math.floor(Math.random() * nums.length));
|
|
110
|
+
}
|
|
111
|
+
return id;
|
|
112
|
+
};
|
|
113
|
+
exports.generateImageId = generateImageId;
|
|
114
|
+
/**
|
|
115
|
+
* Check if a string contains any of the given placeholders
|
|
116
|
+
*/
|
|
117
|
+
const containsPlaceholder = (content, placeholders) => {
|
|
118
|
+
return placeholders.some(p => content.includes(`{{${p}}}`) || content.includes(`<w:t>${p}</w:t>`));
|
|
119
|
+
};
|
|
120
|
+
exports.containsPlaceholder = containsPlaceholder;
|
|
121
|
+
/**
|
|
122
|
+
* Extract all placeholders from content (both {{}} and raw formats)
|
|
123
|
+
*/
|
|
124
|
+
const extractPlaceholders = (content) => {
|
|
125
|
+
const placeholders = new Set();
|
|
126
|
+
// Match {{placeholder}} format
|
|
127
|
+
const bracketMatches = content.match(/\{\{([^}]+)\}\}/g);
|
|
128
|
+
if (bracketMatches) {
|
|
129
|
+
bracketMatches.forEach(match => {
|
|
130
|
+
placeholders.add(match.slice(2, -2));
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
return Array.from(placeholders);
|
|
134
|
+
};
|
|
135
|
+
exports.extractPlaceholders = extractPlaceholders;
|
|
136
|
+
/**
|
|
137
|
+
* Wrap text with XML preserve space attribute for proper whitespace handling
|
|
138
|
+
*/
|
|
139
|
+
const wrapWithPreserveSpace = (text) => {
|
|
140
|
+
return `<w:t xml:space="preserve">${(0, exports.escapeForXml)(text)}</w:t>`;
|
|
141
|
+
};
|
|
142
|
+
exports.wrapWithPreserveSpace = wrapWithPreserveSpace;
|
|
143
|
+
//# sourceMappingURL=string.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"string.js","sourceRoot":"","sources":["../../src/utils/string.ts"],"names":[],"mappings":";AAAA;;GAEG;;;AAEH;;GAEG;AACI,MAAM,YAAY,GAAG,CAAC,MAAc,EAAU,EAAE;IACnD,OAAO,MAAM,CAAC,OAAO,CAAC,qBAAqB,EAAE,MAAM,CAAC,CAAC;AACzD,CAAC,CAAC;AAFW,QAAA,YAAY,gBAEvB;AAEF;;GAEG;AACI,MAAM,UAAU,GAAG,CAAC,GAAW,EAAE,MAAc,EAAE,WAAmB,EAAU,EAAE;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