【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

相关推荐
xyq2024几秒前
Matplotlib 绘图线
开发语言
m0_694845574 分钟前
tinylisp 是什么?超轻量 Lisp 解释器编译与运行教程
服务器·开发语言·云计算·github·lisp
程序员猫哥_6 分钟前
HTML 生成网页工具推荐:从手写代码到 AI 自动生成网页的进化路径
前端·人工智能·html
龙飞057 分钟前
Systemd -systemctl - journalctl 速查表:服务管理 + 日志排障
linux·运维·前端·chrome·systemctl·journalctl
春日见9 分钟前
如何创建一个PR
运维·开发语言·windows·git·docker·容器
我爱加班、、12 分钟前
Websocket能携带token过去后端吗
前端·后端·websocket
AAA阿giao12 分钟前
从零拆解一个 React + TypeScript 的 TodoList:模块化、数据流与工程实践
前端·react.js·ui·typescript·前端框架
C++ 老炮儿的技术栈12 分钟前
VS2015 + Qt 实现图形化Hello World(详细步骤)
c语言·开发语言·c++·windows·qt
杨超越luckly18 分钟前
HTML应用指南:利用GET请求获取中国500强企业名单,揭秘企业增长、分化与转型的新常态
前端·数据库·html·可视化·中国500强
派葛穆19 分钟前
Python-批量安装依赖
开发语言·python