【pdf】自定义组件:预览指定地址的PDF文件

pdf.vue

html 复制代码
<template>
  <iframe
    ref="iframe"
    @load="load_iframe"
    :src="src"
    frameborder="no"
    scrolling="no"
    style="width: 100%; height: 100%"
  />
</template>

<script>
export default {
  data() {
    return {
      src: ``,
      pdfUrl: ``,
      filename: ``,
      keyword: ``,
      keywords: ``,
      spreadMode: 1, //默认[双页模式: 1]
    };
  },
  props: ["data"],
  watch: {
    data: {
      handler(newValue, oldValue) {
        //console.log(`深度监听${this.$options.name}:`, newValue, oldValue);
        if (Object.keys(newValue || {}).length) {
          this.form = JSON.parse(JSON.stringify(newValue));
          this.$g.convertForm2ComponentParam(`disabled`, this);
          this.$g.convertForm2ComponentParam(`pdfUrl`, this);
          this.$g.convertForm2ComponentParam(`filename`, this);
          this.$g.convertForm2ComponentParam(`keyword`, this);
          this.$g.convertForm2ComponentParam(`keywords`, this);
          this.$g.convertForm2ComponentParam(`spreadMode`, this);
          this.init();
        }
      },
      deep: true, //深度监听
      immediate: true, //立即执行
    },
  },

  methods: {
    reload({ pdfUrl, filename, keyword, keywords } = {}) {
      this.src = "";
      this.$nextTick(() => {
        this.init({
          pdfUrl,
          filename,
          keyword,
          keywords,
        });
      });
    },
    //初始化
    init({
      pdfUrl = this.pdfUrl,
      filename = this.filename || this.$route.query.filename,
      keyword = this.keyword || this.$route.query.keyword,
      keywords = this.keywords || this.$route.query.keywords,
    } = {}) {
      // console.log("打印文件路径", pdfUrl);
      this.src = this.$g.getPDFsrc({
        file: pdfUrl,
        filename,
        keyword,
        keywords,
      });
    },
    // PDF预览■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
    load_iframe(d) {
      // iframe加载完成后等待一秒再对PDF进行操作,否则可能没有加载完PDF相关组件无法操作----------------------------------------
      setTimeout(() => {
        this.$nextTick(() => {
          this.PDFViewerApplication = this.getPDFViewerApplication();
          this.pdfSidebar({ command: `open` }); //打开左侧侧边栏
          this.pdfSpreadMode({ spreadMode: this.spreadMode }); //回显单双页模式
        });
      }, 1000);
    },
    // 获取PDF控制器
    getPDFViewerApplication() {
      let PDFViewerApplication;
      if (this.$refs.iframe && this.$refs.iframe.contentWindow) {
        PDFViewerApplication = this.$refs.iframe.contentWindow.PDFViewerApplication;
      }
      return PDFViewerApplication;
    },
    // 跳转到封面
    // 跳转到目录
    jumpPage({
      PDFViewerApplication = this.getPDFViewerApplication(),
      currentPageNumber = 1,
    } = {}) {
      PDFViewerApplication.pdfViewer.currentPageNumber = currentPageNumber;
    },
    // 单双页模式
    pdfSpreadMode({
      PDFViewerApplication = this.getPDFViewerApplication(),
      spreadMode = 1,
    } = {}) {
      /* spreadMode:
      单页模式: 0,
      双页模式: 1,
      书籍模式: 2 */
      PDFViewerApplication && (PDFViewerApplication.pdfViewer.spreadMode = spreadMode);
    },
    // 打开侧边栏
    pdfSidebar({
      PDFViewerApplication = this.getPDFViewerApplication(),
      command = `open`,
    } = {}) {
      PDFViewerApplication && PDFViewerApplication.pdfSidebar[command]();
    },

    // 适合页面
    pdf_page_fit(d) {
      let scaleSelect = this.$refs.iframe.contentDocument.querySelector(`#scaleSelect`);
      scaleSelect && (scaleSelect.value = `page-fit`);
    },
    // ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
    findPrevious(d) {
      let findPrevious = this.$refs.iframe.contentDocument.querySelector(`#findPrevious`);
      findPrevious && findPrevious.click();
    },
    findNext(d) {
      let findNext = this.$refs.iframe.contentDocument.querySelector(`#findNext`);
      findNext && findNext.click();
    },
  },
};
</script>

demo

html 复制代码
<pdf
    :data="{
        pdfUrl: `pdf绝对地址`,//必填
        filename: `pdf文件名`,//必填
    }"
/>

依赖方法:Vue实现预览PDF并且支持打印,不会出现乱码、拉升变形、打印预览被切割等弱智问题_vue-pdf 打印漏字-CSDN博客文章浏览阅读313次。该文章介绍如何在Vue项目中使用PDF.js库来渲染和查看PDF文件。首先,需要下载并引入PDF.js到项目的静态资源目录,然后在模板中通过iframe嵌入viewer.html,传入pdf文件的URL进行展示。代码示例展示了具体的实现方式。https://blog.csdn.net/qq_37860634/article/details/131035174使用PDF.js预览文件老是报错Message: file origin does not match viewer's_otherError @ app.js:1140怎么办??_message: file origin does not match viewer's-CSDN博客文章浏览阅读1.8k次,点赞2次,收藏5次。文章提供了针对PDF.js中因fileOrigin不等于viewerOrigin导致的错误的解决方案。用户需注释掉web/app.js或(如果是最新版本)web/viewer.js中的特定代码行以消除报错。https://blog.csdn.net/qq_37860634/article/details/131035028

相关推荐
無名路人4 分钟前
uniApp 小程序 vue3 app.vue静默登录其他页面等待登录完成方式二
前端·微信小程序·ai编程
CoCo的编程之路8 分钟前
2026 前端效能飞跃:深度解析智能助手的页面构建最大化方案
前端·人工智能·ai编程·智能编程助手·文心快码baiducomate
Dxy123931021614 分钟前
Python 去除 HTML 标签获取纯文本
开发语言·python·html
砚底藏山河32 分钟前
python、JavaScript 、JAVA,定制化数据服务,助力业务高效落地
java·javascript·python
JavaAgent架构师37 分钟前
前端AI工程化(一):AI通信协议深度解析
前端·人工智能
11_x39 分钟前
JS 底层:乖宝宝引擎和乖宝宝声明
javascript
洛的地理研学39 分钟前
Python下载并处理MOD13A3植被指数数据
开发语言·python
flex罗小黑39 分钟前
前端手机号脱敏的 4 个层级,你在第几层?
javascript
林恒smileZAZ40 分钟前
前端如何让图片、视频、pdf等文件在浏览器直接下载而非预览
前端·pdf
humcomm42 分钟前
Java 新特性2026年5月速览
java·开发语言