【PDF.js】PDF文件预览

这篇具有很好参考价值的文章主要介绍了【PDF.js】PDF文件预览。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。


使用PDFJS实现pdf文件的预览,支持预览指定页、关键词搜索、缩略图、页面尺寸调整等等。

一、PDF.js

官方地址
文档地址

二、PDF.js 下载

1、下载PDF.js

下载地址

【PDF.js】PDF文件预览,javascript,pdf,开发语言,vue.js

2、在项目中引入

将下载的压缩包解压并放入到项目中的public文件夹下,我这里下载的是pdfjs-4.0.379-dist版本,如下
【PDF.js】PDF文件预览,javascript,pdf,开发语言,vue.js

3、屏蔽跨域错误

在 pdfjs-4.0.379-dist/web/viewer.mjs 内搜索 throw new Error(“file origin does not match viewer’s”) 并注释,如果不注释,可能会出现跨域错误,无法正常预览文件。
【PDF.js】PDF文件预览,javascript,pdf,开发语言,vue.js

三、项目中使用

内容区域结构(文件预览区域、滑块区域、问答区域)
滑块区域:滑动改变pdf文件预览区域的大小

<div
    v-if="filePreviewStore.getFilePreviewFlag"
    ref="resizeBox"
    class="resize"
    @mousedown="onResizeMouseDown"
  />
<el-main ref="mainContent" class="main-content">
  <!-- 文件预览区域 -->
  <div
    v-if="filePreviewStore.getFilePreviewFlag"
    class="preview-box"
    :style="{width: `${previewBoxWidth}px`}"
  >
    <!-- <PdfPreview v-if="['ppt', 'pptx', 'pdf'].includes(filePreviewStore.getFileType)" /> -->
    <PDF v-if="['ppt', 'pptx', 'pdf'].includes(filePreviewStore.getFileType)" />
    <ExcelPreview v-if="['xls', 'xlsx'].includes(filePreviewStore.getFileType)" />
    <WordPreview v-if="['doc', 'docx'].includes(filePreviewStore.getFileType)" />
    <TxtPreview v-if="filePreviewStore.getFileType === 'txt'" />
  </div>
  <div
    v-if="filePreviewStore.getFilePreviewFlag"
    ref="resizeBox"
    class="resize"
    @mousedown="onResizeMouseDown"
  />
  <!-- 问答区域 -->
  <div class="main_side3 flex1 column-flex">
    <div class="show_content flex1" ref="chatShowRef"> <ChatShow /> </div>
    <div class="chat">
      <askInput />
    </div>
  </div>
</el-main>

下面是PDF组件完整代码

<template>
  <div class="container">
    <iframe id="myIframe" :src="pdfUrl" width="100%" height="100%"></iframe>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref, watch } from 'vue'
import { useFilePreviewStore } from "@/stores";
import { fileRouteUrl } from "@/utils/fileRouteUrl"

const filePreviewStore = useFilePreviewStore()
const pdfUrl = ref('') // pdf文件地址
const fileUrl = '/static/dist/pdfjs-4.0.379-dist/web/viewer.html?file=' // pdfjs文件地址

onMounted(() => {
  // encodeURIComponent() 函数可把字符串作为 URI 组件进行编码。
  // 核心就是将 iframe 的 src 属性设置为 pdfjs 的地址,然后将 pdf 文件的地址作为参数传递给 pdfjs
  // 例如:http://localhost:8080/pdfjs-4.0.189-dist/web/viewer.html?file=http%3A%2F%2Flocalhost%3A8080%2Fpdf%2Ftest.pdf
  const url = filePreviewStore.getFileUrl.replace(fileRouteUrl, '/file')
  pdfUrl.value = fileUrl + encodeURIComponent(url) + `&page=${filePreviewStore.getPageNum}`
})

// 当文档页码修改时,重新预览当前页的文档内容
watch(() => filePreviewStore.getPageNum,
  (val) => {
    if (val) {
      // 页码修改时,需要重新保存记录文档页码,否则会出现点击与第一次相同的页码时,不会切换
      filePreviewStore.setFilePage(val)
      const pdfFrame = document.getElementById('myIframe').contentWindow
      // 传递参数,跳转到指定页
      pdfFrame.PDFViewerApplication.pdfViewer.scrollPageIntoView({
        pageNumber: val
      })
    }
  }
)

