docx、pptx、xlsx、pdf 文件预览

纯前端预览

pdf 预览

(一)pdf.js 👍🏻

GitHub - mozilla/pdf.js

最新版 pdf.js 要求 node 版本不低于 18,可以在 Tags · mozilla/pdf.js · GitHub 下载 2. 版本

具体可以看 pdf预览 | Hu Yu

(二)iframe

一般的浏览器都默认支持预览 pdf

<iframe src="viewFileUrl" />

但是不同的浏览器的展示和交互不同,而且如果 PDF 文件过大也有可能导致浏览器卡死或者白屏。

docx 预览

Github - VolodymyrBaydalka/docxjs

使用方法:

import { renderAsync } from 'docx-preview'

interface DocxOptions {
bodyContainer?: HTMLElement | null
styleContainer?: HTMLElement
buffer: Blob
docxOptions?: Partial<Record<string, string | boolean>>
}

export const renderDocx = (options: DocxOptions): Promise<void> | undefined => {
if (typeof window !== 'undefined') {
const { bodyContainer, styleContainer, buffer, docxOptions = {} } = options
const defaultOptions = {
className: 'docx',
ignoreLastRenderedPageBreak: false,
}
const configuration = Object.assign({}, defaultOptions, docxOptions)
if (bodyContainer) {
return renderAsync(buffer, bodyContainer, styleContainer, configuration)
} else {
const contain = document.createElement('div')
document.body.appendChild(contain)
return renderAsync(buffer, contain, styleContainer, configuration)
}
}
}

xlsx 预览

GitHub - 501351981/vue-office

支持word(.docx)、excel(.xlsx)、pdf等各类型office文件预览的vue组件集合,提供一站式office文件预览方案,支持vue2和3,也支持React等非Vue框架。

vue-office-demo

更多用法可以看官网,比较详细

PPT 预览

暂时没有什么好的解决方法,可以使用下面的服务端预览

服务端预览

kkFileView 👍🏻

kkFileView - 在线文件预览

KkFileView 为文件文档在线预览解决方案,该项目使用流行的 spring boot 搭建,易上手和部署,基本支持主流办公文档的在线预览,如 doc, docx, xls, xlsx, ppt, pptx, pdf, txt, zip, rar, 图片, 视频, 音频等等

注意:KkFileView 为后端部署,前端直接使用后端给的地址即可

其他

WPS WebOffice 开放平台

WebOffice 开放平台

支持在线预览编辑,多人协同

阿里云 IMM

智能媒体管理-阿里云帮助中心

支持文件转换预览,但是需要与 阿里云OSS 一起使用

Office Web Viewer

微软官方提供的文档示例
demo_Sutter.pptx
微软提供的免费在线预览服务,用法简单,但是国内访问速度较慢