html2canvas将dom 转换成图片并上传

 /// .pdf  dom元素
let node = document.querySelector('.pdf')
//解决图片灰色阴影偏移问题
let menuitemWidth = document.querySelector('.theme-dark').offsetWidth
node.style.cssText = `margin-left:-${menuitemWidth}px;margin-top:-90px`
this.$nextTick(() => {
  html2canvas(node, {
    useCORS: true,
    height: node.offsetHeight,
    width: node.offsetWidth,
    height: node.scrollHeight,
    windowHeight: node.scrollHeight,
    scrollY: 0,
    scrollX: 0
  }).then(async (canvas) => {
    //this.showButton = true
    node.style.cssText = 'height:unset;margin:unset'
    var url = canvas.toDataURL(); //图片地址
    let formData = new FormData();
    // this.dataURLtoFile 方法 => 转换为文件流
    formData.append('file', this.dataURLtoFile(url, '周报'))
    formData.append("kanBanName", this.title);
    formData.append("historyId", this.kanbanHistoryInfoId);
    // this.reportFormLoading = true
    uploadFile(formData).then((res) => {
    
    }).catch((err) => {
      console.log(err);
    })
    // let oImg = new Image();
    // oImg.src = canvas.toDataURL(); // 导出图片
    // document.body.appendChild(oImg); // 将生成的图片添加到body
  }).catch((err)=>{
    console.log('err1',err);
  })
})

dataURLtoFile(dataurl, filename) {
 // 获取到base64编码
 const arr = dataurl.split(',')
 // 将base64编码转为字符串
 const bstr = window.atob(arr[1])
 let n = bstr.length
 const u8arr = new Uint8Array(n) // 创建初始化为0的,包含length个元素的无符号整型数组
 while (n--) {
   u8arr[n] = bstr.charCodeAt(n)
 }
 return new File([u8arr], filename, {
   type: 'image/png'
 })
},
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值