Vue~在线预览doc、docx、pdf、img文件

简介: Vue~在线预览doc、docx、pdf、img文件

Vue~在线预览doc、docx、pdf、img文件

1、导入控件

//预览docx


npm install docx-preview --save

npm install jszip --save


//图片缩放空控件


npm install vue-photo-preview --save


2、创建组件

<template>
  <!--  图片、pdf、docx 预览
          "docx-preview": "^0.1.4",
          "jszip": "^3.10.0",-->
  <div>
    <a-modal title="文件预览" :visible="showDoc || showPdf || showImg" :maskClosable="false" @cancel="cancel"
             width="750px">
      <template slot="closeIcon">
        <my-icon icon="close-circle" class="closeIcon"/>
      </template>
      <template slot="footer">
        <div v-if="showPdf" class="pdf-layout-page">
          <my-button @click="changePdfPage(0)" :disabled="currentPage===1" name="上一页"/>
          {{currentPage}} / {{pageCount}}
          <my-button @click="changePdfPage(1)" :disabled="currentPage===pageCount" name="下一页"/>
        </div>
        <my-button name="取消" @click="cancel"/>
      </template>
      <div class="modal-body form">
        <div v-if="showImg">
          <img :src="images" preview="1" preview-text="" style="width:100%"/>
        </div>
        <div v-show="showDoc" ref="word">
          <iframe v-if="fileUrl" frameborder="0"
                  :src="fileUrl"
                  width='100%'
                  height="100%">
          </iframe>
        </div>
        <div v-show="showPdf" class="pdf-layout" id="top">
          <pdf-view
            ref="pdf"
            :src="pdfPath"
            :page="currentPage"
            @num-pages="pageCount=$event"
            @page-loaded="currentPage=$event"
            @loaded="loadPdfHandler"/>
        </div>
      </div>
    </a-modal>
  </div>
</template>
<script>
  import pdfView from 'vue-pdf'
  import axios from 'axios'
  const docxPre = require('docx-preview')
  window.JSZip = require('jszip')
  export default {
    name: 'FilePreview',
    components: { pdfView },
    data() {
      return {
        showDoc: false,//判断如果是否为word文件显示
        showPdf: false,//判断如果是否为pdf文件显示
        showImg: false,//判断如果是否为图片显示
        fileUrl: '',//pdf链接
        images: '',//图片链接
        currentPage: 0, // pdf文件页码
        pageCount: 0, // pdf文件总页数
      }
    },
    methods: {
      showView(filePath) {
        let that = this
         let type = filePath.split('.')[filePath.split('.').length - 1]
        if (type === 'jpg' || type === 'png' || type === 'jpeg') {
          that.images = filePath
          that.showImg = true
        } else if (type === 'pdf') {
          that.loadPdfHandler()//重置pdf第一页展示
          that.pdfPath = filePath
          that.showPdf = true
        } else if (type === 'doc') {//word预览 
          that.fileUrl = 'https://view.officeapps.live.com/op/view.aspx?src=' + filePath
          that.showDoc = true
        } else if (type === 'docx') {//word预览
          that.showDoc = true
          that.previewWord(filePath)
        }
      },
      // 后端返回二进制流
      previewWord(filePath) {
        let that = this
        // 这里需要提起打开弹窗,因为有时很找不到word的ref 会报错
        axios({
          method: 'get',
          responseType: 'blob', // 因为是流文件,所以要指定blob类型
          url: filePath  
        }).then(({ data }) => {
          docxPre.renderAsync(data, this.$refs.word)
        })
      },
      //pdf上一页下一页操作
      changePdfPage(val) { 
        if (val === 0 && this.currentPage > 1) {
          this.currentPage-- 
        }
        if (val === 1 && this.currentPage < this.pageCount) {
          this.currentPage++
          this.top()
        }
      },
      top() {
        document.querySelector('#top').scrollIntoView(true)
      },
      // pdf加载时
      loadPdfHandler(e) {
        this.currentPage = 1 // 加载的时候先加载第一页
      },
      cancel() {
        this.showDoc = false//判断如果是否为word文件显示
        this.showPdf = false//判断如果是否为pdf文件显示
        this.showImg = false//判断如果是否为图片显示
      }
    }
  }
</script>
<style lang="less" scoped>
  .form {
    height: 600px;
    overflow: auto;
  } 
 .pdf-layout-page {
    left: 30%;
    margin: auto;
    display: inline-block;
    text-align: center;
    font-size: 14px;
    position: absolute
  }
