vue3实现base64转pdf文件

<!DOCTYPE html
  PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>base64转pdf</title>
  <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
  <style>
    iframe {
      height: 800px;
      width: 800px
    }
  </style>

<body>

  <iframe></iframe>
</body>
<script>
  let data = 'base64字符'
  $(function () {
    //获取到了blob对象
    let blob = base64ToBlob(data);
    //获取当前url,直接放到iframe就能用,下载同理
    let blobURL = window.URL.createObjectURL(blob);
    $('iframe')[0].src = blobURL;
  })
  function base64ToBlob(code) {
    //Base64一行不能超过76字符,超过则添加回车换行符。因此需要把base64字段中的换行符,回车符给去掉,有时候因为存在需要把加号空格之类的换回来,取决于base64存取时的规则。
    code = code.replace(/[\n\r]/g, '');
    var raw = window.atob(code);
    let rawLength = raw.length;
    //转换成pdf.js能直接解析的Uint8Array类型
    let uInt8Array = new Uint8Array(rawLength);
    for (let i = 0; i < rawLength; ++i) {
      uInt8Array[i] = raw.charCodeAt(i);
    }
    return new Blob([uInt8Array], { type: 'application/pdf' });//转成pdf类型
  }
</script>
</head>

</html>

你可能感兴趣的:(pdf)