赞
踩
import JSZip from "jszip";
import FileSaver from "file-saver";
import domToImage from "dom-to-image";
import * as changedpi from "changedpi";
async toImage2(downFilelist) {
this.notify = this.$notify.success({
title: "提示",
message: "正在导出文件",
showClose: false,
duration: 0,
});
var node3;
var dataUrl;
var node2;
var dataUrl2;
let filelist = [];
let list = [];
for (let n = 0; n < downFilelist.length; n++) {
let e = downFilelist[n];
console.log(e, "=e111");
try {
const options = {
width: 1048, // 高度宽度自行设定
height: 674,
scale: 2, // 后面的字表示需要把一个像素转为几个像素
};
node3 = eval("this.$refs.imageDom" + n)[0];
dataUrl = await domToImage.toPng(node3, { ...options });
dataUrl = changedpi.changeDpiDataUrl(dataUrl, 300);
node2 = eval("this.$refs.imageDom2" + n)[0];
dataUrl2 = await domToImage.toPng(node2, { ...options });
dataUrl2 = changedpi.changeDpiDataUrl(dataUrl2, 300);
list.push(
{
fileDownUrl: dataUrl,
fileDownName: e.school_name + "_" + e.name + "正面",
},
{
fileDownUrl: dataUrl2,
fileDownName: e.school_name + "_" + e.name + "反面",
}
);
if (n == downFilelist.length - 1) {
this.downFileZip(list);
}
} catch (e) {
throw new Error(e);
}
}
},
downFileZip(base64Images) {
const zip = new JSZip();
const imgFolder = zip.folder("");
console.log(base64Images, "=base64Images");
base64Images.forEach((base64, index) => {
console.log(base64.fileDownName, "=fileDownName");
// 提取base64的MIME类型
const mimeType = base64.fileDownUrl.split(";")[0].split(":")[1];
// 提取base64的数据
const data = base64.fileDownUrl.split(",")[1];
// 将base64数据转换为二进制字符串
const binaryString = atob(data);
// 将二进制字符串转换为Uint8Array
const uintArray = new Uint8Array(binaryString.length);
for (let i = 0; i < binaryString.length; i++) {
uintArray[i] = binaryString.charCodeAt(i);
}
// 添加图片到压缩文件夹; `${base64.fileDownName}.${mimeType.split("/")[1]}`,
imgFolder.file(
`${base64.fileDownName}.${mimeType.split("/")[1]}`,
uintArray,
{
binary: true,
}
);
});
let that = this;
// 生成压缩文件
zip.generateAsync({ type: "blob" }).then(function (content) {
// 使用FileSaver保存文件
FileSaver(content, "压缩包名称.zip");
that.$notify.success({
title: "提示",
message: "导出成功",
showClose: false,
});
that.notify.close();
});
},
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。