vue3+PPTXjs 在线ppt预览

- 使用PPTXjs做ppt预览,有完整的代码包,基于jquery

- vue3使用iframe引入用于预览ppt的网页,通过url参数传递需要预览的ppt链接

- 通过网页选择文件上传也可以通过下面的函数把文件转换成链接,实现在文件上传到服务器前就可以预览

URL.createObjectURL(file);

ppt/Index.vue:

<template>
	<div class="page-container m-ppt-wrap" id="page-contaninder" ref="pageContaninderRef">
		<div>
			<input type="file" @change="handleFileChange" />
		</div>
		<iframe class="m-iframe" :src="iframeUrl"></iframe>
	</div>
</template>

<script setup lang="ts">
import { ref } from "vue";

console.log(location)
const iframeUrl = ref(
	`${location.origin}/dist/ppt/index.html?file=${location.origin}/dist/ppt/1.pptx`
);

const handleFileChange = (e: any) => {
	let file: any = e.target.files[0];
	let fileUrl = URL.createObjectURL(file);
	let url = `${location.origin}/dist/ppt/index.html?file=${fileUrl}`;
	iframeUrl.value = url;
};
</script>

<style lang="scss" scoped>
.m-ppt-wrap {
	display: flex;
	flex-direction: column;
	height: 100%;
}
.m-iframe {
	width: 100%;
	flex: 1;
	border: none;
}
</style>

public/ppt:

https://download.youkuaiyun.com/download/xutongbao/89819342

https://pptx.js.org/index.html

index.html:

<!doctype html>
<html>
	<head>
		<meta charset="utf-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=edge" />
		<title>PPTXjs - Meshesha</title>

		<link rel="stylesheet" href="./css/pptxjs.css" />
		<link rel="stylesheet" href="./css/nv.d3.min.css" />

		<script type="text/javascript" src="./js/jquery-1.11.3.min.js"></script>
		<script type="text/javascript" src="./js/jszip.min.js"></script>
		<script type="text/javascript" src="./js/filereader.js"></script>
		<script type="text/javascript" src="./js/d3.min.js"></script>
		<script type="text/javascript" src="./js/nv.d3.min.js"></script>
		<script type="text/javascript" src="./js/pptxjs.js"></script>
		<script type="text/javascript" src="./js/divs2slides.js"></script>

		<script type="text/javascript" src="./js/jquery.fullscreen-min.js"></script>

		<script type="text/javascript">
			$(function () {
				var oldWidth,
					oldMargin,
					isFullscreenMode = false;
				$("#fullscreen-btn").on("click", function () {
					if (!isFullscreenMode) {
						oldWidth = $("#result .slide").css("width");
						oldMargin = $("#result .slide").css("margin");
						$("#result .slide").css({
							width: "99%",
							margin: "0 auto"
						});
						$("#result").toggleFullScreen();
						isFullscreenMode = true;
					} else {
						$("#result .slide").css({
							width: oldWidth,
							margin: oldMargin
						});
						$("#result").toggleFullScreen();
						isFullscreenMode = false;
					}
				});
				$(document).bind("fullscreenchange", function () {
					if (!$(document).fullScreen()) {
						$("#result .slide").css({
							width: oldWidth,
							margin: oldMargin
						});
					}
				});
			});
		</script>
		<style>
			html,
			body {
				margin: 0;
				padding: 0;
			}
			#warper {
				margin-right: auto;
				margin-left: auto;
				width: 900px;
			}
		</style>
	</head>
	<body>
		<div id="warper">
			<input id="uploadFileInput" type="file" />
			<br /><br />
			<div id="container">
				<div id="result"></div>
			</div>
		</div>
		<script>
			var pptxFileUrl = "http://localhost:5173/dist/ppt/2.pptx";
			//http://localhost:5173/dist/ppt/index.html?file=http://localhost:5173/dist/ppt/1.pptx
			var pptxFileUrlValue = window.location.search.substr(1).split("file=")[1];
			if (pptxFileUrlValue) {
				pptxFileUrl = pptxFileUrlValue;
			}
			$("#result").pptxToHtml({
				pptxFileUrl: pptxFileUrl,
				fileInputId: "uploadFileInput",
				slideMode: false,
				keyBoardShortCut: false,
				slideModeConfig: {
					//on slide mode (slideMode: true)
					first: 1,
					nav: false /** true,false : show or not nav buttons*/,
					navTxtColor: "white" /** color */,
					navNextTxt: "&#8250;", //">"
					navPrevTxt: "&#8249;", //"<"
					showPlayPauseBtn: false /** true,false */,
					keyBoardShortCut: false /** true,false */,
					showSlideNum: false /** true,false */,
					showTotalSlideNum: false /** true,false */,
					autoSlide: false /** false or seconds (the pause time between slides) , F8 to active(keyBoardShortCut: true) */,
					randomAutoSlide: false /** true,false ,autoSlide:true */,
					loop: false /** true,false */,
					background: "black" /** false or color*/,
					transition:
						"default" /** transition type: "slid","fade","default","random" , to show transition efects :transitionTime > 0.5 */,
					transitionTime: 1 /** transition time in seconds */
				}
			});
		</script>
	</body>
