使用html2canvas生成图片
// 使用html2canvas生成图片
vue3 里面用ref const canvas = await html2canvas(element, { scale: 2, backgroundColor: null, width: 375, logging: true, useCORS: true }); const dom = document.body.appendChild(canvas); document.body.removeChild(dom); const file = dataURLtoFile(dom.toDataURL('image/png', 1), `${route.query.cashierName}.png`); 转成file格式 dom.toDataURL('image/png', 1)生成base64格式 // 将base64转换为文件,dataurl为base64字符串,filename为文件名(必须带后缀名,如.jpg,.png) function dataURLtoFile(dataurl, filename) { const arr = dataurl.split(',') const mime = arr[0].match(/:(.*?);/u)[1] const bstr = atob(arr[1]) let n = bstr.length const u8arr = new Uint8Array(n) while (n--) { u8arr[n] = bstr.charCodeAt(n) }const blob = new Blob([u8arr], { type: mime }) return new File([blob], filename, { type: mime, lastModified: new Date().getTime() }) }

浙公网安备 33010602011771号