vue3+PPTXjs 在线ppt预览

[复制链接]
发表于 2026-1-15 03:02:04 | 显示全部楼层 |阅读模式
- 使用PPTXjs做ppt预览,有完备的代码包,基于jquery
- vue3使用iframe引入用于预览ppt的网页,通过url参数通报必要预览的ppt链接
- 通过网页选择文件上传也可以通过下面的函数把文件转换成链接,实如今文件上传到服务器前就可以预览
  1. URL.createObjectURL(file);
复制代码



ppt/Index.vue:
  1. <template>
  2.         <div class="page-container m-ppt-wrap" id="page-contaninder" ref="pageContaninderRef">
  3.                 <div>
  4.                         <input type="file" @change="handleFileChange" />
  5.                 </div>
  6.                 <iframe class="m-iframe" :src="iframeUrl"></iframe>
  7.         </div>
  8. </template>
  9. <script setup lang="ts">
  10. import { ref } from "vue";
  11. console.log(location)
  12. const iframeUrl = ref(
  13.         `${location.origin}/dist/ppt/index.html?file=${location.origin}/dist/ppt/1.pptx`
  14. );
  15. const handleFileChange = (e: any) => {
  16.         let file: any = e.target.files[0];
  17.         let fileUrl = URL.createObjectURL(file);
  18.         let url = `${location.origin}/dist/ppt/index.html?file=${fileUrl}`;
  19.         iframeUrl.value = url;
  20. };
  21. </script>
  22. <style lang="scss" scoped>
  23. .m-ppt-wrap {
  24.         display: flex;
  25.         flex-direction: column;
  26.         height: 100%;
  27. }
  28. .m-iframe {
  29.         width: 100%;
  30.         flex: 1;
  31.         border: none;
  32. }
  33. </style>
复制代码

public/ppt:
https://download.csdn.net/download/xutongbao/89819342
https://pptx.js.org/index.html

index.html:
  1. <!doctype html>
  2. <html>
  3.         <head>
  4.                 <meta charset="utf-8" />
  5.                 <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6.                 <title>PPTXjs - Meshesha</title>
  7.                 <link rel="stylesheet" href="./css/pptxjs.css" />
  8.                 <link rel="stylesheet" href="./css/nv.d3.min.css" />
  9.                 <script type="text/javascript" src="./js/jquery-1.11.3.min.js"></script>
  10.                 <script type="text/javascript" src="./js/jszip.min.js"></script>
  11.                 <script type="text/javascript" src="./js/filereader.js"></script>
  12.                 <script type="text/javascript" src="./js/d3.min.js"></script>
  13.                 <script type="text/javascript" src="./js/nv.d3.min.js"></script>
  14.                 <script type="text/javascript" src="./js/pptxjs.js"></script>
  15.                 <script type="text/javascript" src="./js/divs2slides.js"></script>
  16.                 <script type="text/javascript" src="./js/jquery.fullscreen-min.js"></script>
  17.                 <script type="text/javascript">
  18.                         $(function () {
  19.                                 var oldWidth,
  20.                                         oldMargin,
  21.                                         isFullscreenMode = false;
  22.                                 $("#fullscreen-btn").on("click", function () {
  23.                                         if (!isFullscreenMode) {
  24.                                                 oldWidth = $("#result .slide").css("width");
  25.                                                 oldMargin = $("#result .slide").css("margin");
  26.                                                 $("#result .slide").css({
  27.                                                         width: "99%",
  28.                                                         margin: "0 auto"
  29.                                                 });
  30.                                                 $("#result").toggleFullScreen();
  31.                                                 isFullscreenMode = true;
  32.                                         } else {
  33.                                                 $("#result .slide").css({
  34.                                                         width: oldWidth,
  35.                                                         margin: oldMargin
  36.                                                 });
  37.                                                 $("#result").toggleFullScreen();
  38.                                                 isFullscreenMode = false;
  39.                                         }
  40.                                 });
  41.                                 $(document).bind("fullscreenchange", function () {
  42.                                         if (!$(document).fullScreen()) {
  43.                                                 $("#result .slide").css({
  44.                                                         width: oldWidth,
  45.                                                         margin: oldMargin
  46.                                                 });
  47.                                         }
  48.                                 });
  49.                         });
  50.                 </script>
  51.                 <style>
  52.                         html,
  53.                         body {
  54.                                 margin: 0;
  55.                                 padding: 0;
  56.                         }
  57.                         #warper {
  58.                                 margin-right: auto;
  59.                                 margin-left: auto;
  60.                                 width: 900px;
  61.                         }
  62.                 </style>
  63.         </head>
  64.         <body>
  65.                 <div id="warper">
  66.                         <input id="uploadFileInput" type="file" />
  67.                         <br /><br />
  68.                         <div id="container">
  69.                                 <div id="result"></div>
  70.                         </div>
  71.                 </div>
  72.                 <script>
  73.                         var pptxFileUrl = "http://localhost:5173/dist/ppt/2.pptx";
  74.                         //http://localhost:5173/dist/ppt/index.html?file=http://localhost:5173/dist/ppt/1.pptx
  75.                         var pptxFileUrlValue = window.location.search.substr(1).split("file=")[1];
  76.                         if (pptxFileUrlValue) {
  77.                                 pptxFileUrl = pptxFileUrlValue;
  78.                         }
  79.                         $("#result").pptxToHtml({
  80.                                 pptxFileUrl: pptxFileUrl,
  81.                                 fileInputId: "uploadFileInput",
  82.                                 slideMode: false,
  83.                                 keyBoardShortCut: false,
  84.                                 slideModeConfig: {
  85.                                         //on slide mode (slideMode: true)
  86.                                         first: 1,
  87.                                         nav: false /** true,false : show or not nav buttons*/,
  88.                                         navTxtColor: "white" /** color */,
  89.                                         navNextTxt: "&#8250;", //">"
  90.                                         navPrevTxt: "&#8249;", //"<"
  91.                                         showPlayPauseBtn: false /** true,false */,
  92.                                         keyBoardShortCut: false /** true,false */,
  93.                                         showSlideNum: false /** true,false */,
  94.                                         showTotalSlideNum: false /** true,false */,
  95.                                         autoSlide: false /** false or seconds (the pause time between slides) , F8 to active(keyBoardShortCut: true) */,
  96.                                         randomAutoSlide: false /** true,false ,autoSlide:true */,
  97.                                         loop: false /** true,false */,
  98.                                         background: "black" /** false or color*/,
  99.                                         transition:
  100.                                                 "default" /** transition type: "slid","fade","default","random" , to show transition efects :transitionTime > 0.5 */,
  101.                                         transitionTime: 1 /** transition time in seconds */
  102.                                 }
  103.                         });
  104.                 </script>
  105.         </body>
  106. </html>
