引入html2canvas插件,传入需要打印地方的dom,以及需要的相关配置(以下代码仅供参考)
<template>
<div class="download">
<el-button
type="primary"
icon="el-icon-download"
@click="fileDownload"
>{{content}}</el-button>
</div>
</template>
<script>
import html2canvas from "html2canvas";
export default {
data() {
return {
downloadUrl:''
}
},
props: {
container: {
default:()=>{}
},
content: {
type: String,
default:''
},
title: {
type: String,
default: ''
}
},
methods: {
convertToImage(container, options = {}) {
const scale = window.devicePixelRatio;
const width = container.offsetWidth;
const height = container.offsetHeight;
const ops = {
scale,
width,
height,
useCORS: true,
allowTaint: false,
...options
};
return html2canvas(container, ops).then(canvas => {
return canvas.toDataURL("image/png");
});
},
async fileDownload() {
this.downloadUrl = await this.convertToImage(this.container)
let aLink = document.createElement("a");
aLink.style.display = "none";
aLink.href = this.downloadUrl;
aLink.download = `${this.title}.png`;
document.body.appendChild(aLink);
aLink.click();
document.body.removeChild(aLink);
}
},
}
</script>
html2canvas 配置参数