vue将base64编码转为pdf方法

html 复制代码
<iframe width="100%" height="100%" src="" frameborder="0" id="iframe"></iframe>
html 复制代码
使用方法:
直接调用就行
viewPdf('传入base64编码即可')
clike 复制代码
  //content是base64编码格式
  const viewPdf =(content:any)=> {
    const blob = base64ToBlob(content);
    // @ts-ignore
    if (window.navigator && window.navigator.msSaveOrOpenBlob) {
      // @ts-ignore
      window.navigator.msSaveOrOpenBlob(blob);
    } else {
      let iframe = document.getElementById("iframe")
      if(iframe){
        //我这里是用iframe嵌入的 
        // @ts-ignore
        iframe.src= URL.createObjectURL(blob);
      }
      // window.open(fileURL);// 打开ppf文件(如果不用iframe嵌入 打开新的页面 就可用window.open来打开)
    }
  }
  // 2.base转二进制文件流
  const base64ToBlob = (code:any)=> {
    code = code.replace(/[\n\r]/g, '');// 检查base64字符串是否符合base64编码
    // atob() 方法用于解码使用 base-64 编码的字符串。
    const raw = window.atob(code);
    const rawLength = raw.length;
    const uInt8Array = new Uint8Array(rawLength);
    for (let i = 0; i < rawLength; ++i) {
      // 将解码后的逐个字符转换成Unicode序号,放入Unit8Array数组
      uInt8Array[i] = raw.charCodeAt(i);
    }
    // 通过Blob将Uint8Array数组转换成pdf类型的文件对象
    return new Blob([uInt8Array], { type: 'application/pdf' });
  }
相关推荐
鱼钓猫2 分钟前
H5 电子签名组件
vue.js·canvas
专注VB编程开发20年9 分钟前
rust语言-对象多级访问
服务器·前端·rust
徐_三岁18 分钟前
关于npm的钩子函数
前端·npm·node.js
代码小学僧19 分钟前
🎉 在 Tailwind 中愉快的使用 Antd Design 色彩
前端·css·react.js
ssshooter23 分钟前
复习 CSS Flex 和 Grid 布局
前端·css·html
_请输入用户名1 小时前
EventEmitter 是广播,Tapable 是流水线:聊聊它们的本质区别
前端·设计模式
爱学习的茄子1 小时前
React Fiber:让大型应用告别卡顿的性能革命
前端·react.js·面试
龙在天1 小时前
我是前端,我来总结一下前端 配 Nginx 的一些案例
前端
Thetimezipsby1 小时前
基于Taro4打造的一款最新版微信小程序、H5的多端开发简单模板
前端·javascript·微信小程序·typescript·html5·taro
掘金安东尼1 小时前
前端周刊430期(2025年9月1日–9月7日)
前端