前端使用插件预览pdf、docx、xlsx、pptx格式文件

2023-12-08 09:15

本文主要是介绍前端使用插件预览pdf、docx、xlsx、pptx格式文件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

PDF预览

H5页面pdf预览

插件:pdfh5
版本:“pdfh5”: “^1.4.7”

npm install pdfh5

import PdfH5 from "pdfh5";
import "pdfh5/css/pdfh5.css";// methods
this.$nextTick(() => {this.pdfH5 = new PdfH5("#pdf", {pdfurl: this.pdfData.url,pageNum: false,backTop: false,});this.pdfH5.on("ready", () => {this.pdfData.totalNum = this.pdfH5.totalNum - 0this.pdfData.currentNum = this.pdfH5.currentNum - 0})this.pdfH5.on("scroll", (scrollTop, currentNum) => {this.pdfData.currentNum = currentNum - 1 === 0 ? 1 : currentNum - 1})this.pdfH5.on('success', () => {this.inpDisabled = false})
})// html
<view id="pdf"></view><view class="operation-box"><view class="operation"><view class="page-num-info"><text>{{ pdfData.currentNum }}</text>/<text>{{ pdfData.totalNum }}</text></view><view class="jump-box" v-if="!tips" @click="clickInp"><view class="inp-box">跳转 <input style="width: 60px;text-align: center;color: black" type="number" @input="printfVal" v-model="jumpNum" /></view><view @click.stop="goToPage">确定</view></view><view v-else class="tips">尚未加载完成请等待..</view><uni-icons @click="goToTop" type="home" size="30" color="#b0b2b2"></uni-icons></view></view>

docx预览(不能预览doc格式文件)

插件:docx-preview
版本:“docx-preview”: “^0.1.20”

npm i docx-preview

import * as DocxJs from 'docx-preview'// methods
const f = await fetch(this.fileUrl);const ab = await f.arrayBuffer();await DocxJs.renderAsync(ab, document.getElementById('docxDom'), document.getElementById('docxDom'), {inWrapper: false, // 适配移动端
})// html
<view id="docxDom"></view>

xlsx预览

npm i xlsx

const f = await fetch(this.fileUrl);const ab = await f.arrayBuffer();const wb = XLSX.read(ab, {});const ws = wb.Sheets[wb.SheetNames[0]];this.xlsxFileData.tableData = XLSX.utils.sheet_to_json(ws, {raw: false, rawNumbers: false})this.xlsxFileData.labelList.splice(0)Object.keys(this.xlsxFileData.tableData[0]).forEach(v => {this.xlsxFileData.labelList.push(v)})// html
<uni-table border stripe emptyText="暂无更多数据"><!-- 表头行 --><uni-tr><uni-th align="center" v-for="item in xlsxFileData.labelList">{{ item }}</uni-th></uni-tr><!-- 表格数据行 --><uni-tr v-for="item in xlsxFileData.tableData"><uni-td  v-for="key in xlsxFileData.labelList">{{ item[key] }}</uni-td></uni-tr></uni-table>

pptx预览

github: PPTXjs
gitee: PPTXjs

拷贝文件到本地项目中在index.html引入

如果在index.html中引入没有加载,则动态引入

// app.vue// created || onLaunch
this.addLink('/static/PPTXjs/css/pptxjs.css')
this.addLink('/static/PPTXjs/css/nv.d3.min.css')
this.addScript('/static/PPTXjs/js/jquery-1.11.3.min.js')
this.addScript('/static/PPTXjs/js/jszip.min.js')
this.addScript('/static/PPTXjs/js/filereader.js')
this.addScript('/static/PPTXjs/js/d3.min.js')
this.addScript('/static/PPTXjs/js/nv.d3.min.js')
this.addScript('/static/PPTXjs/js/dingbat.js')
this.addScript('/static/PPTXjs/js/pptxjs.js')
this.addScript('/static/PPTXjs/js/divs2slides.js')//methods
addScript (src) {let script = document.createElement('script');script.src = src;script.type = 'text/javascript';// script.setAttribute('async', true);// script.setAttribute('defer', true);document.head.appendChild(script);
},
addLink (href) {let link = document.createElement('link');link.rel = "stylesheet"link.href = hrefdocument.head.appendChild(link);
},

引入拷贝的js文件以后