复制代码
  1. <template>
  2.         <div class="m-wrap">
  3.                 <div v-if="extension === 'docx' || extension === 'doc'" style="height:100%">
  4.                         <vue-office-docx :src="docx" style="height: 100%; margin: 0; padding: 0" />
  5.                 </div>
  6.                 <div v-if="extension === 'xlsx' || extension === 'xls'" style="height:100%">
  7.                         <vue-office-excel :src="excel" style="height: 100%" />
  8.                 </div>
  9.                 <div class="m-iframe-wrap" v-show="extension === 'pdf'">
  10.                         <iframe class="m-iframe" v-show="pdfSrc" :src="pdfSrc" width="100%"></iframe>
  11.                 </div>
  12.                 <div class="m-iframe-wrap" v-show="extension === 'pptx' || extension === 'ppt'">
  13.                         <iframe class="m-iframe" v-show="pptUrl" :src="pptUrl" width="100%" height="100%"></iframe>
  14.                 </div>
  15.         </div>
  16. </template>
  17. <script lang="ts" name="cl-file-viewer" setup>
  18. import { ref, watch } from "vue";
  19. import VueOfficeExcel from "@vue-office/excel";
  20. import VueOfficeDocx from "@vue-office/docx";
  21. import "@vue-office/excel/lib/index.css";
  22. import "@vue-office/docx/lib/index.css";
  23. const props = defineProps({
  24.         fileType: {
  25.                 type: String,
  26.                 default: "file"
  27.         }, // 文件类型
  28.         file: {
  29.                 type: Object,
  30.                 default: null
  31.         } //文件流 或者文件地址
  32. });
  33. // 获取文件扩展名
  34. const extension = ref("");
  35. const docx = ref("");
  36. const excel = ref("");
  37. const pdfSrc:any = ref(null);
  38. const pptUrl:any = ref(null);
  39. const readFile = async () => {
  40.         // 获取选中的文件
  41.         //@ts-ignore
  42.         const file:any = props.fileType === "file" ? props.file : getFileStream(props.file);
  43.         if (!file) {
  44.                 return;
  45.         }
  46.         // 获取文件扩展名
  47.         extension.value = file.name.split(".").pop();
  48.         // 根据文件扩展名进行处理
  49.         switch (extension.value) {
  50.                 case "docx":
  51.                 case "doc":
  52.                         // 读取Word文件
  53.                         readWordFile(file);
  54.                         break;
  55.                 case "xlsx":
  56.                 case "xls":
  57.                         // 读取Excel文件
  58.                         readExcelFile(file);
  59.                         break;
  60.                 case "pdf":
  61.                         // 读取PDF文件
  62.                         readPdfFile(file);
  63.                         break;
  64.                 case "pptx":
  65.                 case "ppt":
  66.                         // 读取Excel文件
  67.                         readPptFile(file);
  68.                         break;
  69.                 default:
  70.                         // 不支持的文件类型
  71.                         alert("Unsupported file type");
  72.         }
  73. };
  74. const readWordFile = (file: any) => {
  75.         docx.value = URL.createObjectURL(file);
  76. };
  77. const readExcelFile = (file: any) => {
  78.         excel.value = URL.createObjectURL(file);
  79. };
  80. const readPdfFile = async (file: any) => {
  81.         if (file) {
  82.                 // 判断传入的 file 参数是否为字符串类型
  83.                 if (props.file instanceof String) {
  84.                         // 如果是字符串类型,则将其赋值给 pdfSrc.value
  85.                         pdfSrc.value = props.file;
  86.                 } else {
  87.                         // 如果不是字符串类型,则使用 URL.createObjectURL 方法创建一个指向该文件的 URL,并将其赋值给 pdfSrc.value
  88.                         pdfSrc.value = URL.createObjectURL(file);
  89.                 }
  90.         }
  91. };
  92. const readPptFile = async (file: any) => {
  93.         if (file) {
  94.                 pptUrl.value = `${location.origin}/dist/ppt/index.html?file=${URL.createObjectURL(file)}` ;
  95.         }
  96. };
  97. // url地址转发为文件流
  98. const getFileStream = (url: string) => {
  99.         return new Promise((resolve, reject) => {
  100.                 // 创建一个XMLHttpRequest对象
  101.                 const xhr = new XMLHttpRequest();
  102.                 // 设置请求方法为GET,并传入请求的URL
  103.                 xhr.open("GET", url);
  104.                 // 设置响应类型为blob,以便能够处理二进制数据
  105.                 xhr.responseType = "blob";
  106.                 // 当请求加载完成时,调用resolve方法并将响应数据作为参数传入
  107.                 xhr.onload = () => resolve(xhr.response);
  108.                 // 当请求发生错误时,调用reject方法并将错误信息作为参数传入
  109.                 xhr.onerror = (err) => reject(err);
  110.         });
  111. };
  112. // 初始化
  113. watch(
  114.         () => props.file,
  115.         (newValue, oldValue) => {
  116.                 if (newValue && newValue != oldValue) {
  117.                         nextTick(() => {
  118.                                 readFile();
  119.                         });
  120.                 }
  121.         },
  122.         { immediate: true }
  123. );
  124. </script>
  125. <style scoped lang="scss">
  126. .m-wrap{display: flex;flex-direction: column; height: 100%}
  127. .m-iframe-wrap{flex: 1;overflow-y: auto;}
  128. .m-iframe{border: none;height: 100%;}
  129. </style>
