-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender-docx.ts
More file actions
129 lines (125 loc) · 5.26 KB
/
Copy pathrender-docx.ts
File metadata and controls
129 lines (125 loc) · 5.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import {
AlignmentType,
BorderStyle,
convertMillimetersToTwip,
Document,
HeadingLevel,
LevelFormat,
LineRuleType,
PageBreak,
Packer,
Paragraph,
Table,
TableCell,
TableLayoutType,
TableRow,
TextRun,
VerticalAlign,
WidthType,
} from "docx";
import type { Block, ResolvedDocument, TextStyle } from "./model.js";
const pointToHalfPoint = (point: number) => Math.round(point * 2);
const pointToTwip = (point: number) => Math.round(point * 20);
const wordFont = (family: string) => ({ ascii: family, hAnsi: family, eastAsia: family, cs: family });
const runStyle = (style: TextStyle) => ({
font: wordFont(style.fontFamily),
size: pointToHalfPoint(style.fontSize.value),
color: style.color,
bold: style.bold,
});
const paragraphSpacing = (style: TextStyle) => ({
before: pointToTwip(style.spacingBefore.value),
after: pointToTwip(style.spacingAfter.value),
line: pointToTwip(style.lineHeight.value),
lineRule: LineRuleType.EXACT,
});
function renderTable(block: Extract<Block, { type: "table" }>, document: ResolvedDocument) {
const page = document.page;
const usableMm = page.width.value - page.margins.left.value - page.margins.right.value;
const tableWidth = convertMillimetersToTwip(usableMm);
const widths = block.columnFractions.map((fraction) => Math.round(tableWidth * fraction));
widths[widths.length - 1] += tableWidth - widths.reduce((sum, width) => sum + width, 0);
const border = { style: BorderStyle.SINGLE, size: 4, color: "B8C2CC" };
const cell = (text: string, width: number, header: boolean) => new TableCell({
width: { size: width, type: WidthType.DXA },
verticalAlign: VerticalAlign.CENTER,
shading: header ? { fill: document.styles.tableHeaderFill } : undefined,
margins: { top: 62, bottom: 62, left: 119, right: 119 },
children: [new Paragraph({
spacing: { after: 0, line: pointToTwip(document.styles.body.lineHeight.value), lineRule: LineRuleType.EXACT },
children: [new TextRun({ text, ...runStyle(document.styles.body), bold: header })],
})],
});
return new Table({
width: { size: tableWidth, type: WidthType.DXA },
columnWidths: widths,
layout: TableLayoutType.FIXED,
borders: { top: border, bottom: border, left: border, right: border, insideHorizontal: border, insideVertical: border },
rows: [
new TableRow({ tableHeader: true, children: block.columns.map((text, index) => cell(text, widths[index], true)) }),
...block.rows.map((row) => new TableRow({ cantSplit: true, children: row.map((text, index) => cell(text, widths[index], false)) })),
],
});
}
function renderBlock(block: Block, document: ResolvedDocument) {
const body = document.styles.body;
switch (block.type) {
case "heading": {
const headingStyle = block.level === 1 ? document.styles.heading1 : document.styles.heading2;
return [new Paragraph({
heading: block.level === 1 ? HeadingLevel.HEADING_1 : HeadingLevel.HEADING_2,
spacing: paragraphSpacing(headingStyle),
keepNext: true,
children: [new TextRun({ text: block.text, ...runStyle(headingStyle) })],
})];
}
case "paragraph":
return [new Paragraph({ spacing: paragraphSpacing(body), children: [new TextRun({ text: block.text, ...runStyle(body) })] })];
case "bulletList":
return block.items.map((text) => new Paragraph({
numbering: { reference: "reference-bullets", level: 0 },
spacing: paragraphSpacing(body),
children: [new TextRun({ text, ...runStyle(body) })],
}));
case "table":
return [renderTable(block, document)];
case "pageBreak":
return [new Paragraph({ children: [new PageBreak()] })];
}
}
export async function renderDocx(document: ResolvedDocument): Promise<Buffer> {
const { body, heading1, heading2 } = document.styles;
const output = new Document({
creator: "web-docx-layout",
title: document.title,
description: "Web and DOCX generated from one resolved physical layout",
styles: { default: {
document: { run: runStyle(body), paragraph: { spacing: paragraphSpacing(body) } },
heading1: { run: runStyle(heading1), paragraph: { spacing: paragraphSpacing(heading1), keepNext: true } },
heading2: { run: runStyle(heading2), paragraph: { spacing: paragraphSpacing(heading2), keepNext: true } },
} },
numbering: { config: [{
reference: "reference-bullets",
levels: [{
level: 0,
format: LevelFormat.BULLET,
text: "•",
alignment: AlignmentType.LEFT,
style: { paragraph: { indent: { left: 720, hanging: 360 }, spacing: paragraphSpacing(body) }, run: runStyle(body) },
}],
}] },
sections: [{
properties: { page: {
size: { width: convertMillimetersToTwip(document.page.width.value), height: convertMillimetersToTwip(document.page.height.value) },
margin: {
top: convertMillimetersToTwip(document.page.margins.top.value),
right: convertMillimetersToTwip(document.page.margins.right.value),
bottom: convertMillimetersToTwip(document.page.margins.bottom.value),
left: convertMillimetersToTwip(document.page.margins.left.value),
},
} },
children: document.blocks.flatMap((block) => renderBlock(block, document)),
}],
});
return Packer.toBuffer(output);
}