一、创建模板
所有要插入动态值得地方格式都是:{参数}
1.标题
标题可以在word中自定义好字体样式
[站外图片上传中...(image-adba6b-1695354318560)]
2.表格
表格以{#table}开始,以{/table}结束
[站外图片上传中...(image-84b461-1695354318560)]
3.图片
一张图片{%image},图片数组{#images}{src}{/images}
[站外图片上传中...(image-4dced0-1695354318560)]
4.模板路径
[站外图片上传中...(image-2d5fb7-1695354318560)]
二、下载插件并引入
import JSZipUtils from 'jszip-utils';
import PizZip from 'pizzip';
import docxtemplater from 'docxtemplater'; //导出word文档的插件
import { saveAs } from 'file-saver';
import ImageModule from 'docxtemplater-image-module-free'; //导出图片才需要这个插件
三、示例
<template>
<div class="">
</div>
</template>
<script>
import JSZipUtils from 'jszip-utils';
import PizZip from 'pizzip';
import docxtemplater from 'docxtemplater'; //导出word文档的插件
import { saveAs } from 'file-saver';
import ImageModule from 'docxtemplater-image-module-free'; //导出图片才需要这个插件
export default {
name: "",
data() {
return {
};
},
props: {
},
mounted() {
},
methods: {
exportWord(baseInfo) {
let that = this;
let doc = null;
JSZipUtils.getBinaryContent('/template/项目任务书.docx', (error, content) => {
const zip = new PizZip(content);
doc = new docxtemplater();
doc.loadZip(zip);
// 图片处理
let opts = {}
opts.centered = true; // 图片居中,在word模板中定义方式为{%src}
opts.fileType = "docx";
opts.getImage = function (chartId) {
return that.base64DataURLToArrayBuffer(chartId)
}
opts.getSize = function () {
return [200, 200];//[宽, 高]
}
let imageModule = new ImageModule(opts);
doc.attachModule(imageModule);
const dataUrl = [];
// 本地图片放在静态文件夹下,不然找不到
that.UrlToBase64('https://img1.baidu.com/it/u=3549522664,2004387788&fm=253&fmt=auto&app=120&f=JPEG?w=1422&h=800', (imgUrl) => {
dataUrl.push({
src: imgUrl
})
})
//处理图片 结束
setTimeout(() => {
doc.setData({
...baseInfo,
// image:dataUrl[0].src,//单张图片是用image
images: dataUrl, //批量图片 模板里复杂一些 {#images} {%image} {/images}
}); //baseInfo为导入表中的变量,键值都可以自己定义
try {
doc.render();
} catch (error) {
const e = {
message: error.message,
name: error.name,
stack: error.stack,
properties: error.properties
};
throw e;
}
const out = doc.getZip().generate({
type: 'blob',
mimeType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
});
saveAs(out, baseInfo.name + '-任务书-' + baseInfo.bookCreatime + '.docx'); //导出的word文档名字
}, 1000)
});
},
//图片转base64地址
UrlToBase64(url, callback) {
let image = new Image();
//解决跨域问题
image.setAttribute('crossOrigin', 'anonymous');
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; //调用回调函数
}
},
//官网提供处理图片的方法
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;
}
}
};
</script>