复制代码
  1. <template>
  2.         <div class="m-wrap">
  3.                 <div v-if="extension === 'docx' || extension === 'doc'" style="height:100%">
  4.                         <vue-office-docx :src="docx" style="height: 100%; margin: 0; padding: 0" />
  5.                 </div>
  6.                 <div v-if="extension === 'xlsx' || extension === 'xls'" style="height:100%">
  7.                         <vue-office-excel :src="excel" style="height: 100%" />
  8.                 </div>
  9.                 <div class="m-iframe-wrap" v-show="extension === 'pdf'">
  10.                         <iframe class="m-iframe" v-show="pdfSrc" :src="pdfSrc" width="100%"></iframe>
  11.                 </div>
  12.                 <div class="m-iframe-wrap" v-show="extension === 'pptx' || extension === 'ppt'">
  13.                         <iframe class="m-iframe" v-show="pptUrl" :src="pptUrl" width="100%" height="100%"></iframe>
  14.                 </div>
  15.         </div>
  16. </template>
  17. <script lang="ts" name="cl-file-viewer" setup>
  18. import { ref, watch } from "vue";
  19. import VueOfficeExcel from "@vue-office/excel";
  20. import VueOfficeDocx from "@vue-office/docx";
  21. import "@vue-office/excel/lib/index.css";
  22. import "@vue-office/docx/lib/index.css";
  23. const props = defineProps({
  24.         fileType: {
  25.                 type: String,
  26.                 default: "file"
  27.         }, // 文件类型
  28.         file: {
  29.                 type: Object,
  30.                 default: null
  31.         } //文件流 或者文件地址
  32. });
  33. // 获取文件扩展名
  34. const extension = ref("");
  35. const docx = ref("");
  36. const excel = ref("");
  37. const pdfSrc:any = ref(null);
  38. const pptUrl:any = ref(null);
  39. const readFile = async () => {
  40.         // 获取选中的文件
  41.         //@ts-ignore
  42.         const file:any = props.fileType === "file" ? props.file : getFileStream(props.file);
  43.         if (!file) {
  44.                 return;
  45.         }
  46.         // 获取文件扩展名
  47.         extension.value = file.name.split(".").pop();
  48.         // 根据文件扩展名进行处理
  49.         switch (extension.value) {
  50.                 case "docx":
  51.                 case "doc":
  52.                         // 读取Word文件
  53.                         readWordFile(file);
  54.                         break;
  55.                 case "xlsx":
  56.                 case "xls":
  57.                         // 读取Excel文件
  58.                         readExcelFile(file);
  59.                         break;
  60.                 case "pdf":
  61.                         // 读取PDF文件
  62.                         readPdfFile(file);
  63.                         break;
  64.                 case "pptx":
  65.                 case "ppt":
  66.                         // 读取Excel文件
  67.                         readPptFile(file);
  68.                         break;
  69.                 default:
  70.                         // 不支持的文件类型
  71.                         alert("Unsupported file type");
  72.         }
  73. };
  74. const readWordFile = (file: any) => {
  75.         docx.value = URL.createObjectURL(file);
  76. };
  77. const readExcelFile = (file: any) => {
  78.         excel.value = URL.createObjectURL(file);
  79. };
  80. const readPdfFile = async (file: any) => {
  81.         if (file) {
  82.                 // 判断传入的 file 参数是否为字符串类型
  83.                 if (props.file instanceof String) {
  84.                         // 如果是字符串类型,则将其赋值给 pdfSrc.value
  85.                         pdfSrc.value = props.file;
  86.                 } else {
  87.                         // 如果不是字符串类型,则使用 URL.createObjectURL 方法创建一个指向该文件的 URL,并将其赋值给 pdfSrc.value
  88.                         pdfSrc.value = URL.createObjectURL(file);
  89.                 }
  90.         }
  91. };
  92. const readPptFile = async (file: any) => {
  93.         if (file) {
  94.                 pptUrl.value = `${location.origin}/dist/ppt/index.html?file=${URL.createObjectURL(file)}` ;
  95.         }
  96. };
  97. // url地址转发为文件流
  98. const getFileStream = (url: string) => {
  99.         return new Promise((resolve, reject) => {
  100.                 // 创建一个XMLHttpRequest对象
  101.                 const xhr = new XMLHttpRequest();
  102.                 // 设置请求方法为GET,并传入请求的URL
  103.                 xhr.open("GET", url);
  104.                 // 设置响应类型为blob,以便能够处理二进制数据
  105.                 xhr.responseType = "blob";
  106.                 // 当请求加载完成时,调用resolve方法并将响应数据作为参数传入
  107.                 xhr.onload = () => resolve(xhr.response);
  108.                 // 当请求发生错误时,调用reject方法并将错误信息作为参数传入
  109.                 xhr.onerror = (err) => reject(err);
  110.         });
  111. };
  112. // 初始化
  113. watch(
  114.         () => props.file,
  115.         (newValue, oldValue) => {
  116.                 if (newValue && newValue != oldValue) {
  117.                         nextTick(() => {
  118.                                 readFile();
  119.                         });
  120.                 }
  121.         },
  122.         { immediate: true }
  123. );
  124. </script>
  125. <style scoped lang="scss">
  126. .m-wrap{display: flex;flex-direction: column; height: 100%}
  127. .m-iframe-wrap{flex: 1;overflow-y: auto;}
  128. .m-iframe{border: none;height: 100%;}
  129. </style>
复制代码



人工智能学习网站
https://chat.xutongbao.top








免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!qidao123.com:ToB企服之家,中国第一个企服评测及软件市场,开放入驻,技术点评得现金

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表