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.csdn.net/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

Vue3是一个流行的JavaScript框架,用于构建Web应用程序。在Vue3中,你可以使用各种库和插件来处理图片和预览图片。其中一种常用的库是vue-image-previewer,它提供了一个简单的组件来预览图片。 关于window.open预览图片,这是一个浏览器内置的函数,用于打开一个新的浏览器窗口或选项卡来显示图片。你可以使用它来预览本地或网络上的图片。 如果你想在Vue3应用程序中使用window.open来预览图片,你可以按照以下步骤进行操作: 1. 在Vue3组件中引入vue-image-previewer库,并使用它的预览组件。 ```javascript import { ImagePreviewer } from 'vue-image-previewer'; ``` 2. 在你的Vue3组件中注册该组件,并在需要预览图片的地方使用它。 ```html <template> <div> <ImagePreviewer :images="imageUrls" /> </div> </template> <script> export default { components: { ImagePreviewer, }, data() { return { imageUrls: ['/path/to/image1.jpg', '/path/to/image2.jpg'], // 图片URL列表 }; }, }; </script> ``` 在上面的代码中,`ImagePreviewer`组件接收一个`images`属性,该属性是一个包含图片URL的数组。当你点击预览按钮时,vue-image-previewer会使用window.open来打开并预览选定的图片。 请注意,在使用`window.open`时,你需要在支持浏览器上运行你的Vue3应用程序,并且确保你已经正确配置了浏览器以允许新窗口或选项卡的创建。此外,如果你打算在Web应用程序中使用`window.open`来打开图片,请确保遵守相关的安全和隐私最佳实践。 希望这可以帮助到你!如果你有任何其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

徐同保

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

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

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

打赏作者

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

抵扣说明:

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

余额充值