From 0796eebe3520fafb0ac5d36ee584af81506d7e9c Mon Sep 17 00:00:00 2001
From: riku <risaku@163.com>
Date: 星期六, 20 九月 2025 14:05:52 +0800
Subject: [PATCH] 2025.9.20 数据产品(待完成)

---
 src/utils/doc.js |  157 ++++++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 122 insertions(+), 35 deletions(-)

diff --git a/src/utils/doc.js b/src/utils/doc.js
index 5e30908..a757cfc 100644
--- a/src/utils/doc.js
+++ b/src/utils/doc.js
@@ -3,9 +3,110 @@
 import docxtemplater from 'docxtemplater';
 import ImageModule from 'docxtemplater-image-module-free';
 import Pizzip from 'pizzip';
-import PizZipUtils from 'pizzip/utils/dist/pizzip-utils';
 import FileSaver from 'file-saver';
-import fs from 'fs';
+
+/**
+ * 绛夋瘮渚嬬缉鏀惧浘鐗�
+ * 鏍规嵁鍥剧墖鐨勯暱瀹芥瘮杩涜涓嶅悓鏂瑰紡鐨勭缉鏀�
+ * 濡傛灉瀹藉害澶т簬楂樺害锛堟í鎷嶅浘鐗囷級锛屽垯鎸夌収璁惧畾楂樺害绛夋瘮缂╂斁锛�
+ * 濡傛灉瀹藉害灏忎簬楂樺害锛堢珫鎷嶅浘鐗囷級锛屽垯鎸夌収璁惧畾瀹藉害绛夋瘮缂╂斁锛�
+ * @param {Number} horizontalHeight 璁惧畾楂樺害
+ * @param {Number} verticalWidth 璁惧畾瀹藉害
+ * @param {*} img
+ * @param {*} tagValue
+ * @param {*} tagName
+ * @returns
+ */
+function getSizeProportional(
+  horizontalHeight,
+  verticalWidth,
+  img,
+  tagValue,
+  tagName
+) {
+  return new Promise(function (resolve, reject) {
+    const image = new Image();
+    image.src = tagValue;
+    image.onload = function () {
+      let width = image.width;
+      let height = image.height;
+      // console.log('width height', width, height);
+
+      if (width > height && horizontalHeight && height > horizontalHeight) {
+        const scale = image.height / horizontalHeight;
+        height = horizontalHeight;
+        width = image.width / scale;
+      } else if (width <= height && verticalWidth && width > verticalWidth) {
+        const scale = image.width / verticalWidth;
+        width = verticalWidth;
+        height = image.height / scale;
+      }
+      // console.log('scale', width, height);
+
+      resolve([width, height]);
+    };
+    image.onerror = function (e) {
+      console.log('img, tagValue, tagName : ', img, tagValue, tagName);
+      alert('An error occured while loading ' + tagValue);
+      reject(e);
+    };
+  });
+}
+
+/**
+ * 鍥哄畾澶у皬缂╂斁鍥剧墖
+ * 鏍规嵁鍥剧墖鐨勯暱瀹芥瘮杩涜涓嶅悓鏂瑰紡鐨勭缉鏀�
+ * 濡傛灉瀹藉害澶т簬楂樺害锛堟í鎷嶅浘鐗囷級锛屽垯鎸夌収璁惧畾楂樺害鍜屽楂樻瘮缂╂斁锛�
+ * 濡傛灉瀹藉害灏忎簬楂樺害锛堢珫鎷嶅浘鐗囷級锛屽垯鎸夌収璁惧畾瀹藉害鍜屽楂樻瘮缂╂斁锛�
+ * @param {*} horizontalHeight 璁惧畾楂樺害
+ * @param {*} verticalWidth 璁惧畾瀹藉害
+ * @param {*} scale 缂╂斁姣斾緥锛岄暱杈归櫎鐭竟鐨勬瘮渚嬬郴鏁�
+ * @param {*} img
+ * @param {*} tagValue
+ * @param {*} tagName
+ * @returns
+ */
+function getSizeFixed(
+  horizontalHeight,
+  verticalWidth,
+  scale,
+  img,
+  tagValue,
+  tagName
+) {
+  return new Promise(function (resolve, reject) {
+    const image = new Image();
+    image.src = tagValue;
+    image.onload = function () {
+      let width = image.width;
+      let height = image.height;
+
+      if (
+        width > height &&
+        horizontalHeight &&
+        height > horizontalHeight &&
+        scale
+      ) {
+        height = horizontalHeight;
+        width = horizontalHeight * scale;
+      } else if (
+        width <= height &&
+        verticalWidth &&
+        width > verticalWidth &&
+        scale
+      ) {
+        width = verticalWidth;
+        height = verticalWidth * scale;
+      }
+      resolve([width, height]);
+    };
+    image.onerror = function (e) {
+      console.log('img, tagValue, tagName : ', img, tagValue, tagName);
+      alert('An error occured while loading ' + tagValue);
+      reject(e);
+    };
+  });
+}
 
 /**
  * 鑾峰彇鍥剧墖閰嶇疆淇℃伅
@@ -14,8 +115,8 @@
  * @returns
  */
 function getImageOptions(options) {
-  const horizontalHeight = options ? options.horizontalHeight : undefined
-  const verticalWidth = options ? options.verticalWidth : undefined
+  const horizontalHeight = options ? options.horizontalHeight : undefined;
+  const verticalWidth = options ? options.verticalWidth : undefined;
   return {
     centered: false,
     fileType: 'docx',
@@ -30,34 +131,17 @@
         });
       });
     },
+
     getSize(img, tagValue, tagName) {
-      return new Promise(function (resolve, reject) {
-        const image = new Image();
-        image.src = tagValue;
-        image.onload = function () {
-          let width = image.width;
-          let height = image.height;
-          // console.log('width height', width, height);
-
-          if (width > height && horizontalHeight && height > horizontalHeight) {
-            const scale = image.height / horizontalHeight;
-            height = horizontalHeight;
-            width = image.width / scale;
-          } else if (width <= height && verticalWidth && width > verticalWidth) {
-            const scale = image.width / verticalWidth;
-            width = verticalWidth;
-            height = image.height / scale;
-          }
-          // console.log('scale', width, height);
-
-          resolve([width, height]);
-        };
-        image.onerror = function (e) {
-          console.log('img, tagValue, tagName : ', img, tagValue, tagName);
-          alert('An error occured while loading ' + tagValue);
-          reject(e);
-        };
-      });
+      // return getSizeProportional(horizontalHeight, verticalWidth, img, tagValue, tagName)
+      return getSizeFixed(
+        horizontalHeight,
+        verticalWidth,
+        options.scale,
+        img,
+        tagValue,
+        tagName
+      );
     }
   };
 }
@@ -70,12 +154,15 @@
         throw error;
       }
       const zip = new Pizzip(content);
-      const imageOptions = getImageOptions(imageSize);
       let doc = new docxtemplater()
         .setOptions({ paragraphLoop: true })
-        .loadZip(zip)
-        .attachModule(new ImageModule(imageOptions))
-        .compile();
+        .loadZip(zip);
+
+      if (imageSize) {
+        const imageOptions = getImageOptions(imageSize);
+        doc.attachModule(new ImageModule(imageOptions));
+      }
+      doc.compile();
       doc.resolveData(data).then(() => {
         try {
           doc.render();

--
Gitblit v1.9.3