</html>
<template>
	<div class="m-wrap">
		<div v-if="extension === 'docx' || extension === 'doc'" style="height:100%">
			<vue-office-docx :src="docx" style="height: 100%; margin: 0; padding: 0" />
		</div>
		<div v-if="extension === 'xlsx' || extension === 'xls'" style="height:100%">
			<vue-office-excel :src="excel" style="height: 100%" />
		</div>
		<div class="m-iframe-wrap" v-show="extension === 'pdf'">
			<iframe class="m-iframe" v-show="pdfSrc" :src="pdfSrc" width="100%"></iframe>
		</div>
		<div class="m-iframe-wrap" v-show="extension === 'pptx' || extension === 'ppt'">
			<iframe class="m-iframe" v-show="pptUrl" :src="pptUrl" width="100%" height="100%"></iframe>
		</div>
	</div>
</template>
<script lang="ts" name="cl-file-viewer" setup>
import { ref, watch } from "vue";
import VueOfficeExcel from "@vue-office/excel";
import VueOfficeDocx from "@vue-office/docx";
import "@vue-office/excel/lib/index.css";
import "@vue-office/docx/lib/index.css";

const props = defineProps({
	fileType: {
		type: String,
		default: "file"
	}, // 文件类型
	file: {
		type: Object,
		default: null
	} //文件流 或者文件地址
});

// 获取文件扩展名
const extension = ref("");
const docx = ref("");
const excel = ref("");
const pdfSrc:any = ref(null);
const pptUrl:any = ref(null);

const readFile = async () => {
	// 获取选中的文件
	//@ts-ignore
	const file:any = props.fileType === "file" ? props.file : getFileStream(props.file);
	if (!file) {
		return;
	}

	// 获取文件扩展名
	extension.value = file.name.split(".").pop();

	// 根据文件扩展名进行处理
	switch (extension.value) {
		case "docx":
		case "doc":
			// 读取Word文件
			readWordFile(file);
			break;
		case "xlsx":
		case "xls":
			// 读取Excel文件
			readExcelFile(file);
			break;
		case "pdf":
			// 读取PDF文件
			readPdfFile(file);
			break;
		case "pptx":
		case "ppt":
			// 读取Excel文件
			readPptFile(file);
			break;
		default:
			// 不支持的文件类型
			alert("Unsupported file type");
	}
};
const readWordFile = (file: any) => {
	docx.value = URL.createObjectURL(file);
};

const readExcelFile = (file: any) => {
	excel.value = URL.createObjectURL(file);
};

const readPdfFile = async (file: any) => {
	if (file) {
		// 判断传入的 file 参数是否为字符串类型
		if (props.file instanceof String) {
			// 如果是字符串类型,则将其赋值给 pdfSrc.value
			pdfSrc.value = props.file;
		} else {
			// 如果不是字符串类型,则使用 URL.createObjectURL 方法创建一个指向该文件的 URL,并将其赋值给 pdfSrc.value
			pdfSrc.value = URL.createObjectURL(file);
		}
	}
};

const readPptFile = async (file: any) => {
	if (file) {
		pptUrl.value = `${location.origin}/dist/ppt/index.html?file=${URL.createObjectURL(file)}` ;
	}
};

// url地址转发为文件流
const getFileStream = (url: string) => {
	return new Promise((resolve, reject) => {
		// 创建一个XMLHttpRequest对象
		const xhr = new XMLHttpRequest();
		// 设置请求方法为GET,并传入请求的URL
		xhr.open("GET", url);
		// 设置响应类型为blob,以便能够处理二进制数据
		xhr.responseType = "blob";
		// 当请求加载完成时,调用resolve方法并将响应数据作为参数传入
		xhr.onload = () => resolve(xhr.response);
		// 当请求发生错误时,调用reject方法并将错误信息作为参数传入
		xhr.onerror = (err) => reject(err);
	});
};

// 初始化
watch(
	() => props.file,
	(newValue, oldValue) => {
		if (newValue && newValue != oldValue) {
			nextTick(() => {
				readFile();
			});
		}
	},
	{ immediate: true }
);
</script>

