kongdeqiang
2024-10-16 413e771cf893e7374dbdacad25b3f5f8f6d46cb7
src/libs/word.js
@@ -28,7 +28,13 @@
        // 创建一个PizZip实例,内容为模板的内容
        let zip = new PizZip(content);
        // 创建并加载docx templater实例对象
        let doc = new docxtemplater().loadZip(zip);
        let doc = new docxtemplater().loadZip(zip).setOptions({
            linebreaks:true,
            paragraphLoop: true,
            nullGetter:function () {
                return ""
            }
        });
        // 设置模板变量的值
        doc.setData(e);
@@ -58,145 +64,151 @@
//下载word,带图片
export const exportDocWidthImg = (e, path , dname, imgSize) => {
  for (let attr in e) {
    if (e[attr] == null) {
      e[attr] = "";
    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;
        }
    // 创建一个PizZip实例,内容为模板的内容
    let zip = new PizZip(content);
    // 创建并加载docx templater实例对象
    let doc = new docxtemplater().loadZip(zip).setOptions({
      linebreaks:true,
      paragraphLoop: true,
      nullGetter:function () {
        return ""
      }
    });
        // 创建一个PizZip实例,内容为模板的内容
        let zip = new PizZip(content);
        // 创建并加载docx templater实例对象
        let doc = new docxtemplater().loadZip(zip).setOptions({
            linebreaks:true,
            paragraphLoop: true,
            nullGetter:function () {
                return ""
            }
        });
    // // 图片处理
    let opts = {}
    opts = {
      // 图像是否居中
      centered: false
    };
    opts.fileType = "docx";
    opts.getImage = (chartId) =>{
      return base64DataURLToArrayBuffer(chartId);
    }
        // // 图片处理
        let opts = {}
        opts = {
            // 图像是否居中
            centered: false
        };
        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 [300,300]
      }
    }
    let imageModule = new ImageModule(opts);
    doc.attachModule(imageModule);
        opts.getSize = function(img, tagVlaue, tagName){  //图片size
            if(imgSize.hasOwnProperty(tagName)) {
                return imgSize[tagName]
            }else {
                return [70, 30]
            }
        }
        let imageModule = new ImageModule(opts);
        doc.attachModule(imageModule);
    // 设置模板变量的值
    doc.setData(e);
    console.log(doc,'下载---带图片的---')
        // 设置模板变量的值
        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;
        }
    // 生成一个代表docxtemplater对象的zip文件(不是一个真实的文件,而是在内存中的表示)
    let out = doc.getZip().generate({
      type: "blob",
      mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
    });
    // 将目标文件对象保存为目标类型的文件,并命名
    saveAs(out, docxname);
  },2000);
        // 生成一个代表docxtemplater对象的zip文件(不是一个真实的文件,而是在内存中的表示)
        let out = doc.getZip().generate({
            type: "blob",
            mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
        });
        // 将目标文件对象保存为目标类型的文件,并命名
        saveAs(out, docxname);
    },2000);
}
//下载word,带图片
export const exportDocWidthImg2 = (e, path , dname, arry, imgSize) => {
  for (let attr in e) {
    if (e[attr] == null) {
      e[attr] = "";
    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;
        }
    // 创建一个PizZip实例,内容为模板的内容
    let zip = new PizZip(content);
    // 创建并加载docx templater实例对象
    let doc = new docxtemplater().loadZip(zip);
        // 创建一个PizZip实例,内容为模板的内容
        let zip = new PizZip(content);
        // 创建并加载docx templater实例对象
        let doc = new docxtemplater().loadZip(zip).setOptions({
            linebreaks:true,
            paragraphLoop: true,
            nullGetter:function () {
                return ""
            }
        });
    // // 图片处理
    const opts = {}
    opts.fileType = "docx";
    opts.getImage = (chartId) =>{
      return base64DataURLToArrayBuffer(chartId);
    }
        // // 图片处理
        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 [300,300]
      }
    }
    let imageModule = new ImageModule(opts);
    doc.attachModule(imageModule);
        opts.getSize = function(img, tagVlaue, tagName){  //图片size
            if(imgSize.hasOwnProperty(tagName)) {
                return imgSize[tagName]
            }else {
                return [300,300]
            }
        }
        let imageModule = new ImageModule(opts);
        doc.attachModule(imageModule);
    // 设置模板变量的值
    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;
    }
        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;
        }
    // 生成一个代表docxtemplater对象的zip文件(不是一个真实的文件,而是在内存中的表示)
    let out = doc.getZip().generate({
      type: "blob",
      mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
    });
    // 将目标文件对象保存为目标类型的文件,并命名
    saveAs(out, docxname);
  },2000);
        // 生成一个代表docxtemplater对象的zip文件(不是一个真实的文件,而是在内存中的表示)
        let out = doc.getZip().generate({
            type: "blob",
            mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
        });
        // 将目标文件对象保存为目标类型的文件,并命名
        saveAs(out, docxname);
    },2000);
}
//预览word,不带图片的