// 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: [],
|
fileListThree: [],
|
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 })
|
} 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)
|
}
|
}
|