<style scoped lang="scss">
.m-wrap{display: flex;flex-direction: column; height: 100%}
.m-iframe-wrap{flex: 1;overflow-y: auto;}
.m-iframe{border: none;height: 100%;}
</style>
<template>
	<div class="m-wrap">
		<div v-if="extension === 'docx' || extension === 'doc'" style="height:100%">
			<vue-office-docx :src="docx" style="height: 100%; margin: 0; padding: 0" />
		</div>
		<div v-if="extension === 'xlsx' || extension === 'xls'" style="height:100%">
			<vue-office-excel :src="excel" style="height: 100%" />
		</div>
		<div class="m-iframe-wrap" v-show="extension === 'pdf'">
			<iframe class="m-iframe" v-show="pdfSrc" :src="pdfSrc" width="100%"></iframe>
		</div>
		<div class="m-iframe-wrap" v-show="extension === 'pptx' || extension === 'ppt'">
			<iframe class="m-iframe" v-show="pptUrl" :src="pptUrl" width="100%" height="100%"></iframe>
		</div>
	</div>
</template>
<script lang="ts" name="cl-file-viewer" setup>
import { ref, watch } from "vue";
import VueOfficeExcel from "@vue-office/excel";
import VueOfficeDocx from "@vue-office/docx";
import "@vue-office/excel/lib/index.css";
import "@vue-office/docx/lib/index.css";

const props = defineProps({
	fileType: {
		type: String,
		default: "file"
	}, // 文件类型
	file: {
		type: Object,
		default: null
	} //文件流 或者文件地址
});

// 获取文件扩展名
const extension = ref("");
const docx = ref("");
const excel = ref("");
const pdfSrc:any = ref(null);
const pptUrl:any = ref(null);

const readFile = async () => {
	// 获取选中的文件
	//@ts-ignore
	const file:any = props.fileType === "file" ? props.file : getFileStream(props.file);
	if (!file) {
		return;
	}

	// 获取文件扩展名
	extension.value = file.name.split(".").pop();

	// 根据文件扩展名进行处理
	switch (extension.value) {
		case "docx":
		case "doc":
			// 读取Word文件
			readWordFile(file);
			break;
		case "xlsx":
		case "xls":
			// 读取Excel文件
			readExcelFile(file);
			break;
		case "pdf":
			// 读取PDF文件
			readPdfFile(file);
			break;
		case "pptx":
		case "ppt":
			// 读取Excel文件
			readPptFile(file);
			break;
		default:
			// 不支持的文件类型
			alert("Unsupported file type");
	}
};
const readWordFile = (file: any) => {
	docx.value = URL.createObjectURL(file);
};

const readExcelFile = (file: any) => {
	excel.value = URL.createObjectURL(file);
};

const readPdfFile = async (file: any) => {
	if (file) {
		// 判断传入的 file 参数是否为字符串类型
		if (props.file instanceof String) {
			// 如果是字符串类型,则将其赋值给 pdfSrc.value
			pdfSrc.value = props.file;
		} else {
			// 如果不是字符串类型,则使用 URL.createObjectURL 方法创建一个指向该文件的 URL,并将其赋值给 pdfSrc.value
			pdfSrc.value = URL.createObjectURL(file);
		}
	}
};

const readPptFile = async (file: any) => {
	if (file) {
		pptUrl.value = `${location.origin}/dist/ppt/index.html?file=${URL.createObjectURL(file)}` ;
	}
};

// url地址转发为文件流
const getFileStream = (url: string) => {
	return new Promise((resolve, reject) => {
		// 创建一个XMLHttpRequest对象
		const xhr = new XMLHttpRequest();
		// 设置请求方法为GET,并传入请求的URL
		xhr.open("GET", url);
		// 设置响应类型为blob,以便能够处理二进制数据
		xhr.responseType = "blob";
		// 当请求加载完成时,调用resolve方法并将响应数据作为参数传入
		xhr.onload = () => resolve(xhr.response);
		// 当请求发生错误时,调用reject方法并将错误信息作为参数传入
		xhr.onerror = (err) => reject(err);
	});
};

// 初始化
watch(
	() => props.file,
	(newValue, oldValue) => {
		if (newValue && newValue != oldValue) {
			nextTick(() => {
				readFile();
			});
		}
	},
	{ immediate: true }
);
</script>

<style scoped lang="scss">
.m-wrap{display: flex;flex-direction: column; height: 100%}
.m-iframe-wrap{flex: 1;overflow-y: auto;}
.m-iframe{border: none;height: 100%;}
</style>

人工智能学习网站

https://chat.xutongbao.top

