yichenxi
2022-12-13 4e15972e4db30f68e4ea74759fd04f00e6e44be2
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
// exportLUImage.js
import Docxtemplater from 'docxtemplater'
import PizZip from 'pizzip'
import JSZipUtils from 'jszip-utils'
import { saveAs } from 'file-saver'
import { imageToBase64 } from '@/utils/imageExchange'
 
let base64Img = ''
export const exportLUImage = (tempDocxPath, data, fileName) => {
  const ImageModule = require('docxtemplater-image-module-free')
  // 读取并获得模板文件的二进制内容
  JSZipUtils.getBinaryContent(tempDocxPath, (error, content) => {
    if (error) {
      throw error
    }
    // 图片处理
    const opts = {}
    opts.centered = true // 图片居中,在word模板中定义方式为{%%image}
    opts.fileType = 'docx'
 
    const p = new Promise(resolve => {
      // const images1 = []
      const images1 = {
        fileListOne: [],
        fileListTwo: [],
        o3Map: [],
        o3Sence: [],
        pm10Map: [],
        pm10Sence: [],
        so2Map: [],
        so2Sence: [],
        no2Map: [],
        no2Sence: [],
        coMap: [],
        coSence: [],
        tvocMap: [],
        tvocSence: [],
        pm25Map: [],
        pm25Sence: [],
      }
      if (data.index !== 0) {
        var num = 0
        for (let j = 0; j < data.fileLists.length; j++) {
          if (data.fileLists[j]) {
            data.fileLists[j].forEach(image => {
              main(image, (base64) => {
                // var src = `src${j + 1}`
                // images1.push({ [src]: base64 })
                if (j === 0) {
                  images1.fileListOne.push({ src: base64 })
                  // console.log(images1.fileListOne,'images1.fileListOne')
                } else if (j === 1) {
                  images1.fileListTwo.push({ src: base64 })
                } else if (j === 2) {
                  images1.fileListThree.push({ src: base64 })
                } else if (j === 3) {
                  images1.o3Map.push({ src: base64 })
                } else if (j === 4) {
                  images1.o3Sence.push({ src: base64 })
                } else if (j === 5) {
                  images1.pm10Map.push({ src: base64 })
                } else if (j === 6) {
                  images1.pm10Sence.push({ src: base64 })
                } else if (j === 7) {
                  images1.so2Map.push({ src: base64 })
                } else if (j === 8) {
                  images1.so2Sence.push({ src: base64 })
                }
                num = num + 1
                if (num === data.index) {
                  resolve(images1)
                }
              })
            })
          }
        }
      } else {
        resolve(images1)
      }
    })
    p.then(res => {
      opts.getImage = function(image) {
        return base64DataURLToArrayBuffer(image)
      }
      opts.getSize = function() {
        return [480, 300]
      }
 
      const imageModule = new ImageModule(opts)
 
      const zip = new PizZip(content)
      const doc = new Docxtemplater().loadZip(zip)
      doc.attachModule(imageModule)
      const obj = { ...data, ...res }
      // console.log(obj)
      doc.setData({ ...obj })
      try {
        // render the document (replace all occurences of {first_name} by John, {last_name} by Doe, ...)
        doc.render()
      } catch (error) {
        const e = {
          message: error.message,
          name: error.name,
          stack: error.stack,
          properties: error.properties
        }
        console.log({
          error: e
        })
        // The error thrown here contains additional information when logged with JSON.stringify (it contains a property object).
        throw error
      }
      const out = doc.getZip().generate({
        type: 'blob',
        mimeType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
      }) // Output the document using Data-URI
      saveAs(out, fileName)
    })
  })
}
 
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
}
 
function main(url, cb) {
  var image = new Image()
  image.crossOrigin = ''
  image.src = url
  image.onload = function() {
    base64Img = imageToBase64(image)
    cb && cb(base64Img)
  }
}