Vue-纯前端导出word文档

var buf = doc.getZip()
.generate({type: ‘nodebuffer’});

// buf is a nodejs buffer, you can either write it to a file or do anything else with it.
fs.writeFileSync(path.resolve(__dirname, ‘output.docx’), buf);


#### jszip-utils


[jszip-utils]( ) 提供一个getBinaryContent(path, data)接口,path即是文件的路径,支持AJAX get请求,data为读取的文件内容。下面是官网给的例子。




// loading a file and add it in a zip file
JSZipUtils.getBinaryContent(“path/to/picture.png”, function (err, data) {
if(err) {
throw err; // or handle the error
}
var zip = new JSZip();
zip.file(“picture.png”, data, {binary:true});
});


#### file-saver


[file-saver]( )是一款适合在客户端生成文件的工具,它提供的接口saveAs(blob, "1.docx")将会使用到。


#### 安装


接下来就是安装以上工具




– 安装 docxtemplater
cnpm install docxtemplater pizzip --save

– 安装 jszip-utils
cnpm install jszip-utils -

你可能感兴趣的:(前端,vue.js,word)