html2Canvas+JsPDF 导出pdf 无法显示网络图片

html2Canvas+JsPDF 导出pdf

问题:类似于下面着这种网络图片使用img导出的时候是空白的

https://gimg3.baidu.com/search/src=http%3A%2F%2Fpics4.baidu.com%2Ffeed%2F7e3e6709c93d70cf827fb2fda054500cb8a12bc9.jpeg%40f_auto%3Ftoken%3Dd97d3f0fd06e680e592584f8c7a24ac8\&refer=http%3A%2F%2Fwww.baidu.com\&app=2021\&size=f360,240\&n=0\&g=0n\&q=75\&fmt=auto?sec=1690045200\&t=e1a88c44b1b9907677400a079766e0bf

解决:将网络图片转成base64

复制代码
<img :src="aaa" style="width:200px" alt="">

转base64
let res = await this.getBase64Image('https://gimg3.baidu.com/search/src=http%3A%2F%2Fpics4.baidu.com%2Ffeed%2F7e3e6709c93d70cf827fb2fda054500cb8a12bc9.jpeg%40f_auto%3Ftoken%3Dd97d3f0fd06e680e592584f8c7a24ac8&refer=http%3A%2F%2Fwww.baidu.com&app=2021&size=f360,240&n=0&g=0n&q=75&fmt=auto?sec=1690045200&t=e1a88c44b1b9907677400a079766e0bf')
this.aaa = res

getBase64Image(src) {
      return new Promise(resolve => {
        const img = new Image()
        img.crossOrigin = ''
        img.src = src
        img.onload = function() {
          const canvas = document.createElement('canvas')
          canvas.width = img.width
          canvas.height = img.height
          const ctx = canvas.getContext('2d')
          ctx && ctx.drawImage(img, 0, 0, img.width, img.height)
          const ext = img.src.substring(img.src.lastIndexOf('.') + 1).toLowerCase()
          const dataURL = canvas.toDataURL('image/' + ext)
          resolve(dataURL)
        }
      })
    },

导出

相关推荐
非凡ghost12 分钟前
批量转双层PDF(可识别各种语言)
windows·学习·pdf·软件需求
乐迁~1 小时前
前端PDF导出完全指南:JSPDF与HTML2Canvas深度解析与实战(上)
前端·pdf
月巴月巴白勺合鸟月半19 小时前
PDF转图片的另外一种方法
pdf·c#
多则惑少则明19 小时前
AI大模型综合(四)langchain4j 解析PDF文档
pdf·springboot·大语言模型
m5655bj19 小时前
使用 C# 对比两个 PDF 文档的差异
pdf·c#·visual studio
WXDcsdn20 小时前
Windows无法使用Microsoft to PDF输出PDF文件
windows·pdf·电脑·it运维
Yqlqlql21 小时前
基于 Python+PySide6 开发的本地复合文件工具:图片转 PDF+PDF 转 Word 双功能
pdf
softshow10261 天前
html2canvas + jspdf实现页面导出成pdf
开发语言·javascript·pdf
木叶子---1 天前
pdf生成排查记录与解决方案
java·pdf
开开心心_Every1 天前
文件数量统计工具:支持多层文件夹数量统计
游戏·微信·pdf·excel·语音识别·swift·lisp