pdf 的预览

88 阅读1分钟

url 格式预览

可以 window.open 直接打开

当需要当前页面预览显示时:

embed 太古时期标签,不推荐

<embed
 type="application/pdf"
 :src="pdfUrl"
 width="800"
 height="600" />

iframe

<iframe
 :src="pdfUrl"
 width="800"
 height="600" />

embed 和 iframe 缺点是: 没办法阻止用户打印和下载pdf

base64格式的pdf预览

//content就是上面的那串base64格式的pdf
viewPdf (content) {
  if (!content) {
    console.log(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, '')
  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' })
}