// 当预览的文件地址修改时,预览新的文档
watch(() => filePreviewStore.getFileUrl,
  (val) => {
    if (val) {
      // 服务器文档地址
      const pdfFileUrl = val.replace(fileRouteUrl, '/file');
      // 加载PDF文件
      pdfUrl.value = fileUrl + encodeURIComponent(pdfFileUrl) + `&page=${filePreviewStore.getPageNum}`
    }
  }
)

</script>

<style scoped lang="less">
.container {
  width: 100%;
  height: 100%;
  border: 1px solid #ccc;
  box-sizing: border-box;
  #myIframe {
    border: none;
  }
}
</style>

四、说明

1)在文件地址后面添加参数page(预览指定页)
【PDF.js】PDF文件预览,javascript,pdf,开发语言,vue.js
2)在 pdfjs-4.0.379-dist/web/viewer.mjs中的setInitialView方法中添加如下代码
【PDF.js】PDF文件预览,javascript,pdf,开发语言,vue.js
3)改变文件预览区域的宽度

// 修改左侧文件预览区域的宽度
const previewBoxWidth = ref(0)
const mainContent = ref()
const resizeBox = ref()
const mainContentWidth = ref(0)
const onResizeMouseDown = (e: MouseEvent) => {
  const startX = e.clientX
  resizeBox.value.left = resizeBox.value.offsetLeft
  // 解决预览pdf文档时,鼠标移入iframe后,无法捕获移动和抬起操作
  const myIframe = document.querySelector('iframe')
  myIframe && (myIframe.style['pointer-events'] = 'none')
  const onDocumentMouseMove = (e: MouseEvent) => {
    const endX = e.clientX
    const previewWidth = resizeBox.value.left + (endX - startX) - side1Width.value - 20
    // 文件预览区域宽度最小为内容区域的30%,最大为内容区域的70%
    if (previewWidth >= mainContentWidth.value * 0.7) {
      previewBoxWidth.value = mainContentWidth.value * 0.7
    } else if (previewWidth <= mainContentWidth.value * 0.3) {
      previewBoxWidth.value = mainContentWidth.value * 0.3
    } else {
      previewBoxWidth.value = previewWidth
    }
  }

  const onDocumentMouseUp = () => {
    myIframe && (myIframe.style['pointer-events'] = 'auto')
    document.removeEventListener('mousemove', onDocumentMouseMove)
    document.removeEventListener('mouseup', onDocumentMouseUp)
    resizeBox.value.releaseCapture && resizeBox.value.releaseCapture()
  }

  document.addEventListener('mousemove', onDocumentMouseMove)
  document.addEventListener('mouseup', onDocumentMouseUp)
  resizeBox.value.setCapture && resizeBox.value.setCapture()
}

// 
const { width } = useWindowSize() // 响应式获取窗口尺寸
// 当浏览器窗口尺寸改变时,重新修改设置文件预览区域的宽度
watch(() => width.value,
  (val) => {
    val && (previewBoxWidth.value = mainContentWidth.value * 0.7)
  }
)

// 获取内容区域的宽度
useResizeObserver(mainContent , (entries) => {
  const entry = entries[0]
  const { width } = entry.contentRect
  mainContentWidth.value = width
})

这里需要注意,因为在PDF组件中使用了iframe,当鼠标移入iframe区域时,无法捕获到鼠标的移动和抬起动作,会出现鼠标移出iframe区域后有可以改变该区域宽度,解决办法如下:
【PDF.js】PDF文件预览,javascript,pdf,开发语言,vue.js

五、实现效果

【PDF.js】PDF文件预览,javascript,pdf,开发语言,vue.js文章来源地址https://www.toymoban.com/news/detail-847399.html

