validate-health-template.mjs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import fs from "fs";
  2. import PizZip from "pizzip";
  3. import Docxtemplater from "docxtemplater";
  4. import ImageModule from "docxtemplater-image-module-free";
  5. import {
  6. HEALTH_IMAGE_MARKERS,
  7. patchTemplateXml,
  8. } from "../src/server/reportService/docxReportBuilder.js";
  9. const zip = new PizZip(fs.readFileSync("templates/health-report-template.docx"));
  10. let xml = zip.file("word/document.xml").asText();
  11. console.log(
  12. "p open/close",
  13. (xml.match(/<w:p(?:\s|>)/g) || []).length,
  14. (xml.match(/<\/w:p>/g) || []).length,
  15. );
  16. const wt = [];
  17. xml.replace(/<w:t(?:\s[^>]*)?>([\s\S]*?)<\/w:t>/g, (_, t) => {
  18. wt.push(t);
  19. return _;
  20. });
  21. const bad = wt.filter(
  22. (t) => (t.match(/{/g) || []).length !== (t.match(/}/g) || []).length,
  23. );
  24. console.log("unbalanced w:t", bad.length);
  25. bad.slice(0, 10).forEach((t) => console.log(JSON.stringify(t.slice(0, 120))));
  26. xml = patchTemplateXml(xml, HEALTH_IMAGE_MARKERS);
  27. zip.file("word/document.xml", xml);
  28. const empty = Buffer.from(
  29. "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==",
  30. "base64",
  31. );
  32. const imageModule = new ImageModule({
  33. centered: true,
  34. getImage: () => empty,
  35. getSize: () => [100, 60],
  36. });
  37. const doc = new Docxtemplater(zip, {
  38. paragraphLoop: true,
  39. linebreaks: true,
  40. modules: [imageModule],
  41. nullGetter: () => "",
  42. });
  43. doc.render({
  44. farm_name: "测试",
  45. field_id: "F1",
  46. create_time: "t",
  47. source_datetime: "d",
  48. turbine_count: "1",
  49. valid_turbine_count: "1",
  50. turbine_types: "x",
  51. overall_score: "80",
  52. system_score: "70",
  53. component_score: "70",
  54. "structure_score/未评估": "70",
  55. excellent_count: "1",
  56. good_count: "0",
  57. fair_count: "0",
  58. poor_count: "0",
  59. overall_level_text: "良",
  60. main_level: "良",
  61. level_distribution_text: "x",
  62. overall_summary_text: "x",
  63. focus_engine_list: "A",
  64. chart_fig1: "a",
  65. chart_fig2: "b",
  66. turbine_rows: [
  67. {
  68. index: 1,
  69. engine_name: "A1",
  70. machine_type: "T",
  71. overall: "80",
  72. level: "良",
  73. system: "70",
  74. component: "70",
  75. structure: "70",
  76. yaw: "1",
  77. pitch: "1",
  78. mcs: "1",
  79. hpu: "1",
  80. generator: "1",
  81. converter: "1",
  82. gearbox: "1",
  83. shaft: "1",
  84. rotor: "1",
  85. tower: "1",
  86. },
  87. ],
  88. focus_rows: [
  89. {
  90. index: 1,
  91. engine_name: "A1",
  92. machine_type: "T",
  93. overall: "80",
  94. level: "中",
  95. system: "70",
  96. component: "70",
  97. structure: "70",
  98. low_item: "系统健康",
  99. },
  100. ],
  101. turbine_trends: [
  102. {
  103. engine_title: "A1 趋势",
  104. charts: [
  105. { chart_title: "综合健康评分趋势图", trend_image: "a" },
  106. { chart_title: "结构健康趋势图", trend_image: "a" },
  107. ],
  108. },
  109. ],
  110. });
  111. console.log("docxtemplater compile+render ok");