// methods
this.$nextTick(() => {$("#pptxDom").pptxToHtml({pptxFileUrl: this.fileUrl,slidesScale: "100%",slideMode: false,keyBoardShortCut: false,});// 适配移动端!!let timer = setInterval(() => {const $slides = $(".slides");if ($slides.children().length) {const slidesWidth = Math.max(...Array.from($slides.children()).map((s) => s.offsetWidth));const $wrapper = $("#pptxDom");const wrapperWidth = window.innerWidth;const wrapperHeight = window.innerHeight;$wrapper.css({transform: `scale(${wrapperWidth / slidesWidth})`,"transform-origin": "top left",height: wrapperHeight * (1 / (wrapperWidth / slidesWidth)) + "px",});clearInterval(timer);}}, 100)})

使用iframe预览(缺点:在微信内置浏览器不显示或者跨域,需要使用插件。)

文档预览服务

xdocin需要付费,前几天免费体验
日常格式都可以预览,比如xls、xlsx、doc、docx、pptx、pdf

<iframe :src="`https://view.xdocin.com/view?src=${encodeURIComponent(fileUrl)}`"  style="min-width: 100vw;min-height: 100vh;"></iframe>

微软免费预览
日常格式除了不能预览pdf,其他类型都可以(文件大小不超过10M)

<!-- pdf格式文件可以直接赋值iframe的src预览,其余交给微软服务 -->
<iframe allow="payment" v-if="fileUrl.includes('.pdf')" :src="fileUrl"  style="min-width: 100vw;min-height: 100vh;"></iframe>
<iframe v-else :src="`https://view.officeapps.live.com/op/view.aspx?src=${encodeURIComponent(fileUrl)}`"  style="min-width: 100vw;min-height: 100vh;"></iframe>

这篇关于前端使用插件预览pdf、docx、xlsx、pptx格式文件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/469313

相关文章

python中的flask_sqlalchemy的使用及示例详解

《python中的flask_sqlalchemy的使用及示例详解》文章主要介绍了在使用SQLAlchemy创建模型实例时,通过元类动态创建实例的方式,并说明了如何在实例化时执行__init__方法,... 目录@orm.reconstructorSQLAlchemy的回滚关联其他模型数据库基本操作将数据添

Spring配置扩展之JavaConfig的使用小结

《Spring配置扩展之JavaConfig的使用小结》JavaConfig是Spring框架中基于纯Java代码的配置方式,用于替代传统的XML配置,通过注解(如@Bean)定义Spring容器的组... 目录JavaConfig 的概念什么是JavaConfig?为什么使用 JavaConfig?Jav

Java使用Spire.Doc for Java实现Word自动化插入图片

《Java使用Spire.DocforJava实现Word自动化插入图片》在日常工作中,Word文档是不可或缺的工具,而图片作为信息传达的重要载体,其在文档中的插入与布局显得尤为关键,下面我们就来... 目录1. Spire.Doc for Java库介绍与安装2. 使用特定的环绕方式插入图片3. 在指定位

Springboot3 ResponseEntity 完全使用案例

《Springboot3ResponseEntity完全使用案例》ResponseEntity是SpringBoot中控制HTTP响应的核心工具——它能让你精准定义响应状态码、响应头、响应体,相比... 目录Spring Boot 3 ResponseEntity 完全使用教程前置准备1. 项目基础依赖(M

Java使用Spire.Barcode for Java实现条形码生成与识别

《Java使用Spire.BarcodeforJava实现条形码生成与识别》在现代商业和技术领域,条形码无处不在,本教程将引导您深入了解如何在您的Java项目中利用Spire.Barcodefor... 目录1. Spire.Barcode for Java 简介与环境配置2. 使用 Spire.Barco

Android使用java实现网络连通性检查详解

《Android使用java实现网络连通性检查详解》这篇文章主要为大家详细介绍了Android使用java实现网络连通性检查的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录NetCheck.Java(可直接拷贝)使用示例(Activity/Fragment 内)权限要求

SpringBoot返回文件让前端下载的几种方式

《SpringBoot返回文件让前端下载的几种方式》文章介绍了开发中文件下载的两种常见解决方案,并详细描述了通过后端进行下载的原理和步骤,包括一次性读取到内存和分块写入响应输出流两种方法,此外,还提供... 目录01 背景02 一次性读取到内存,通过响应输出流输出到前端02 将文件流通过循环写入到响应输出流

C# 预处理指令(# 指令)的具体使用

《C#预处理指令(#指令)的具体使用》本文主要介绍了C#预处理指令(#指令)的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录1、预处理指令的本质2、条件编译指令2.1 #define 和 #undef2.2 #if, #el

C#中Trace.Assert的使用小结

《C#中Trace.Assert的使用小结》Trace.Assert是.NET中的运行时断言检查工具,用于验证代码中的关键条件,下面就来详细的介绍一下Trace.Assert的使用,具有一定的参考价值... 目录1、 什么是 Trace.Assert?1.1 最简单的比喻1.2 基本语法2、⚡ 工作原理3

C# IPAddress 和 IPEndPoint 类的使用小结

《C#IPAddress和IPEndPoint类的使用小结》本文主要介绍了C#IPAddress和IPEndPoint类的使用小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定... 目录一、核心作用网络编程基础类二、IPAddress 类详解三种初始化方式1. byte 数组初始化2. l