到了这里,关于【PDF.js】PDF文件预览的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【vue2中的pdf预览】iframe/pdf.js/vue-pdf

    vue2中预览pdf的方法有pdf.js和vue-pdf等。下面进行简单对比使用方法的介绍。 使用iframe预览pdf 如果后端返回的不是url,那么需要使用 responseType = \\\'blob\\\' 来读取后端传来的内容。 扩展: 同样是使用iframe进行预览,如果有 其他格式 如word/xls/ppt/txt的文件需要预览,可以使用微软解

    2024年02月09日
    浏览(42)
  • vue前端预览pdf并加水印、ofd文件,控制打印、下载、另存,vue-pdf的使用方法以及在开发中所踩过的坑合集

    根据公司的实际项目需求,要求实现对pdf和ofd文件的预览,并且需要限制用户是否可以下载、打印、另存pdf、ofd文件,如果该用户可以打印、下载需要控制每个用户的下载次数以及可打印的次数。正常的预览pdf很简单,直接调用浏览器的预览就可以而且功能也比较全,但是一

    2024年02月16日
    浏览(131)
  • 利用PDF.js在微信小程序里预览PDF文件

    在微信小程序可以通过wx.downloadFile 和 wx.openDocument 两个api下载并打开pdf文件。这种方式主要有不少的缺点: 1、需要下载才可以查看,且每次打开都需要下载生成一个临时文件,如果PDF文件比较多的话,临时文件会越来越多,且如果PDF文件比较大的话,打开会比较慢。 2、在导

    2024年02月03日
    浏览(59)
  • 使用pdf.js预览pdf文件时如何兼容chrome66版本

    最近在做一个需求,在PC端实现预览pdf文件的功能,但是要最低兼容chrome的66版本,因为公司用的chrome浏览器最低版本就是66版本。 现在下载PDF.js (链接:https://mozilla.github.io/pdf.js/) 下载下来的版本是 v3.11.174 ,都是已经构建好的。 我先拿 v3.11.174 这个版本试了一下,发现在

    2024年02月07日
    浏览(42)
  • 前端js打开pdf文件--文件通过浏览器打开,以pdf形式进行预览

    通过点击button按钮,触发 @click=\\\"openPDF(performance_report)\\\"方法,把对应需要展示的pdf传送到openPDF()方法内,这里的pdf文件格式必须包括id、name、url。 在这里,performance_report为预览的文件:

    2024年02月14日
    浏览(40)
  • vue2 使用pdf.js 实现pdf预览,并可复制文本

            需求:pdf预览,并且可以选中pdf的内容进行复制。                 在ruoyi的vue前端项目中用到,参考了网上不少文章,因为大部分没给具体的pdf.js版本,导致运行过程中报各种api 错误,经过尝试以下版本可用,故记录一下:         安装依赖:         vue 页面

    2024年01月19日
    浏览(45)
  • js下载图片、pdf等文件,无预览

    直接使用window.open()或window.locat.href()下载文件遇到图片或pdf文件就会跳转预览页,不能满足我想要的点击直接下载文件到本地的需求,尝试多次,最终通过以下方法实现了我的需求。 鉴于后端返回的是文件路径,首先要将文件url地址转为文件对象,代码如下: npm install saveA

    2024年02月13日
    浏览(36)
  • js实现PDF 预览和文件下载

    在开发过程中要求对 PDF 类型的发票提供 预览 和 下载 功能, PDF 类型文件的来源又包括 H5 移动端 和 PC 端 ,而针对这两个不同端的处理会有些许不同,下文会有所提及。 针对 PDF 预览 的文章不在少数,但似乎都没有提及可能遇到的问题,或是提供对应的具体需求场景下如何

    2024年02月15日
    浏览(39)
  • vue或uniapp使用pdf.js预览

    一、先下载稳定版的pdf.js,可以去官网下载  官网下载地址  或  pdf.js包下载(已配置好,无需修改) 二、下载好的pdf.js文件放在public下静态文件里, uniapp是放在 static下静态文件里 三、使用方式    1. vue项目 注意路径  :src=\\\"`static/pdfjs-1.9/web/viewer.html?file=你的pdf路径  2.

    2024年02月13日
    浏览(28)
  • 【Vue】vue2使用pdfjs预览pdf文件,在线预览方式一,pdfjs文件包打开新窗口预览pdf文件

    【Vue】vue2预览显示quill富文本内容,vue-quill-editor回显页面,v-html回显富文本内容 【Vue】vue2项目使用swiper轮播图2023年8月21日实战保姆级教程 【Vue】vue2使用pdfjs预览pdf文件,在线预览方式一,pdfjs文件包打开新窗口预览pdf文件 提示:这里可以添加本文要记录的大概内容: vue

    2024年02月07日
    浏览(39)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包