Vue は base64 ストリーム データを pdf ファイルに変換し、新しいページで開きます

いろいろな書き方を参考にして、やっとこの書き方にたどり着きました。

具体的な実装方法: vue に以下の 2 つのメソッドを記述し、viewPdf 関数を直接呼び出し、変換対象の base64 ストリーム データとしてパラメーターを渡します。

    viewPdf(content) {
      if (!content) {
        this.$message.error('暂无意见')
        return
      }
      const blob = this.base64ToBlob(content)
      if (window.navigator && window.navigator.msSaveOrOpenBlob) {
        window.navigator.msSaveOrOpenBlob(blob)
      } else {
        const fileURL = URL.createObjectURL(blob)
        window.open(fileURL)
      }
    },
    base64ToBlob(code) {
      code = code.replace(/[\n\r]/g, '')
      // atob() 方法用于解码使用 base-64 编码的字符串。
      const raw = window.atob(code)
      const rawLength = raw.length
      const uInt8Array = new Uint8Array(rawLength)
      for (let i = 0; i < rawLength; ++i) {
        uInt8Array[i] = raw.charCodeAt(i)
      }
      return new Blob([uInt8Array], { type: 'application/pdf' })
    },

おすすめ

転載: blog.csdn.net/luobo2345/article/details/122499168