vue生成海报
1、引入插件
npm install qrcodejs2 --save
npm install html2canvas --save
//在需要的地方引入
import VueQrcode from '@xkeshi/vue-qrcode'
import html2canvas from 'html2canvas';
2、html海报中背景图片,这里用的是本地路径,如果是用线上路径生成的海报背景图空白,也不能用backgroud:(url)
<div class="backImg" id="posterHtml">
//海报中背景图片,这里用的是本地路径,如果是用线上路径生成的海报背景图空白,也不能用backgroud:(url)
<img style="width: 312px;height: 411px;" src="@/views/assets/images/shoukuan.jpg" alt="">
<div class="title">{{tenantName}}</div>
<div class="erweima">
<qrcode :value="xiazaiUrl" :options="xiazaiOption"/>
</div>
</div>
3、js
posterImgMounted(){
let that = this
let domObj = document.getElementById('posterHtml')
//延迟加载
setTimeout(function (){
html2canvas(domObj, {
width: domObj.clientWidth, //dom 原始宽度
height: domObj.clientHeight,
useCORS: true,
allowTaint: true,
onclone (doc) {
let e = doc.querySelector('#posterHtml')
e.style.display = 'block'
}
}).then(canvas => {
that.posterImg = canvas.toDataURL('image/png')
//js本地下载生成的base64格式的图片,下篇介绍
that.downLoad(that.posterImg)
})
},1000)
},
生成了base64格式的海报图,下篇介绍如何js如何下载