<think>好的,我现在需要帮用户解决如何在Vue3中实现PPT文件的在线预览功能。根据用户提供的引用内容,他们提到了使用iframe嵌入页面和vue-office的pdf插件,还有遇到各种文件类型时的挑战。首先,我得考虑用户提到的两种方法:浏览器自带预览和使用第三方库。 首先,使用iframe嵌入可能是一个简单的方法。用户提到直接使用iframe,利用浏览器自身的预览功能。这可能适用于支持直接预览的文件类型,比如PDF,但PPT文件可能需要浏览器有相应的插件或支持。不过现代浏览器可能不支持直接预览PPT,所以这种方法可能有限制。这时候需要考虑是否将PPT转换为PDF或其他格式,或者使用在线服务如微软的Office Online Viewer,但该服务已经停止,可能需要替代方案。 然后,用户提到的vue-office插件,特别是@vue-office/docx支持DOCX,但PPTX可能需要其他处理。可能需要查看是否有类似的vue-office插件支持PPTX,或者使用其他库。比如,pptx.js是一个用于解析和展示PPTX的JavaScript库,可能可以集成到Vue3中。此外,使用微软的在线预览服务可能需要注册API密钥,或者使用Google Docs的嵌入方式,但需要注意权限和网络问题。 接下来,需要考虑用户的具体需求。如果用户希望在前端直接处理而不依赖后端转换,可能需要使用pptx.js来解析PPTX文件并渲染成HTML。这需要将PPTX文件上传,然后通过pptx.js解析,提取幻灯片内容,并在页面上展示。这可能需要处理异步加载和渲染,以及样式的问题。 另外,第三方服务如OnlyOffice或LibreOffice Online可能提供嵌入的预览功能,但需要搭建服务器或使用云服务,这可能增加复杂性。用户可能需要权衡实现的难度和需求,选择最适合的方法。 还需要考虑文件的上传和安全性问题。如果PPT文件包含敏感信息,使用第三方服务可能存在风险,因此可能需要本地解析。此外,浏览器的兼容性也是一个问题,比如pptx.js在不同浏览器上的表现是否一致。 总结下来,可能的解决方案有: 1. 使用iframe嵌入在线服务(如OnlyOffice Viewer)的URL。 2. 使用pptx.js在前端解析并渲染PPTX文件。 3. 使用vue-office或其他第三方库支持PPTX预览。 4. 转换为PDF后再用PDF预览库展示。 需要验证这些方法的可行性,比如检查vue-office是否支持PPTX,或者pptx.js的集成难度。同时,提供代码示例,帮助用户具体实现。</think>以下是使用Vue3实现PPT在线预览的三种常用方案,分步骤说明: ### 方案一:使用浏览器原生嵌入(基础方案) ```vue <template> <iframe :src="`https://view.officeapps.live.com/op/embed.aspx?src=${fileUrl}`" width="100%" height="600px" frameborder="0" ></iframe> </template> <script setup> const fileUrl = ref(&#39;https://your-domain.com/files/example.pptx&#39;) // 需配置CORS </script> ``` *说明*:微软官方接口支持直接嵌入(需确保文件可跨域访问)[^1] ### 方案二:使用pptx.js解析(纯前端方案) 1. 安装依赖: ```bash npm install pptxjs ``` 2. 组件实现: ```vue <template> <div ref="pptContainer" class="ppt-viewer"></div> </template> <script setup> import { ref, onMounted } from &#39;vue&#39; import Pptxjs from &#39;pptxjs&#39; const pptContainer = ref(null) const props = defineProps([&#39;fileUrl&#39;]) onMounted(async () => { const ppt = new Pptxjs() await ppt.load(props.fileUrl) ppt.render(pptContainer.value, { includeAnnotations: true, slideMode: true }) }) </script> ``` ### 方案三:使用第三方库vue-office(推荐方案) 1. 安装扩展包: ```bash npm install @vue-office/pptx ``` 2. 组件封装: ```vue <template> <vue-office-pptx :src="pptFile" @rendered="handleRendered" style="width: 100%; height: 70vh; border: 1px solid #eee" /> </template> <script setup> import { defineComponent } from &#39;vue&#39; import VueOfficePptx from &#39;@vue-office/pptx&#39; const pptFile = ref(&#39;https://example.com/demo.pptx&#39;) const handleRendered = () => { console.log(&#39;PPT渲染完成&#39;) } </script> ``` ### 方案对比 | 方案 | 优点 | 缺点 | 适用场景 | |------|------|------|----------| | 原生嵌入 | 无需额外开发,支持动画 | 依赖网络,隐私风险 | 公开文件 | | pptx.js | 完全本地化,高度定制 | 渲染效果简单 | 内部系统 | | vue-office | 开箱即用,样式统一 | 依赖库更新 | 快速开发 |
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

徐同保

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值