kongdeqiang
2025-02-20 105128d7780861f699a3261fdad68804fe5b7c80
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
import docxtemplater from 'docxtemplater'
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,/;
 
//下载word,不带图片
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;
        }
 
        // 创建一个PizZip实例,内容为模板的内容
        let zip = new PizZip(content);
        // 创建并加载docx templater实例对象
        let doc = new docxtemplater().loadZip(zip).setOptions({
            linebreaks:true,
            paragraphLoop: true,
            nullGetter:function () {
                return ""
            }
        });
        // 设置模板变量的值
        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;
        }
 
        // 生成一个代表docxtemplater对象的zip文件(不是一个真实的文件,而是在内存中的表示)
        let out = doc.getZip().generate({
            type: "blob",
            mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
        });
        // 将目标文件对象保存为目标类型的文件,并命名
        saveAs(out, docxname);
    });
};
 
//下载word,带图片
export const exportDocWidthImg = (e, path , dname, imgSize) => {
 
    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;
        }
 
        // 创建一个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);
        }
 
        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);
 
        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);
}
 
//下载word,带图片
export const exportDocWidthImg2 = (e, path , dname, arry, imgSize) => {
    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;
        }
 
        // 创建一个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);
        }
 
        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);
 
        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);
}
 
//预览word,不带图片的
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;
        }
 
        // 创建一个PizZip实例,内容为模板的内容
        let zip = new PizZip(content);
        // 创建并加载docx 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
            };
            throw error;
        }
 
        // 生成一个代表docxtemplater对象的zip文件(不是一个真实的文件,而是在内存中的表示)
        let out = doc.getZip().generate({
            type: "blob",
            mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
        });
        docx.renderAsync(out, continer); // 渲染到页面预览
    });
};
 
//预览带图片的word
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;
        }
 
        const doc = new docxtemplater();
        // // 创建一个PizZip实例,内容为模板的内容
        const zip = new PizZip(content);
        doc.loadZip(zip)
        // // 图片处理
        const opts = {}
        //opts.centered = true;
        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 [70,40]
          }
        }
 
        let imageModule = new ImageModule(opts);
        doc.attachModule(imageModule);
 
        doc.setData(e);
        console.log(doc,'预览带图片====')
 
        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"
        });
        docx.renderAsync(out, continer); // 渲染到页面预览
    });
};
 
//预览带签名和按手印的word
export const viewWithImg2 = (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;
    }
 
    const doc = new docxtemplater();
    // // 创建一个PizZip实例,内容为模板的内容
    const zip = new PizZip(content);
    doc.loadZip(zip)
    // // 图片处理
    const opts = {}
    //opts.centered = true;
    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);
 
    try {
      //     //替换所有模板变量
      doc.render();
    } catch (error) {
      let e = {
        message: error.message,
        name: error.name,
        stack: error.stack,
        properties: error.properties
      };
      //     throw error;
    }
 
    // 生成一个代表docxtemplater对象的zip文件(不是一个真实的文件,而是在内存中的表示)
    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;
}
 
 
export const urlToBase64 = (url, callback) => {
  let image = new Image();
  //解决跨域问题
  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;
    //这里的dataurl就是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, "");
 
    // 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;
}