此项目以vue为例,引用方式是
cnpm install html2canvas -s
import html2canvas from 'html2canvas'
不是 vue 的可以直接引用 js 文件,不多说话,直接上代码(注释都在里面了)
printSeat() {
this.$store.commit("updatePageLoading", ++this.$store.state.pageLoading)
document.querySelectorAll('#chartList')[0].style.top = 0;
document.querySelectorAll('#chartList')[0].style.left = 0;
try {
document.querySelectorAll('.group-area-right')[0].style.right = '180px';
} catch (error) {
}
try {
document.querySelectorAll('.group-area-left')[0].style.left = '180px';
} catch (error) {
}
try {
document.querySelectorAll('.area-class')[0].style.left = '180px';
} catch (error) {
}
let canvas2 = document.createElement("canvas");
let _canvas = document.querySelectorAll('#seatPrint')[0];
let w = parseInt(window.getComputedStyle(_canvas).width);
let h = parseInt(window.getComputedStyle(_canvas).height);
canvas2.width = w * 1.9;
canvas2.height = h * 1.6;
let context = canvas2.getContext("2d");
context.scale(2, 2);
document.querySelectorAll('#seatPrint')[0].style.transform = 'scale(.8)';
document.querySelectorAll('#seatPrint')[0].style.marginLeft = '-10%';
document.querySelectorAll('#seatPrint')[0].style.width = '120%';
let that = this
html2canvas(document.querySelectorAll('#seatPrint')[0], { canvas: canvas2 }).then((canvas) => {
const blob = this.convertBase64UrlToBlob(canvas.toDataURL());
const fileName = this.$route.query.planName + '座次图'
if (window.navigator.msSaveOrOpenBlob) {
navigator.msSaveBlob(blob, fileName);
} else {
const link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = fileName;
document.body.appendChild(link);
const evt = document.createEvent("MouseEvents");
evt.initEvent("click", false, false);
link.dispatchEvent(evt);
document.body.removeChild(link);
setTimeout(() => {
this.$store.commit("updatePageLoading", --this.$store.state.pageLoading)
document.querySelectorAll('#seatPrint')[0].style.transform = 'scale(1)';
document.querySelectorAll('#seatPrint')[0].style.marginLeft = 0;
document.querySelectorAll('#seatPrint')[0].style.width = '100%';
try {
document.querySelectorAll('.group-area-right')[0].style.right = '30px';
} catch (error) {
}
try {
document.querySelectorAll('.group-area-left')[0].style.left = '30px';
} catch (error) {
}
try {
document.querySelectorAll('.area-class')[0].style.left = '0px';
} catch (error) {
}
}, 100)
}
}).catch(err => {
this.$store.commit("updatePageLoading", --this.$store.state.pageLoading)
});
convertBase64UrlToBlob(base64) {
let parts = base64.split(";base64,")
let contentType = parts[0].split(":")[1]
let raw = window.atob(parts[1])
let rawLength = raw.length
let uInt8Array = new Uint8Array(rawLength)
for (let i = 0
uInt8Array[i] = raw.charCodeAt(i)
}
return new Blob([uInt8Array], { type: contentType })
}
在中间遇到了一个奇怪的问题,就是chrome浏览器生成图片大约要3秒钟左右,而火狐浏览器能加载一分钟,提示显示

刚开始以为打印的dom元素比较多,然后就尝试了打印部分dom,都没有问题,只要已使用外层的dom就会提示,后来我改了dom元素的层级结构,然后就快了很多,中间尝试过直接后端生成,但是我觉得前端应该也没问题,网上查了说chrome 和 火狐浏览器都挺好用的,然后就开始改层级了