EvidenceGuard-AI / scripts /render_report_docx.js
leeshal's picture
Upload 55 files
a805048 verified
Raw History Blame Contribute Delete
8.1 kB
// Renders reports/report_model.json (built by build_report.py) into a Word document with docx-js.
// Usage: node scripts/render_report_docx.js reports/report_model.json reports/project_report.docx
const fs = require("fs");
const path = require("path");
const {
Document, Packer, Paragraph, TextRun, HeadingLevel, AlignmentType, Table, TableRow, TableCell,
WidthType, ShadingType, ImageRun, PageBreak, LevelFormat, Footer, Header, PageNumber,
TableOfContents, BorderStyle,
} = require("docx");
const [, , inPath, outPath] = process.argv;
const blocks = JSON.parse(fs.readFileSync(inPath, "utf8"));
const FONT = "Calibri";
const CM = 567; // DXA per cm
const CONTENT_W = 16 * CM; // A4 21 cm - 2 x 2.5 cm margins
function pngSize(file) {
const b = fs.readFileSync(file);
return { w: b.readUInt32BE(16), h: b.readUInt32BE(20), data: b };
}
const children = [];
const numberRefs = [];
const h1Titles = blocks.filter(x => x.t === "h1").map(x => x.text);
for (const b of blocks) {
switch (b.t) {
case "title":
children.push(new Paragraph({ spacing: { before: 2400, after: 240 }, alignment: AlignmentType.CENTER,
children: [new TextRun({ text: b.title, font: FONT, size: 56, bold: true, color: "1C5CAB" })] }));
children.push(new Paragraph({ spacing: { after: 960 }, alignment: AlignmentType.CENTER,
children: [new TextRun({ text: b.subtitle, font: FONT, size: 30 })] }));
for (const l of b.lines) children.push(new Paragraph({ alignment: AlignmentType.CENTER, spacing: { after: 160 },
children: [new TextRun({ text: l, font: FONT, size: 24 })] }));
children.push(new Paragraph({ alignment: AlignmentType.CENTER, spacing: { before: 1200 },
children: [new TextRun({ text: "All documents and data in this project are SYNTHETIC DEMONSTRATION DATA describing a fictional university.",
font: FONT, size: 20, italics: true, color: "B00020" })] }));
children.push(new Paragraph({ children: [new PageBreak()] }));
children.push(new Paragraph({ spacing: { after: 240 }, children: [new TextRun({ text: "Contents", font: FONT, size: 32, bold: true, color: "1C5CAB" })] }));
for (const t of h1Titles) children.push(new Paragraph({ spacing: { after: 90 }, indent: { left: t.match(/^\d/) ? 0 : 0 },
children: [new TextRun({ text: t, font: FONT, size: 23 })] }));
break;
case "h1":
children.push(new Paragraph({ heading: HeadingLevel.HEADING_1, children: [new TextRun(b.text)] })); break;
case "h2":
children.push(new Paragraph({ heading: HeadingLevel.HEADING_2, children: [new TextRun(b.text)] })); break;
case "p":
children.push(new Paragraph({ alignment: AlignmentType.JUSTIFIED, spacing: { after: 140, line: 300 },
children: [new TextRun({ text: b.text, font: FONT, size: 22 })] })); break;
case "caption":
children.push(new Paragraph({ alignment: AlignmentType.CENTER, spacing: { before: 60, after: 220 },
children: [new TextRun({ text: b.text, font: FONT, size: 18, italics: true, color: "52514E" })] })); break;
case "bullets":
case "numbers": {
const ref = b.t === "bullets" ? "bul" : `num${numberRefs.length}`;
if (b.t === "numbers") numberRefs.push(ref);
for (const it of b.items) children.push(new Paragraph({ numbering: { reference: ref, level: 0 },
spacing: { after: 80, line: 290 }, children: [new TextRun({ text: it, font: FONT, size: 22 })] }));
break;
}
case "refs":
b.items.forEach((r, i) => children.push(new Paragraph({ spacing: { after: 100 }, indent: { left: 567, hanging: 567 },
children: [new TextRun({ text: `[${i + 1}]\t${r}`, font: FONT, size: 20 })] })));
break;
case "code":
for (const line of b.text.split("\n")) children.push(new Paragraph({ spacing: { after: 0 },
shading: { fill: "F4F6F8", type: ShadingType.CLEAR, color: "auto" },
children: [new TextRun({ text: line, font: "Consolas", size: 18 })] }));
break;
case "pagebreak":
children.push(new Paragraph({ children: [new PageBreak()] })); break;
case "img": {
if (!fs.existsSync(b.path)) break;
const s = pngSize(b.path);
const wPx = Math.round(b.width_cm * 37.8);
let hPx = Math.round(wPx * s.h / s.w);
const maxH = 22 * 37.8;
let w = wPx;
if (hPx > maxH) { w = Math.round(wPx * maxH / hPx); hPx = Math.round(maxH); }
children.push(new Paragraph({ alignment: AlignmentType.CENTER, spacing: { before: 120 },
children: [new ImageRun({ type: "png", data: s.data, transformation: { width: w, height: hPx },
altText: { title: path.basename(b.path), description: b.caption || path.basename(b.path), name: path.basename(b.path) } })] }));
break;
}
case "table": {
const n = b.header.length;
let widths = b.widths || Array(n).fill(16 / n);
const tot = widths.reduce((a, c) => a + c, 0);
const dx = widths.map(x => Math.floor(x / tot * CONTENT_W));
dx[dx.length - 1] += CONTENT_W - dx.reduce((a, c) => a + c, 0);
const sz = Math.round((b.size || 8) * 2);
const border = { style: BorderStyle.SINGLE, size: 4, color: "BFBFBF" };
const borders = { top: border, bottom: border, left: border, right: border };
const mkCell = (text, i, head) => new TableCell({
width: { size: dx[i], type: WidthType.DXA }, borders,
shading: head ? { fill: "DCE8F7", type: ShadingType.CLEAR, color: "auto" } : undefined,
margins: { top: 40, bottom: 40, left: 70, right: 70 },
children: [new Paragraph({ children: [new TextRun({ text: String(text), font: FONT, size: sz, bold: head })] })],
});
const rows = [new TableRow({ tableHeader: true, children: b.header.map((h, i) => mkCell(h, i, true)) })];
for (const r of b.rows) rows.push(new TableRow({ cantSplit: true, children: r.map((c, i) => mkCell(c, i, false)) }));
children.push(new Table({ width: { size: CONTENT_W, type: WidthType.DXA }, columnWidths: dx, rows }));
break;
}
}
}
const numberingConfig = [{ reference: "bul", levels: [{ level: 0, format: LevelFormat.BULLET, text: "•",
alignment: AlignmentType.LEFT, style: { paragraph: { indent: { left: 567, hanging: 283 } } } }] }];
for (const ref of numberRefs) numberingConfig.push({ reference: ref, levels: [{ level: 0, format: LevelFormat.DECIMAL,
text: "%1.", alignment: AlignmentType.LEFT, style: { paragraph: { indent: { left: 567, hanging: 340 } } } }] });
const doc = new Document({
creator: "EvidenceGuard AI report builder",
title: "EvidenceGuard AI - MSc Project Report",
features: { updateFields: true },
styles: {
default: { document: { run: { font: FONT, size: 22 } } },
paragraphStyles: [
{ id: "Heading1", name: "Heading 1", basedOn: "Normal", next: "Normal", quickFormat: true,
run: { font: FONT, size: 32, bold: true, color: "1C5CAB" },
paragraph: { spacing: { before: 360, after: 160 }, keepNext: true, outlineLevel: 0 } },
{ id: "Heading2", name: "Heading 2", basedOn: "Normal", next: "Normal", quickFormat: true,
run: { font: FONT, size: 26, bold: true, color: "0B0B0B" },
paragraph: { spacing: { before: 240, after: 120 }, keepNext: true, outlineLevel: 1 } },
],
},
numbering: { config: numberingConfig },
sections: [{
properties: { page: { size: { width: 11906, height: 16838 },
margin: { top: 1418, bottom: 1418, left: 1418, right: 1418 } } },
headers: { default: new Header({ children: [new Paragraph({ alignment: AlignmentType.RIGHT,
children: [new TextRun({ text: "EvidenceGuard AI - MSc Project Report (synthetic data)", font: FONT, size: 16, color: "8A8986" })] })] }) },
footers: { default: new Footer({ children: [new Paragraph({ alignment: AlignmentType.CENTER,
children: [new TextRun({ children: ["Page ", PageNumber.CURRENT, " of ", PageNumber.TOTAL_PAGES], font: FONT, size: 18 })] })] }) },
children,
}],
});
Packer.toBuffer(doc).then(buf => { fs.writeFileSync(outPath, buf); console.log("docx written", outPath); });