abstract-document 19.1.25 → 19.2.1
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 +15 -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 +66 -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 +76 -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
|
}
|
|
@@ -232,6 +253,7 @@ function renderTable(
|
|
|
232
253
|
);
|
|
233
254
|
|
|
234
255
|
return new Table({
|
|
256
|
+
layout: "fixed",
|
|
235
257
|
alignment:
|
|
236
258
|
style.alignment === "Left"
|
|
237
259
|
? AlignmentType.LEFT
|
|
@@ -250,37 +272,37 @@ function renderTable(
|
|
|
250
272
|
},
|
|
251
273
|
borders: {
|
|
252
274
|
top: {
|
|
253
|
-
color: style.cellStyle.borderColor ??
|
|
275
|
+
color: style.cellStyle.borderColor ?? undefined,
|
|
254
276
|
size: 0,
|
|
255
277
|
style: BorderStyle.NONE,
|
|
256
278
|
},
|
|
257
279
|
right: {
|
|
258
|
-
color: style.cellStyle.borderColor ??
|
|
280
|
+
color: style.cellStyle.borderColor ?? undefined,
|
|
259
281
|
size: 0,
|
|
260
282
|
style: BorderStyle.NONE,
|
|
261
283
|
},
|
|
262
284
|
bottom: {
|
|
263
|
-
color: style.cellStyle.borderColor ??
|
|
285
|
+
color: style.cellStyle.borderColor ?? undefined,
|
|
264
286
|
size: 0,
|
|
265
287
|
style: BorderStyle.NONE,
|
|
266
288
|
},
|
|
267
289
|
left: {
|
|
268
|
-
color: style.cellStyle.borderColor ??
|
|
290
|
+
color: style.cellStyle.borderColor ?? undefined,
|
|
269
291
|
size: 0,
|
|
270
292
|
style: BorderStyle.NONE,
|
|
271
293
|
},
|
|
272
294
|
insideHorizontal: {
|
|
273
|
-
color: style.cellStyle.borderColor ??
|
|
295
|
+
color: style.cellStyle.borderColor ?? undefined,
|
|
274
296
|
size: 0,
|
|
275
297
|
style: BorderStyle.NONE,
|
|
276
298
|
},
|
|
277
299
|
insideVertical: {
|
|
278
|
-
color: style.cellStyle.borderColor ??
|
|
300
|
+
color: style.cellStyle.borderColor ?? undefined,
|
|
279
301
|
size: 0,
|
|
280
302
|
style: BorderStyle.NONE,
|
|
281
303
|
},
|
|
282
304
|
},
|
|
283
|
-
rows: table.children.map((c) => renderRow(c, resources, style.cellStyle, columnWidths, keepNext)),
|
|
305
|
+
rows: table.headerRows.concat(table.children).map((c) => renderRow(c, resources, style.cellStyle, columnWidths, keepNext)),
|
|
284
306
|
});
|
|
285
307
|
}
|
|
286
308
|
|
|
@@ -291,9 +313,28 @@ function renderRow(
|
|
|
291
313
|
columnWidths: ReadonlyArray<number>,
|
|
292
314
|
keepNext: boolean
|
|
293
315
|
): TableRow {
|
|
316
|
+
const children = row.children.reduce(
|
|
317
|
+
(acc, cell) => {
|
|
318
|
+
const span = cell.columnSpan ?? 1;
|
|
319
|
+
|
|
320
|
+
const width = columnWidths
|
|
321
|
+
.slice(acc.columnIndex, acc.columnIndex + span)
|
|
322
|
+
.reduce((a, b) => a + b, 0);
|
|
323
|
+
|
|
324
|
+
return {
|
|
325
|
+
columnIndex: acc.columnIndex + span,
|
|
326
|
+
children: [
|
|
327
|
+
...acc.children,
|
|
328
|
+
renderCell(cell, resources, tableCellStyle, width, keepNext),
|
|
329
|
+
],
|
|
330
|
+
};
|
|
331
|
+
},
|
|
332
|
+
{ columnIndex: 0, children: [] as TableCell[] }
|
|
333
|
+
).children;
|
|
334
|
+
|
|
294
335
|
return new TableRow({
|
|
295
336
|
cantSplit: true,
|
|
296
|
-
children:
|
|
337
|
+
children: children,
|
|
297
338
|
});
|
|
298
339
|
}
|
|
299
340
|
|
|
@@ -304,6 +345,8 @@ function renderCell(
|
|
|
304
345
|
width: number,
|
|
305
346
|
keepNext: boolean
|
|
306
347
|
): TableCell {
|
|
348
|
+
const abstractDocPxCellWidth = width / abstractDocPixelToDocxDXARatio;
|
|
349
|
+
|
|
307
350
|
const style = AD.Resources.getStyle(
|
|
308
351
|
tableCellStyle,
|
|
309
352
|
cell.style,
|
|
@@ -339,29 +382,29 @@ function renderCell(
|
|
|
339
382
|
},
|
|
340
383
|
borders: {
|
|
341
384
|
top: {
|
|
342
|
-
color: style.borderColor ??
|
|
343
|
-
size: styleBorders.top,
|
|
385
|
+
color: style.borderColor ?? undefined,
|
|
386
|
+
size: styleBorders.top * abstractDocBorderToDocxBorderSizeRatio,
|
|
344
387
|
style: styleBorders.top ? BorderStyle.SINGLE : BorderStyle.NONE,
|
|
345
388
|
},
|
|
346
389
|
right: {
|
|
347
|
-
color: style.borderColor ??
|
|
348
|
-
size: styleBorders.right,
|
|
390
|
+
color: style.borderColor ?? undefined,
|
|
391
|
+
size: styleBorders.right * abstractDocBorderToDocxBorderSizeRatio,
|
|
349
392
|
style: styleBorders.right ? BorderStyle.SINGLE : BorderStyle.NONE,
|
|
350
393
|
},
|
|
351
394
|
bottom: {
|
|
352
|
-
color: style.borderColor ??
|
|
353
|
-
size: styleBorders.bottom,
|
|
395
|
+
color: style.borderColor ?? undefined,
|
|
396
|
+
size: styleBorders.bottom * abstractDocBorderToDocxBorderSizeRatio,
|
|
354
397
|
style: styleBorders.bottom ? BorderStyle.SINGLE : BorderStyle.NONE,
|
|
355
398
|
},
|
|
356
399
|
left: {
|
|
357
|
-
color: style.borderColor ??
|
|
358
|
-
size: styleBorders.left,
|
|
400
|
+
color: style.borderColor ?? undefined,
|
|
401
|
+
size: styleBorders.left * abstractDocBorderToDocxBorderSizeRatio,
|
|
359
402
|
style: styleBorders.left ? BorderStyle.SINGLE : BorderStyle.NONE,
|
|
360
403
|
},
|
|
361
404
|
},
|
|
362
405
|
|
|
363
406
|
children: cell.children.reduce((sofar, c) => {
|
|
364
|
-
sofar.push(...renderSectionElement(c, resources,
|
|
407
|
+
sofar.push(...renderSectionElement(c, resources, abstractDocPxCellWidth, keepNext));
|
|
365
408
|
return sofar;
|
|
366
409
|
}, [] as Array<Paragraph | Table>),
|
|
367
410
|
});
|
|
@@ -446,7 +489,7 @@ function renderPageNumber(style: AD.TextStyle.TextStyle): TextRun {
|
|
|
446
489
|
return new TextRun({
|
|
447
490
|
font: style.fontFamily || "Helvetica",
|
|
448
491
|
size: fontSize,
|
|
449
|
-
color: style.color || "
|
|
492
|
+
color: style.color || "#000000",
|
|
450
493
|
bold: style.bold || style.fontWeight === "bold",
|
|
451
494
|
characterSpacing: style.characterSpacing,
|
|
452
495
|
underline: style.underline
|
|
@@ -464,7 +507,7 @@ function renderTotalPages(style: AD.TextStyle.TextStyle): TextRun {
|
|
|
464
507
|
return new TextRun({
|
|
465
508
|
font: style.fontFamily || "Helvetica",
|
|
466
509
|
size: fontSize,
|
|
467
|
-
color: style.color || "
|
|
510
|
+
color: style.color || "#000000",
|
|
468
511
|
bold: style.bold || style.fontWeight === "bold",
|
|
469
512
|
characterSpacing: style.characterSpacing,
|
|
470
513
|
underline: style.underline
|
|
@@ -484,7 +527,7 @@ function renderText(style: AD.TextStyle.TextStyle, text: string): TextRun {
|
|
|
484
527
|
text: text,
|
|
485
528
|
font: style.fontFamily || "Helvetica",
|
|
486
529
|
size: fontSize,
|
|
487
|
-
color: style.color || "
|
|
530
|
+
color: style.color || "#000000",
|
|
488
531
|
bold: style.bold || style.fontWeight === "bold",
|
|
489
532
|
characterSpacing: style.characterSpacing,
|
|
490
533
|
underline: style.underline
|