From 2591d9565cda1c392a630ddfd988952c7ae65cda Mon Sep 17 00:00:00 2001
From: yang <1441163367@qq.com>
Date: 星期三, 28 十二月 2022 17:10:55 +0800
Subject: [PATCH] 第一版PAD

---
 src/libs/word.js |  386 ++++++++++++++++++++++++++++---------------------------
 1 files changed, 196 insertions(+), 190 deletions(-)

diff --git a/src/libs/word.js b/src/libs/word.js
index a2ca891..8b758e2 100644
--- a/src/libs/word.js
+++ b/src/libs/word.js
@@ -2,62 +2,65 @@
 import PizZip from 'pizzip'
 import JSZipUtils from 'jszip-utils'
 import {saveAs} from 'file-saver'
+
 let docx = require("docx-preview");
 const ImageModule = require("docxtemplater-image-module-free");
 
 const base64Regex =
-    /^data:image\/(png|jpg|svg|svg\+xml);base64,/;
+  /^data:image\/(png|jpg|svg|svg\+xml);base64,/;
 
 //涓嬭浇word锛屼笉甯﹀浘鐗�
-export const exportDoc = (e, path , dname) => {
-    for (let attr in e) {
-        if (e[attr] == null) {
-            e[attr] = "";
-        }
+export const exportDoc = (e, path, dname) => {
+  for (let attr in e) {
+    if (e[attr] == null) {
+      e[attr] = "";
     }
-    let docxsrc = path;        //妯℃澘鏂囦欢鐨勪綅缃�
-    let docxname = dname;        //瀵煎嚭鏂囦欢鐨勫悕瀛�
-    // 璇诲彇骞惰幏寰楁ā鏉挎枃浠剁殑浜岃繘鍒跺唴瀹�
-    JSZipUtils.getBinaryContent(docxsrc, function (error, content) {
-        // docxsrc鏄ā鏉裤�傛垜浠湪瀵煎嚭鐨勬椂鍊欙紝浼氭牴鎹妯℃澘鏉ュ鍑哄搴旂殑鏁版嵁
-        // 鎶涘嚭寮傚父
-        if (error) {
-            throw error;
-        }
+  }
+  let docxsrc = path;        //妯℃澘鏂囦欢鐨勪綅缃�
+  let docxname = dname;        //瀵煎嚭鏂囦欢鐨勫悕瀛�
+  // 璇诲彇骞惰幏寰楁ā鏉挎枃浠剁殑浜岃繘鍒跺唴瀹�
+  JSZipUtils.getBinaryContent(docxsrc, function (error, content) {
+    // docxsrc鏄ā鏉裤�傛垜浠湪瀵煎嚭鐨勬椂鍊欙紝浼氭牴鎹妯℃澘鏉ュ鍑哄搴旂殑鏁版嵁
+    // 鎶涘嚭寮傚父
+    if (error) {
+      throw error;
+    }
 
-        // 鍒涘缓涓�涓狿izZip瀹炰緥锛屽唴瀹逛负妯℃澘鐨勫唴瀹�
-        let zip = new PizZip(content);
-        // 鍒涘缓骞跺姞杞絛ocx templater瀹炰緥瀵硅薄
-        let doc = new docxtemplater().loadZip(zip);
-        // 璁剧疆妯℃澘鍙橀噺鐨勫��
-        doc.setData(e);
+    // 鍒涘缓涓�涓狿izZip瀹炰緥锛屽唴瀹逛负妯℃澘鐨勫唴瀹�
+    let zip = new PizZip(content);
+    // 鍒涘缓骞跺姞杞絛ocx templater瀹炰緥瀵硅薄
+    let doc = new docxtemplater().loadZip(zip);
+    // 璁剧疆妯℃澘鍙橀噺鐨勫��
+    doc.setData(e);
+    console.log(doc.setData(e), 'doc.setData(e);')
 
-        try {
-            //鏇挎崲鎵�鏈夋ā鏉垮彉閲�
-            doc.render();
-        } catch (error) {
-            let e = {
-                message: error.message,
-                name: error.name,
-                stack: error.stack,
-                properties: error.properties
-            };
-            console.log(JSON.stringify({error: e}));
-            throw error;
-        }
-
-        // 鐢熸垚涓�涓唬琛╠ocxtemplater瀵硅薄鐨剒ip鏂囦欢锛堜笉鏄竴涓湡瀹炵殑鏂囦欢锛岃�屾槸鍦ㄥ唴瀛樹腑鐨勮〃绀猴級
-        let out = doc.getZip().generate({
-            type: "blob",
-            mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
-        });
-        // 灏嗙洰鏍囨枃浠跺璞′繚瀛樹负鐩爣绫诲瀷鐨勬枃浠讹紝骞跺懡鍚�
-        saveAs(out, docxname);
+    try {
+      //鏇挎崲鎵�鏈夋ā鏉垮彉閲�
+      doc.render();
+    } catch (error) {
+      let e = {
+        message: error.message,
+        name: error.name,
+        stack: error.stack,
+        properties: error.properties
+      };
+      console.log(JSON.stringify({error: e}));
+      throw error;
+    }
+    console.log(doc, 'out~doc')
+    // 鐢熸垚涓�涓唬琛╠ocxtemplater瀵硅薄鐨剒ip鏂囦欢锛堜笉鏄竴涓湡瀹炵殑鏂囦欢锛岃�屾槸鍦ㄥ唴瀛樹腑鐨勮〃绀猴級
+    let out = doc.getZip().generate({
+      type: "blob",
+      mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
     });
+    console.log(doc.getZip().generate, 'doc.getZip().generate')
+    // 灏嗙洰鏍囨枃浠跺璞′繚瀛樹负鐩爣绫诲瀷鐨勬枃浠讹紝骞跺懡鍚�
+    saveAs(out, docxname);
+  });
 };
 
 //涓嬭浇word锛屽甫鍥剧墖
-export const exportDocWidthImg = (e, path , dname, imgSize) => {
+export const exportDocWidthImg = (e, path, dname, imgSize) => {
   for (let attr in e) {
     if (e[attr] == null) {
       e[attr] = "";
@@ -81,15 +84,15 @@
     // // 鍥剧墖澶勭悊
     const opts = {}
     opts.fileType = "docx";
-    opts.getImage = (chartId) =>{
+    opts.getImage = (chartId) => {
       return base64DataURLToArrayBuffer(chartId);
     }
 
-    opts.getSize = function(img, tagVlaue, tagName){  //鍥剧墖size
-      if(imgSize.hasOwnProperty(tagName)) {
+    opts.getSize = function (img, tagVlaue, tagName) {  //鍥剧墖size
+      if (imgSize.hasOwnProperty(tagName)) {
         return imgSize[tagName]
-      }else {
-        return [100,100]
+      } else {
+        return [100, 100]
       }
     }
     let imageModule = new ImageModule(opts);
@@ -123,172 +126,175 @@
 }
 
 //棰勮word锛屼笉甯﹀浘鐗囩殑
-export const viewD = (e, path , continer) => {
-    for (let attr in e) {
-        if (e[attr] == null) {
-            e[attr] = "";
-        }
+export const viewD = (e, path, continer) => {
+  for (let attr in e) {
+    if (e[attr] == null) {
+      e[attr] = "";
     }
-    let docxsrc = path;        //妯℃澘鏂囦欢鐨勪綅缃�
-    // 璇诲彇骞惰幏寰楁ā鏉挎枃浠剁殑浜岃繘鍒跺唴瀹�
-    JSZipUtils.getBinaryContent(docxsrc, function (error, content) {
-        // docxsrc鏄ā鏉裤�傛垜浠湪瀵煎嚭鐨勬椂鍊欙紝浼氭牴鎹妯℃澘鏉ュ鍑哄搴旂殑鏁版嵁
-        // 鎶涘嚭寮傚父
-        if (error) {
-            throw error;
-        }
+  }
+  let docxsrc = path;        //妯℃澘鏂囦欢鐨勪綅缃�
+  // 璇诲彇骞惰幏寰楁ā鏉挎枃浠剁殑浜岃繘鍒跺唴瀹�
+  JSZipUtils.getBinaryContent(docxsrc, function (error, content) {
+    // docxsrc鏄ā鏉裤�傛垜浠湪瀵煎嚭鐨勬椂鍊欙紝浼氭牴鎹妯℃澘鏉ュ鍑哄搴旂殑鏁版嵁
+    // 鎶涘嚭寮傚父
+    if (error) {
+      throw error;
+    }
 
-        // 鍒涘缓涓�涓狿izZip瀹炰緥锛屽唴瀹逛负妯℃澘鐨勫唴瀹�
-        let zip = new PizZip(content);
-        // 鍒涘缓骞跺姞杞絛ocx templater瀹炰緥瀵硅薄
-        let doc = new docxtemplater().loadZip(zip);
-        // 璁剧疆妯℃澘鍙橀噺鐨勫��
-        doc.setData(e);
+    // 鍒涘缓涓�涓狿izZip瀹炰緥锛屽唴瀹逛负妯℃澘鐨勫唴瀹�
+    let zip = new PizZip(content);
+    // 鍒涘缓骞跺姞杞絛ocx templater瀹炰緥瀵硅薄
+    let doc = new docxtemplater().loadZip(zip);
+    // 璁剧疆妯℃澘鍙橀噺鐨勫��
+    doc.setData(e);
 
-        try {
-            //鏇挎崲鎵�鏈夋ā鏉垮彉閲�
-            doc.render();
-        } catch (error) {
-            let e = {
-                message: error.message,
-                name: error.name,
-                stack: error.stack,
-                properties: error.properties
-            };
-            console.log(JSON.stringify({error: e}));
-            throw error;
-        }
+    try {
+      //鏇挎崲鎵�鏈夋ā鏉垮彉閲�
+      doc.render();
+    } catch (error) {
+      let e = {
+        message: error.message,
+        name: error.name,
+        stack: error.stack,
+        properties: error.properties
+      };
+      console.log(JSON.stringify({error: e}));
+      throw error;
+    }
 
-        // 鐢熸垚涓�涓唬琛╠ocxtemplater瀵硅薄鐨剒ip鏂囦欢锛堜笉鏄竴涓湡瀹炵殑鏂囦欢锛岃�屾槸鍦ㄥ唴瀛樹腑鐨勮〃绀猴級
-        let out = doc.getZip().generate({
-            type: "blob",
-            mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
-        });
-        docx.renderAsync(out, continer); // 娓叉煋鍒伴〉闈㈤瑙�
+    // 鐢熸垚涓�涓唬琛╠ocxtemplater瀵硅薄鐨剒ip鏂囦欢锛堜笉鏄竴涓湡瀹炵殑鏂囦欢锛岃�屾槸鍦ㄥ唴瀛樹腑鐨勮〃绀猴級
+    let out = doc.getZip().generate({
+      type: "blob",
+      mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
     });
+    console.log(out, 'out')
+    // window.android.toDownLoadWord(out)
+    docx.renderAsync(out, continer); // 娓叉煋鍒伴〉闈㈤瑙�
+  });
 };
 
 //棰勮甯﹀浘鐗囩殑word
-export const viewWithImg = (e, path , continer, imgSize) => {
-    for (let attr in e) {
-        if (e[attr] == null) {
-            e[attr] = "";
-        }
+export const viewWithImg = (e, path, continer, imgSize) => {
+  for (let attr in e) {
+    if (e[attr] == null) {
+      e[attr] = "";
     }
-    let docxsrc = path;        //妯℃澘鏂囦欢鐨勪綅缃�
-    JSZipUtils.getBinaryContent(docxsrc, function (error, content) {
-        // docxsrc鏄ā鏉裤�傛垜浠湪瀵煎嚭鐨勬椂鍊欙紝浼氭牴鎹妯℃澘鏉ュ鍑哄搴旂殑鏁版嵁
-        // 鎶涘嚭寮傚父
-        if (error) {
-            throw error;
-        }
+  }
+  let docxsrc = path;        //妯℃澘鏂囦欢鐨勪綅缃�
+  JSZipUtils.getBinaryContent(docxsrc, function (error, content) {
+    // docxsrc鏄ā鏉裤�傛垜浠湪瀵煎嚭鐨勬椂鍊欙紝浼氭牴鎹妯℃澘鏉ュ鍑哄搴旂殑鏁版嵁
+    // 鎶涘嚭寮傚父
+    if (error) {
+      throw error;
+    }
 
-        const doc = new docxtemplater();
-        // // 鍒涘缓涓�涓狿izZip瀹炰緥锛屽唴瀹逛负妯℃澘鐨勫唴瀹�
-        const zip = new PizZip(content);
-        doc.loadZip(zip)
-        // // 鍥剧墖澶勭悊
-        const opts = {}
-        opts.fileType = "docx";
-        opts.getImage = (chartId) =>{
-            return base64DataURLToArrayBuffer(chartId);
-        }
+    const doc = new docxtemplater();
+    // // 鍒涘缓涓�涓狿izZip瀹炰緥锛屽唴瀹逛负妯℃澘鐨勫唴瀹�
+    const zip = new PizZip(content);
+    doc.loadZip(zip)
+    // // 鍥剧墖澶勭悊
+    const opts = {}
+    opts.fileType = "docx";
+    opts.getImage = (chartId) => {
+      return base64DataURLToArrayBuffer(chartId);
+    }
 
-        opts.getSize = function(img, tagVlaue, tagName){  //鍥剧墖size
-          if(imgSize.hasOwnProperty(tagName)) {
-            return imgSize[tagName]
-          }else {
-            return [100,100]
-          }
-        }
-        let imageModule = new ImageModule(opts);
-        doc.attachModule(imageModule);
-        doc.setData(e);
+    opts.getSize = function (img, tagVlaue, tagName) {  //鍥剧墖size
+      if (imgSize.hasOwnProperty(tagName)) {
+        return imgSize[tagName]
+      } else {
+        return [100, 100]
+      }
+    }
+    let imageModule = new ImageModule(opts);
+    doc.attachModule(imageModule);
+    doc.setData(e);
 
-        try {
-            //     //鏇挎崲鎵�鏈夋ā鏉垮彉閲�
-            doc.render();
-        } catch (error) {
-            let e = {
-                message: error.message,
-                name: error.name,
-                stack: error.stack,
-                properties: error.properties
-            };
-            console.log(JSON.stringify({error: e}));
-            //     throw error;
-        }
+    try {
+      //     //鏇挎崲鎵�鏈夋ā鏉垮彉閲�
+      doc.render();
+    } catch (error) {
+      let e = {
+        message: error.message,
+        name: error.name,
+        stack: error.stack,
+        properties: error.properties
+      };
+      console.log(JSON.stringify({error: e}));
+      //     throw error;
+    }
 
-        // 鐢熸垚涓�涓唬琛╠ocxtemplater瀵硅薄鐨剒ip鏂囦欢锛堜笉鏄竴涓湡瀹炵殑鏂囦欢锛岃�屾槸鍦ㄥ唴瀛樹腑鐨勮〃绀猴級
-        let out = doc.getZip().generate({
-            type: "blob",
-            mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
-        });
-        docx.renderAsync(out, continer); // 娓叉煋鍒伴〉闈㈤瑙�
+    // 鐢熸垚涓�涓唬琛╠ocxtemplater瀵硅薄鐨剒ip鏂囦欢锛堜笉鏄竴涓湡瀹炵殑鏂囦欢锛岃�屾槸鍦ㄥ唴瀛樹腑鐨勮〃绀猴級
+    let out = doc.getZip().generate({
+      type: "blob",
+      mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
     });
+    docx.renderAsync(out, continer); // 娓叉煋鍒伴〉闈㈤瑙�
+  });
 };
+
 function base64DataURLToArrayBuffer(dataURL) {
-    const base64Regex = /^data:image\/(png|jpg|svg|svg\+xml);base64,/;
-    if (!base64Regex.test(dataURL)) {
-        return false;
-    }
-    const stringBase64 = dataURL.replace(base64Regex, "");
-    let binaryString;
-    if (typeof window !== "undefined") {
-        binaryString = window.atob(stringBase64);
-    } else {
-        binaryString = new Buffer(stringBase64, "base64").toString("binary");
-    }
-    const len = binaryString.length;
-    const bytes = new Uint8Array(len);
-    for (let i = 0; i < len; i++) {
-        const ascii = binaryString.charCodeAt(i);
-        bytes[i] = ascii;
-    }
-    return bytes.buffer;
+  const base64Regex = /^data:image\/(png|jpg|svg|svg\+xml);base64,/;
+  if (!base64Regex.test(dataURL)) {
+    return false;
+  }
+  const stringBase64 = dataURL.replace(base64Regex, "");
+  let binaryString;
+  if (typeof window !== "undefined") {
+    binaryString = window.atob(stringBase64);
+  } else {
+    binaryString = new Buffer(stringBase64, "base64").toString("binary");
+  }
+  const len = binaryString.length;
+  const bytes = new Uint8Array(len);
+  for (let i = 0; i < len; i++) {
+    const ascii = binaryString.charCodeAt(i);
+    bytes[i] = ascii;
+  }
+  return bytes.buffer;
 }
 
 
 export const urlToBase64 = (url, callback) => {
-    let image = new Image();
-    //瑙e喅璺ㄥ煙闂
-    image.setAttribute('crossOrigin', '*');
-    image.src = url
-    image.onload = () => {
-        var canvas = document.createElement("canvas");
-        canvas.width = image.width;
-        canvas.height = image.height;
-        var context = canvas.getContext('2d');
-        context.drawImage(image, 0, 0, image.width, image.height);
-        var quality = 0.8;
-        //杩欓噷鐨刣ataurl灏辨槸base64绫诲瀷
-        var dataURL = canvas.toDataURL("image/png", quality);
-        callback ? callback(dataURL) : null; //璋冪敤鍥炶皟鍑芥暟
-    }
+  let image = new Image();
+  //瑙e喅璺ㄥ煙闂
+  image.setAttribute('crossOrigin', '*');
+  image.src = url
+  image.onload = () => {
+    var canvas = document.createElement("canvas");
+    canvas.width = image.width;
+    canvas.height = image.height;
+    var context = canvas.getContext('2d');
+    context.drawImage(image, 0, 0, image.width, image.height);
+    var quality = 0.8;
+    //杩欓噷鐨刣ataurl灏辨槸base64绫诲瀷
+    var dataURL = canvas.toDataURL("image/png", quality);
+    callback ? callback(dataURL) : null; //璋冪敤鍥炶皟鍑芥暟
+  }
 
 };
 
 function base64Parser(dataURL) {
-    if (
-        typeof dataURL !== "string" ||
-        !base64Regex.test(dataURL)
-    ) {
-        return false;
-    }
-    const stringBase64 = dataURL.replace(base64Regex, "");
+  if (
+    typeof dataURL !== "string" ||
+    !base64Regex.test(dataURL)
+  ) {
+    return false;
+  }
+  const stringBase64 = dataURL.replace(base64Regex, "");
 
-    // For nodejs
-    if (typeof Buffer !== "undefined" && Buffer.from) {
-        return Buffer.from(stringBase64, "base64");
-    }
+  // For nodejs
+  if (typeof Buffer !== "undefined" && Buffer.from) {
+    return Buffer.from(stringBase64, "base64");
+  }
 
-    // For browsers :
-    const binaryString = window.atob(stringBase64);
-    const len = binaryString.length;
-    const bytes = new Uint8Array(len);
-    for (let i = 0; i < len; i++) {
-        const ascii = binaryString.charCodeAt(i);
-        bytes[i] = ascii;
-    }
-    return bytes.buffer;
+  // For browsers :
+  const binaryString = window.atob(stringBase64);
+  const len = binaryString.length;
+  const bytes = new Uint8Array(len);
+  for (let i = 0; i < len; i++) {
+    const ascii = binaryString.charCodeAt(i);
+    bytes[i] = ascii;
+  }
+  return bytes.buffer;
 }

--
Gitblit v1.9.1