abstract-document 19.1.25 → 19.2.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/CHANGELOG.md +11 -0
- package/lib/abstract-document-exporters/docx2/render-image.d.ts.map +1 -1
- package/lib/abstract-document-exporters/docx2/render-image.js +222 -143
- package/lib/abstract-document-exporters/docx2/render-image.js.map +1 -1
- package/lib/abstract-document-exporters/docx2/render.d.ts.map +1 -1
- package/lib/abstract-document-exporters/docx2/render.js +65 -33
- package/lib/abstract-document-exporters/docx2/render.js.map +1 -1
- package/package.json +3 -3
- package/src/abstract-document-exporters/docx2/render-image.ts +331 -161
- package/src/abstract-document-exporters/docx2/render.ts +75 -33
|
@@ -34,6 +34,7 @@ import { Readable } from "stream";
|
|
|
34
34
|
|
|
35
35
|
const abstractDocToDocxFontRatio = 2;
|
|
36
36
|
const abstractDocPixelToDocxDXARatio = 20;
|
|
37
|
+
const abstractDocBorderToDocxBorderSizeRatio = 8;
|
|
37
38
|
|
|
38
39
|
export function exportToHTML5Blob(doc: AD.AbstractDoc.AbstractDoc): Promise<Blob> {
|
|
39
40
|
return new Promise((resolve) => {
|
|
@@ -72,8 +73,13 @@ function createDocument(doc: AD.AbstractDoc.AbstractDoc): Document {
|
|
|
72
73
|
function renderSection(section: AD.Section.Section, parentResources: AD.Resources.Resources): ISectionOptions {
|
|
73
74
|
const pageWidth = AD.PageStyle.getWidth(section.page.style);
|
|
74
75
|
const pageHeight = AD.PageStyle.getHeight(section.page.style);
|
|
76
|
+
const pageHeaderMargins = AD.LayoutFoundation.orDefault(section.page.style.headerMargins)
|
|
77
|
+
const pageFooterMargins = AD.LayoutFoundation.orDefault(section.page.style.footerMargins)
|
|
75
78
|
const pageContentMargins = AD.LayoutFoundation.orDefault(section.page.style.contentMargins);
|
|
76
79
|
|
|
80
|
+
const hasFrontHeader = section.page.frontHeader !== undefined && section.page.frontHeader.length !== 0;
|
|
81
|
+
const hasFrontFooter = section.page.frontFooter !== undefined && section.page.frontFooter.length !== 0;
|
|
82
|
+
|
|
77
83
|
const contentAvailableWidth =
|
|
78
84
|
pageWidth - (pageContentMargins.left + pageContentMargins.right);
|
|
79
85
|
|
|
@@ -84,12 +90,20 @@ function renderSection(section: AD.Section.Section, parentResources: AD.Resource
|
|
|
84
90
|
return sofar;
|
|
85
91
|
}, [] as Array<Paragraph | Table>);
|
|
86
92
|
|
|
87
|
-
const
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
+
const firstpageHeaderChildren = hasFrontHeader ? section.page.frontHeader.reduce((sofar, c) => {
|
|
94
|
+
sofar.push(...renderSectionElement(c, resources, contentAvailableWidth));
|
|
95
|
+
return sofar;
|
|
96
|
+
}, [] as Array<Paragraph | Table>) : [];
|
|
97
|
+
|
|
98
|
+
const footerChildren = section.page.footer.reduce((sofar, c) => {
|
|
99
|
+
sofar.push(...renderSectionElement(c, resources, contentAvailableWidth));
|
|
100
|
+
return sofar;
|
|
101
|
+
}, [] as Array<Paragraph | Table>);
|
|
102
|
+
|
|
103
|
+
const firstPageFooterChildren = hasFrontFooter ? section.page.frontFooter.reduce((sofar, c) => {
|
|
104
|
+
sofar.push(...renderSectionElement(c, resources, contentAvailableWidth));
|
|
105
|
+
return sofar;
|
|
106
|
+
}, [] as Array<Paragraph | Table>) : [];
|
|
93
107
|
|
|
94
108
|
const contentChildren = [
|
|
95
109
|
new Paragraph({
|
|
@@ -109,6 +123,7 @@ function renderSection(section: AD.Section.Section, parentResources: AD.Resource
|
|
|
109
123
|
|
|
110
124
|
return {
|
|
111
125
|
properties: {
|
|
126
|
+
titlePage: hasFrontHeader || hasFrontFooter,
|
|
112
127
|
page: {
|
|
113
128
|
size: {
|
|
114
129
|
//DOC JS does the orientation after the width and height are set
|
|
@@ -122,8 +137,8 @@ function renderSection(section: AD.Section.Section, parentResources: AD.Resource
|
|
|
122
137
|
top: pageContentMargins.top * abstractDocPixelToDocxDXARatio,
|
|
123
138
|
right: pageContentMargins.right * abstractDocPixelToDocxDXARatio,
|
|
124
139
|
left: pageContentMargins.left * abstractDocPixelToDocxDXARatio,
|
|
125
|
-
header:
|
|
126
|
-
footer:
|
|
140
|
+
header: pageHeaderMargins.top * abstractDocPixelToDocxDXARatio,
|
|
141
|
+
footer: pageFooterMargins.bottom * abstractDocPixelToDocxDXARatio,
|
|
127
142
|
},
|
|
128
143
|
},
|
|
129
144
|
},
|
|
@@ -131,11 +146,17 @@ function renderSection(section: AD.Section.Section, parentResources: AD.Resource
|
|
|
131
146
|
default: new Header({
|
|
132
147
|
children: headerChildren,
|
|
133
148
|
}),
|
|
149
|
+
first: hasFrontHeader ? new Header({
|
|
150
|
+
children: firstpageHeaderChildren
|
|
151
|
+
}) : undefined
|
|
134
152
|
},
|
|
135
153
|
footers: {
|
|
136
154
|
default: new Footer({
|
|
137
|
-
children: footerChildren
|
|
155
|
+
children: footerChildren
|
|
138
156
|
}),
|
|
157
|
+
first: hasFrontFooter ? new Footer({
|
|
158
|
+
children: firstPageFooterChildren
|
|
159
|
+
}) : undefined
|
|
139
160
|
},
|
|
140
161
|
children: contentChildren,
|
|
141
162
|
};
|
|
@@ -150,12 +171,12 @@ function renderHyperLink(
|
|
|
150
171
|
text: hyperLink.text,
|
|
151
172
|
font: style.fontFamily || "Helvetica",
|
|
152
173
|
size: fontSize,
|
|
153
|
-
color: style.color || "
|
|
174
|
+
color: style.color || "#0000ff",
|
|
154
175
|
bold: style.bold || style.fontWeight === "bold",
|
|
155
176
|
characterSpacing: style.characterSpacing,
|
|
156
177
|
underline: style.underline
|
|
157
178
|
? {
|
|
158
|
-
color: style.color || "
|
|
179
|
+
color: style.color || "#0000ff",
|
|
159
180
|
type: UnderlineType.SINGLE,
|
|
160
181
|
}
|
|
161
182
|
: undefined,
|
|
@@ -164,12 +185,12 @@ function renderHyperLink(
|
|
|
164
185
|
if (hyperLink.target.startsWith("#") && !hyperLink.target.startsWith("#page=")) {
|
|
165
186
|
return new InternalHyperlink({
|
|
166
187
|
anchor: hyperLink.target,
|
|
167
|
-
|
|
188
|
+
children: [textRun],
|
|
168
189
|
});
|
|
169
190
|
} else {
|
|
170
191
|
return new ExternalHyperlink({
|
|
171
192
|
link: hyperLink.target,
|
|
172
|
-
|
|
193
|
+
children: [textRun],
|
|
173
194
|
});
|
|
174
195
|
}
|
|
175
196
|
}
|
|
@@ -250,37 +271,37 @@ function renderTable(
|
|
|
250
271
|
},
|
|
251
272
|
borders: {
|
|
252
273
|
top: {
|
|
253
|
-
color: style.cellStyle.borderColor ??
|
|
274
|
+
color: style.cellStyle.borderColor ?? undefined,
|
|
254
275
|
size: 0,
|
|
255
276
|
style: BorderStyle.NONE,
|
|
256
277
|
},
|
|
257
278
|
right: {
|
|
258
|
-
color: style.cellStyle.borderColor ??
|
|
279
|
+
color: style.cellStyle.borderColor ?? undefined,
|
|
259
280
|
size: 0,
|
|
260
281
|
style: BorderStyle.NONE,
|
|
261
282
|
},
|
|
262
283
|
bottom: {
|
|
263
|
-
color: style.cellStyle.borderColor ??
|
|
284
|
+
color: style.cellStyle.borderColor ?? undefined,
|
|
264
285
|
size: 0,
|
|
265
286
|
style: BorderStyle.NONE,
|
|
266
287
|
},
|
|
267
288
|
left: {
|
|
268
|
-
color: style.cellStyle.borderColor ??
|
|
289
|
+
color: style.cellStyle.borderColor ?? undefined,
|
|
269
290
|
size: 0,
|
|
270
291
|
style: BorderStyle.NONE,
|
|
271
292
|
},
|
|
272
293
|
insideHorizontal: {
|
|
273
|
-
color: style.cellStyle.borderColor ??
|
|
294
|
+
color: style.cellStyle.borderColor ?? undefined,
|
|
274
295
|
size: 0,
|
|
275
296
|
style: BorderStyle.NONE,
|
|
276
297
|
},
|
|
277
298
|
insideVertical: {
|
|
278
|
-
color: style.cellStyle.borderColor ??
|
|
299
|
+
color: style.cellStyle.borderColor ?? undefined,
|
|
279
300
|
size: 0,
|
|
280
301
|
style: BorderStyle.NONE,
|
|
281
302
|
},
|
|
282
303
|
},
|
|
283
|
-
rows: table.children.map((c) => renderRow(c, resources, style.cellStyle, columnWidths, keepNext)),
|
|
304
|
+
rows: table.headerRows.concat(table.children).map((c) => renderRow(c, resources, style.cellStyle, columnWidths, keepNext)),
|
|
284
305
|
});
|
|
285
306
|
}
|
|
286
307
|
|
|
@@ -291,9 +312,28 @@ function renderRow(
|
|
|
291
312
|
columnWidths: ReadonlyArray<number>,
|
|
292
313
|
keepNext: boolean
|
|
293
314
|
): TableRow {
|
|
315
|
+
const children = row.children.reduce(
|
|
316
|
+
(acc, cell) => {
|
|
317
|
+
const span = cell.columnSpan ?? 1;
|
|
318
|
+
|
|
319
|
+
const width = columnWidths
|
|
320
|
+
.slice(acc.columnIndex, acc.columnIndex + span)
|
|
321
|
+
.reduce((a, b) => a + b, 0);
|
|
322
|
+
|
|
323
|
+
return {
|
|
324
|
+
columnIndex: acc.columnIndex + span,
|
|
325
|
+
children: [
|
|
326
|
+
...acc.children,
|
|
327
|
+
renderCell(cell, resources, tableCellStyle, width, keepNext),
|
|
328
|
+
],
|
|
329
|
+
};
|
|
330
|
+
},
|
|
331
|
+
{ columnIndex: 0, children: [] as TableCell[] }
|
|
332
|
+
).children;
|
|
333
|
+
|
|
294
334
|
return new TableRow({
|
|
295
335
|
cantSplit: true,
|
|
296
|
-
children:
|
|
336
|
+
children: children,
|
|
297
337
|
});
|
|
298
338
|
}
|
|
299
339
|
|
|
@@ -304,6 +344,8 @@ function renderCell(
|
|
|
304
344
|
width: number,
|
|
305
345
|
keepNext: boolean
|
|
306
346
|
): TableCell {
|
|
347
|
+
const abstractDocPxCellWidth = width / abstractDocPixelToDocxDXARatio;
|
|
348
|
+
|
|
307
349
|
const style = AD.Resources.getStyle(
|
|
308
350
|
tableCellStyle,
|
|
309
351
|
cell.style,
|
|
@@ -339,29 +381,29 @@ function renderCell(
|
|
|
339
381
|
},
|
|
340
382
|
borders: {
|
|
341
383
|
top: {
|
|
342
|
-
color: style.borderColor ??
|
|
343
|
-
size: styleBorders.top,
|
|
384
|
+
color: style.borderColor ?? undefined,
|
|
385
|
+
size: styleBorders.top * abstractDocBorderToDocxBorderSizeRatio,
|
|
344
386
|
style: styleBorders.top ? BorderStyle.SINGLE : BorderStyle.NONE,
|
|
345
387
|
},
|
|
346
388
|
right: {
|
|
347
|
-
color: style.borderColor ??
|
|
348
|
-
size: styleBorders.right,
|
|
389
|
+
color: style.borderColor ?? undefined,
|
|
390
|
+
size: styleBorders.right * abstractDocBorderToDocxBorderSizeRatio,
|
|
349
391
|
style: styleBorders.right ? BorderStyle.SINGLE : BorderStyle.NONE,
|
|
350
392
|
},
|
|
351
393
|
bottom: {
|
|
352
|
-
color: style.borderColor ??
|
|
353
|
-
size: styleBorders.bottom,
|
|
394
|
+
color: style.borderColor ?? undefined,
|
|
395
|
+
size: styleBorders.bottom * abstractDocBorderToDocxBorderSizeRatio,
|
|
354
396
|
style: styleBorders.bottom ? BorderStyle.SINGLE : BorderStyle.NONE,
|
|
355
397
|
},
|
|
356
398
|
left: {
|
|
357
|
-
color: style.borderColor ??
|
|
358
|
-
size: styleBorders.left,
|
|
399
|
+
color: style.borderColor ?? undefined,
|
|
400
|
+
size: styleBorders.left * abstractDocBorderToDocxBorderSizeRatio,
|
|
359
401
|
style: styleBorders.left ? BorderStyle.SINGLE : BorderStyle.NONE,
|
|
360
402
|
},
|
|
361
403
|
},
|
|
362
404
|
|
|
363
405
|
children: cell.children.reduce((sofar, c) => {
|
|
364
|
-
sofar.push(...renderSectionElement(c, resources,
|
|
406
|
+
sofar.push(...renderSectionElement(c, resources, abstractDocPxCellWidth, keepNext));
|
|
365
407
|
return sofar;
|
|
366
408
|
}, [] as Array<Paragraph | Table>),
|
|
367
409
|
});
|
|
@@ -446,7 +488,7 @@ function renderPageNumber(style: AD.TextStyle.TextStyle): TextRun {
|
|
|
446
488
|
return new TextRun({
|
|
447
489
|
font: style.fontFamily || "Helvetica",
|
|
448
490
|
size: fontSize,
|
|
449
|
-
color: style.color || "
|
|
491
|
+
color: style.color || "#000000",
|
|
450
492
|
bold: style.bold || style.fontWeight === "bold",
|
|
451
493
|
characterSpacing: style.characterSpacing,
|
|
452
494
|
underline: style.underline
|
|
@@ -464,7 +506,7 @@ function renderTotalPages(style: AD.TextStyle.TextStyle): TextRun {
|
|
|
464
506
|
return new TextRun({
|
|
465
507
|
font: style.fontFamily || "Helvetica",
|
|
466
508
|
size: fontSize,
|
|
467
|
-
color: style.color || "
|
|
509
|
+
color: style.color || "#000000",
|
|
468
510
|
bold: style.bold || style.fontWeight === "bold",
|
|
469
511
|
characterSpacing: style.characterSpacing,
|
|
470
512
|
underline: style.underline
|
|
@@ -484,7 +526,7 @@ function renderText(style: AD.TextStyle.TextStyle, text: string): TextRun {
|
|
|
484
526
|
text: text,
|
|
485
527
|
font: style.fontFamily || "Helvetica",
|
|
486
528
|
size: fontSize,
|
|
487
|
-
color: style.color || "
|
|
529
|
+
color: style.color || "#000000",
|
|
488
530
|
bold: style.bold || style.fontWeight === "bold",
|
|
489
531
|
characterSpacing: style.characterSpacing,
|
|
490
532
|
underline: style.underline
|