</style>

3、使用组件

<template> 
 <div>
    <file-preview ref="filePreview"/> 
  </div>
</template>
<script>
//组件中引入 
import FilePreview from '@/components/FilePreview/index'
 export default {
    components: { FilePreview  },
     methods: {
      lookFile() {
        this.$refs.filePreview.showView(fileUrl)
      }
  }
} 
</script>

docx-preview组件在解析doc文件会失败,docx可以;没搞清楚是为什么


注:示例中使用的ui框架是 Ant Design Vue ,使用的控件可替换成相应项目的ui框架


antui框架官网

https://1x.antdv.com/docs/vue/introduce-cn/


目录
相关文章
|
26天前
|
机器学习/深度学习 人工智能 文字识别
Zerox:AI驱动的万能OCR工具,精准识别复杂布局并输出Markdown格式,支持PDF、DOCX、图片等多种文件格式
Zerox 是一款开源的本地化高精度OCR工具,基于GPT-4o-mini模型,支持PDF、DOCX、图片等多种格式文件,能够零样本识别复杂布局文档,输出Markdown格式结果。
100 4
Zerox:AI驱动的万能OCR工具,精准识别复杂布局并输出Markdown格式,支持PDF、DOCX、图片等多种文件格式
|
2月前
|
数据挖掘 BI
.net8 Syncfusion生成pdf/doc/xls/ppt最新版本
通过使用 Syncfusion,您可以高效地生成各种文档,满足不同的业务需求。这些工具不仅易于使用,还具有高性能和高度可扩展性,是处理文档的理想选择。
102 16
|
4月前
|
JSON 数据格式
LangChain-20 Document Loader 文件加载 加载MD DOCX EXCEL PPT PDF HTML JSON 等多种文件格式 后续可通过FAISS向量化 增强检索
LangChain-20 Document Loader 文件加载 加载MD DOCX EXCEL PPT PDF HTML JSON 等多种文件格式 后续可通过FAISS向量化 增强检索
284 2
|
4月前
|
资源调度 前端开发 JavaScript
安利一款基于canvas/svg的富文本编辑器-支持在线导出PDF、DOCX
高性能:利用Canvas和SVG进行图形和矢量图形的渲染,提供高性能的绘图能力。 可扩展性:Canvas-Editor是一个开源项目,支持通过插件机制扩展编辑器的功能,如DOCX、PDF导出、表格分页等。 丰富的文本编辑功能:支持多种文本编辑操作,如插入表格、分页、性能优化等。
507 0
|
4月前
|
JSON 缓存 JavaScript
vue尚品汇商城项目-day01【1.vue-cli脚手架初始化项目生成文件的介绍】
vue尚品汇商城项目-day01【1.vue-cli脚手架初始化项目生成文件的介绍】
51 0
|
6月前
|
JavaScript 容器
Vue学习之--------组件的基本使用(非单文件组件)(代码实现)(2022/7/22)
这篇文章讲解了Vue中组件的基本使用方法,包括组件的定义、注册和使用过程,并通过代码实例演示了非单文件组件的创建和使用,同时指出了一些使用组件时的注意事项。
Vue学习之--------组件的基本使用(非单文件组件)(代码实现)(2022/7/22)
|
6月前
|
JavaScript
Vue————Vue v2.7.14 入口文件【二】
Vue————Vue v2.7.14 入口文件【二】
76 0
|
7月前
|
JavaScript
vue项目打包后自动压缩成zip文件
vue项目打包后自动压缩成zip文件
482 0
|
7月前
|
移动开发 小程序 前端开发
uniap开发微信小程序如何在线预览pdf文件
这是一段关于在线预览和处理PDF的多方案说明,包括使用JavaScript库PDF.js(如`pdfh5.js`)实现H5页面预览,提供QQ群和技术博客链接以获取帮助和支持。还介绍了两个适用于Uni-app的插件,一个用于H5、小程序和App中的PDF预览和下载,另一个专门解决手机端PDF预览问题。此外,还详细描述了在Uni-app中使用微信小程序API`wx.openDocument`显示PDF的步骤,包括上传文件、配置权限和编写代码。
|
8月前
|
JavaScript
vue : 无法将“vue”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。
vue : 无法将“vue”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。

热门文章

最新文章