通过vue-pdf和print-js实现PDF和图片在线预览

news2025/5/24 12:52:30

npm install vue-pdf
npm install print-js

<template>
  <div>
    <!-- PDF 预览模态框 -->
    <a-modal
      :visible="showDialog"
      :footer="null"
      @cancel="handleCancel"
      :width="800"
      :maskClosable="true"
      :keyboard="true"
    >
      <div style="overflow-y: auto; overflow-x: hidden; height: 600px">
        <!-- 使用 sticky 定位打印按钮 -->
        <div style="position: sticky; top: 0; background: white; padding: 0px 0; z-index: 1;">
          <a-button
            shape="round"
            icon="file-pdf"
            @click="handlePrint"
            size="small"
            style="margin-bottom: 10px"
          >打印</a-button
          >
        </div>
        <div id="printFrom">
          <pdf
            v-if="isPdf"
            ref="pdf"
            v-for="item in pageTotal"
            :src="previewFileSrc"
            :key="item"
            :page="item"
          ></pdf>
          <img
            v-else-if="isImage"
            :src="previewImage"
            style="max-width: 100%; max-height: 500px; display: block; margin: 0 auto"
          />
          <div v-else style="text-align: center">
            <p>不支持预览此文件类型</p>
            <a :href="previewFileSrc" download>下载文件</a>
          </div>
        </div>
      </div>
    </a-modal>
  </div>
</template>

<script>
import Vue from "vue";
import { ACCESS_TOKEN } from "@/store/mutation-types";
import { getFileAccessHttpUrl } from "@/api/manage";
import pdf from "vue-pdf";
import printJS from "print-js";

const FILE_TYPE_ALL = "all";
const FILE_TYPE_IMG = "image";
const FILE_TYPE_TXT = "file";

export default {
  name: "AutoFilePreview",
  components: { pdf },
  props: {
    // 是否显示预览
    showDialog: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      fileUrl: null,
      printData: {
        printable: "printFrom",
        header: "",
        ignore: ["no-print"],
      },
      previewImage: "",
      previewFileSrc: "",
      pageTotal: null,
      isImage: false,
      isPdf: false,
    };
  },
  watch: {
    fileUrl: {
      immediate: true,
      handler(newVal) {
        if (newVal) {
          this.previewFileSrc = newVal;
          this.checkFileType();
        }
      },
    },
    showDialog: {
      immediate: true,
      handler(newVal) {
        if (newVal && this.fileUrl) {
          this.previewFileSrc = this.fileUrl;
          this.checkFileType();
        }
      },
    },
  },
  methods: {
    handlePrint() {
      printJS({
        printable: "printFrom",
        type: "html",
        header: "",
        targetStyles: ["*"],
        style: "@page {margin:0 10mm};",
        ignoreElements: ["no-print"],
      });
      this.test();
    },
    async checkFileType() {
      // 重置状态
      this.isImage = false;
      this.isPdf = false;

      // 获取文件类型
      const fileType = this.matchFileType(this.fileUrl);
      if (fileType === "image") {
        this.previewImage = this.fileUrl;
        this.isImage = true;
        this.pageTotal = 1;
      } else if (fileType === "pdf") {
        this.isPdf = true;
        await this.getTotal();
      } else {
        // 其他文件类型直接下载
        this.isImage = false;
        this.isPdf = false;
      }
    },
    async getTotal() {
      try {
        // 多页pdf的src中不能直接使用后端获取的pdf地址
        // 需要使用下述方法的返回值作为url
        const loadingTask = pdf.createLoadingTask(this.previewFileSrc);
        this.previewFileSrc = loadingTask;

        // 获取页码
        const pdfDoc = await loadingTask.promise;
        this.pageTotal = pdfDoc.numPages;
      } catch (error) {
        console.error("PDF加载错误:", error);
        this.$message.error("PDF文件加载失败");
      }
    },
    matchFileType(fileName) {
      // 后缀获取
      let suffix = "";
      // 获取类型结果
      let result = "";

      // 从URL中提取文件名
      const urlParts = fileName.split("/");
      const fullFileName = urlParts[urlParts.length - 1];

      try {
        // 截取文件后缀
        suffix = fullFileName.substr(fullFileName.lastIndexOf(".") + 1, fullFileName.length);
        // 文件后缀转小写,方便匹配
        suffix = suffix.toLowerCase();
      } catch (err) {
        suffix = "";
      }

      // fileName无后缀返回 false
      if (!suffix) {
        result = false;
        return result;
      }

      const fileTypeList = [
        // 图片类型
        { typeName: "image", types: ["png", "jpg", "jpeg", "bmp", "gif"] },
        // 文本类型
        { typeName: "txt", types: ["txt"] },
        // excel类型
        { typeName: "excel", types: ["xls", "xlsx"] },
        { typeName: "word", types: ["doc", "docx"] },
        { typeName: "pdf", types: ["pdf"] },
        { typeName: "ppt", types: ["ppt"] },
        // 视频类型
        { typeName: "video", types: ["mp4", "m2v", "mkv"] },
        // 音频
        { typeName: "radio", types: ["mp3", "wav", "wmv"] },
      ];

      for (let i = 0; i < fileTypeList.length; i++) {
        const fileTypeItem = fileTypeList[i];
        const typeName = fileTypeItem.typeName;
        const types = fileTypeItem.types;

        result = types.some(function(item) {
          return item === suffix;
        });

        if (result) {
          return typeName;
        }
      }

      return "other";
    },
    handleCancel() {
      this.$emit("update:showDialog", false);
    },
    loadFileUrl(url) {
      this.fileUrl = url
    }
  },
};
</script>

<style lang="less" scoped>
/* 可以根据需要添加样式 */
</style>

父组件调用

 <a-button
  :ghost="true"
   type="primary"
   icon="eye"
   size="small"
   @click="showFilePreview(text)">
   预览
 </a-button>
<auto-file-preview ref="autoFilePreview" :show-dialog.sync="showPreview"></auto-file-preview>
showFilePreview(url){
  this.showPreview = true
  this.$refs.autoFilePreview.loadFileUrl(this.getImgView(url))
}

show-dialog是否展示true,false

效果:
在这里插入图片描述
在这里插入图片描述

进阶版,支持word,excel,音频,视频预览,组件使用方式相同

先安装所需依赖:
npm install xlsx mammoth

<template>
  <div>
    <a-modal
      :visible="showDialog"
      :footer="null"
      @cancel="handleCancel"
      :width="800"
      :maskClosable="true"
      :keyboard="true"
    >
      <div style="overflow-y: auto; overflow-x: hidden; height: 600px">
        <div style="position: sticky; top: 0; background: white; padding: 10px 0; z-index: 1;">
          <a-button
            shape="round"
            icon="file-pdf"
            @click="handlePrint"
            size="small"
            style="margin-bottom: 10px"
          >打印</a-button>
        </div>
        <div id="printFrom">
          <pdf
            v-if="isPdf"
            ref="pdf"
            v-for="item in pageTotal"
            :src="previewFileSrc"
            :key="item"
            :page="item"
          ></pdf>
          <img
            v-else-if="isImage"
            :src="previewImage"
            style="max-width: 100%; max-height: 500px; display: block; margin: 0 auto"
          />
          <pre v-else-if="isText" style="white-space: pre-wrap; word-wrap: break-word;">
            {{ textContent }}
          </pre>
          <table v-else-if="isExcel">
            <tr v-for="(row, index) in excelData" :key="index">
              <td v-for="(value, key) in row" :key="key">{{ value }}</td>
            </tr>
          </table>
          <pre v-else-if="isWord" style="white-space: pre-wrap; word-wrap: break-word;">
            {{ wordContent }}
          </pre>
          <video
            v-else-if="isVideo"
            controls
            style="max-width: 100%; display: block; margin: 0 auto"
          >
            <source :src="previewFileSrc" :type="getVideoType(previewFileSrc)" />
            您的浏览器不支持视频标签。
          </video>
          <audio
            v-else-if="isAudio"
            controls
            style="display: block; margin: 0 auto"
          >
            <source :src="previewFileSrc" :type="getAudioType(previewFileSrc)" />
            您的浏览器不支持音频标签。
          </audio>
          <div v-else-if="isUnsupported" style="text-align: center">
            <p>不支持预览此文件类型</p>
            <a :href="previewFileSrc" download>下载文件</a>
          </div>
        </div>
      </div>
    </a-modal>
  </div>
</template>

<script>
import pdf from "vue-pdf";
import printJS from "print-js";
import * as XLSX from "xlsx";
import mammoth from "mammoth";

export default {
  name: "AutoFilePreview",
  components: { pdf },
  props: {
    showDialog: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      fileUrl: null,
      previewImage: "",
      previewFileSrc: "",
      pageTotal: null,
      isImage: false,
      isPdf: false,
      isText: false,
      isExcel: false,
      isWord: false,
      isVideo: false,
      isAudio: false,
      isUnsupported: false,
      textContent: "",
      excelData: [],
      wordContent: "",
    };
  },
  watch: {
    fileUrl: {
      immediate: true,
      handler(newVal) {
        if (newVal) {
          this.previewFileSrc = newVal;
          this.checkFileType();
        }
      },
    },
    showDialog: {
      immediate: true,
      handler(newVal) {
        if (newVal && this.fileUrl) {
          this.previewFileSrc = this.fileUrl;
          this.checkFileType();
        }
      },
    },
  },
  methods: {
    handlePrint() {
      printJS({
        printable: "printFrom",
        type: "html",
        header: "",
        targetStyles: ["*"],
        style: "@page {margin:0 10mm};",
        ignoreElements: ["no-print"],
      });
    },
    async checkFileType() {
      this.isImage = false;
      this.isPdf = false;
      this.isText = false;
      this.isExcel = false;
      this.isWord = false;
      this.isVideo = false;
      this.isAudio = false;
      this.isUnsupported = false;

      const fileType = this.matchFileType(this.fileUrl);

      if (fileType === "image") {
        this.previewImage = this.fileUrl;
        this.isImage = true;
        this.pageTotal = 1;
      } else if (fileType === "pdf") {
        this.isPdf = true;
        await this.getTotal();
      } else if (fileType === "txt") {
        await this.fetchTextFile();
        this.isText = true;
      } else if (fileType === "excel") {
        await this.fetchExcelFile();
        this.isExcel = true;
      } else if (fileType === "word") {
        await this.fetchWordFile();
        this.isWord = true;
      } else if (fileType === "video") {
        this.isVideo = true;
      } else if (fileType === "audio") {
        this.isAudio = true;
      } else {
        this.isUnsupported = true;
      }
    },
    async getTotal() {
      try {
        const loadingTask = pdf.createLoadingTask(this.previewFileSrc);
        const pdfDoc = await loadingTask.promise;
        this.pageTotal = pdfDoc.numPages;
      } catch (error) {
        console.error("PDF加载错误:", error);
        this.$message.error("PDF文件加载失败");
      }
    },
    matchFileType(fileName) {
      let suffix = "";
      try {
        suffix = fileName.split(".").pop().toLowerCase();
      } catch (e) {
        suffix = "";
      }
      const fileTypeList = [
        { typeName: "image", types: ["png", "jpg", "jpeg", "bmp", "gif"] },
        { typeName: "pdf", types: ["pdf"] },
        { typeName: "txt", types: ["txt"] },
        { typeName: "excel", types: ["xls", "xlsx"] },
        { typeName: "word", types: ["doc", "docx"] },
        { typeName: "video", types: ["mp4", "webm", "ogg", "mov", "avi"] },
        { typeName: "audio", types: ["mp3", "wav", "ogg", "aac"] },
      ];

      for (const fileTypeItem of fileTypeList) {
        if (fileTypeItem.types.includes(suffix)) {
          return fileTypeItem.typeName;
        }
      }

      return "other";
    },
    async fetchTextFile() {
      try {
        const response = await fetch(this.fileUrl);
        this.textContent = await response.text();
      } catch (error) {
        console.error("加载文本文件失败:", error);
        this.$message.error("文件加载失败");
      }
    },
    async fetchExcelFile() {
      try {
        const response = await fetch(this.fileUrl);
        const arrayBuffer = await response.arrayBuffer();
        const workbook = XLSX.read(arrayBuffer, { type: "array" });
        const firstSheetName = workbook.SheetNames[0];
        const worksheet = workbook.Sheets[firstSheetName];
        this.excelData = XLSX.utils.sheet_to_json(worksheet);
      } catch (error) {
        console.error("加载 Excel 文件失败:", error);
        this.$message.error("文件加载失败");
      }
    },
    async fetchWordFile() {
      try {
        const response = await fetch(this.fileUrl);
        const arrayBuffer = await response.arrayBuffer();
        const result = await mammoth.extractRawText({ arrayBuffer });
        this.wordContent = result.value;
      } catch (error) {
        console.error("加载 Word 文件失败:", error);
        this.$message.error("文件加载失败");
      }
    },
    getVideoType(url) {
      let suffix = "";
      try {
        suffix = url.split(".").pop().toLowerCase();
      } catch (e) {
        suffix = "";
      }
      const videoTypes = {
        mp4: "video/mp4",
        webm: "video/webm",
        ogg: "video/ogg",
        mov: "video/quicktime",
        avi: "video/x-msvideo",
      };
      return videoTypes[suffix] || "video/mp4";
    },
    getAudioType(url) {
      let suffix = "";
      try {
        suffix = url.split(".").pop().toLowerCase();
      } catch (e) {
        suffix = "";
      }
      const audioTypes = {
        mp3: "audio/mpeg",
        wav: "audio/wav",
        ogg: "audio/ogg",
        aac: "audio/aac",
      };
      return audioTypes[suffix] || "audio/mpeg";
    },
    handleCancel() {
      this.$emit("update:showDialog", false);
    },
    loadFileUrl(url) {
      this.fileUrl = url;
    },
  },
};
</script>

<style lang="less" scoped>
/* 可以根据需要添加样式 */
</style>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2384604.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

视频监控管理平台EasyCVR结合AI分析技术构建高空抛物智能监控系统,筑牢社区安全防护网

高空抛物严重威胁居民生命安全与公共秩序&#xff0c;传统监管手段存在追责难、威慑弱等问题。本方案基于EasyCVR视频监控与AI视频分析技术&#xff08;智能分析网关&#xff09;&#xff0c;构建高空抛物智能监控系统&#xff0c;实现24小时实时监测、智能识别与精准预警&…

2.2.1 05年T1复习

引言 从现在进去考研英语基础阶段的进阶&#xff0c;主要任务还是05-09年阅读真题的解题&#xff0c;在本阶段需要注意正确率。阅读最后目标&#xff1a;32-34分&#xff0c;也就是每年真题最多错四个。 做题步骤&#xff1a; 1. 预习&#xff1a;读题干并找关键词 做题&#…

Python-11(集合)

与字典类似&#xff0c;集合最大的特点就是唯一性。集合中所有的元素都应该是独一无二的&#xff0c;并且也是无序的。 创建集合 使用花括号 set {"python","Java"} print(type(set)) 使用集合推导式 set {s for s in "python"} print(set…

Opixs: Fluxim推出的全新显示仿真模拟软件

Opixs 是 Fluxim 最新研发的显示仿真模拟软件&#xff0c;旨在应对当今显示技术日益复杂的挑战。通过 Opixs&#xff0c;研究人员和工程师可以在制造前&#xff0c;设计并验证 新的像素架构&#xff0c;从而找出更功节能、色彩表现更优的布局方案。 Opixs 适用于学术研究和工业…

佰力博与您探讨PVDF薄膜极化特性及其影响因素

PVDF&#xff08;聚偏氟乙烯&#xff09;薄膜的极化是其压电性能形成的关键步骤&#xff0c;通过极化处理可以显著提高其压电系数和储能能力。极化过程涉及多种方法和条件&#xff0c;以下从不同角度详细说明PVDF薄膜的极化特性及其影响因素。 1、极化方法 热极化&#xff1a;…

自动获取ip地址安全吗?如何自动获取ip地址

在数字化网络环境中&#xff0c;IP地址的获取方式直接影响设备连接的便捷性与安全性。自动获取IP地址&#xff08;通过DHCP协议&#xff09;虽简化了配置流程&#xff0c;但其安全性常引发用户疑虑。那么&#xff0c;自动获取IP地址安全吗&#xff1f;如何自动获取IP地址&#…

STM32:深度解析RS-485总线与SP3485芯片

32个设备 知识点1【RS-485的简介】 RS-485是一种物理层差分总线标准&#xff0c;在串口的基础上演变而来&#xff1b; 两者虽然不在同一层次上直接对等&#xff0c;但在实际系统中&#xff0c;往往使用RS-485驱动差分总线&#xff0c;将USART转换为适合长距离、多点通信的物…

亚马逊搜索代理: 终极指南

文章目录 前言一、为什么需要代理来搜索亚马逊二、如何选择正确的代理三、搜索亚马逊的最佳代理类型四、为亚马逊搜索设置代理五、常见挑战及克服方法六、亚马逊搜索的替代方法总结 前言 在没有代理的情况下搜索亚马逊会导致 IP 禁止、验证码和速度限制&#xff0c;从而使数据…

C++笔记-封装红黑树实现set和map

1.源码及框架分析 上面就是在stl库中set和map的部分源代码。 通过上图对框架的分析&#xff0c;我们可以看到源码中rb_tree⽤了⼀个巧妙的泛型思想实现&#xff0c;rb_tree是实 现key的搜索场景&#xff0c;还是key/value的搜索场景不是直接写死的&#xff0c;⽽是由第⼆个模板…

留给王小川的时间不多了

王小川&#xff0c;这位头顶“天才少年”光环的清华学霸、搜狗输入法创始人、中国互联网初代技术偶像&#xff0c;正迎来人生中最难啃的硬骨头。 他在2023年创立的百川智能&#xff0c;被称为“大模型六小虎”之一。今年4月&#xff0c;王小川在全员信中罕见地反思过去两年工作…

国产频谱仪性能如何?矢量信号分析仪到底怎么样?

矢量信号分析仪是一种高性能的电子测量设备&#xff0c;具备频谱分析、矢量信号分析、实时频谱分析、脉冲信号分析、噪声系数测量、相位噪声测量等多种功能。它能够对各类复杂信号进行精确的频谱特性分析、调制质量评估、信号完整性检测以及干扰源定位等操作。广泛应用于通信、…

熔断器(Hystrix,Resilience4j)

熔断器 核心原理​ 熔断器通过监控服务调用失败率&#xff0c;在达到阈值时自动切断请求&#xff0c;进入熔断状态&#xff08;类似电路保险丝&#xff09;。其核心流程为&#xff1a; 关闭状态&#xff08;Closed&#xff09;​​&#xff1a;正常处理请求&#xff0c;统计失…

C++23 容器从其他兼容范围的可构造性与可赋值性 (P1206R7)

文章目录 背景与动机提案内容与实现细节提案 P1206R7实现细节编译器支持 对开发者的影响提高灵活性简化代码向后兼容性 总结 C23标准引入了对容器构造和赋值的新特性&#xff0c;这些特性使得容器能够更灵活地从其他兼容范围初始化&#xff0c;并支持从范围赋值。这些改进由提案…

多通道振弦式数据采集仪MCU安装指南

设备介绍 数据采集仪 MCU集传统数据采集器与5G/4G,LoRa/RS485两种通信功能与一体的智能数据采集仪。该产品提供振弦、RS-485等的物理接口&#xff0c;能自动采集并存储多种自然资源、建筑、桥梁、城市管廊、大坝、隧道、水利、气象传感器的实时数据&#xff0c;利用现场采集的数…

SOC-ESP32S3部分:9-GPIO输入按键状态读取

飞书文档https://x509p6c8to.feishu.cn/wiki/L6IGwHKV6ikQ08kqwAwcAvhznBc 前面我们学习了GPIO的输出&#xff0c;GPIO输入部分其实也是一样的&#xff0c;这里我们使用按键作为GPIO输入例程讲解&#xff0c;分三步走。 查看板卡原理图&#xff0c;确定使用的是哪个GPIO查看G…

Ubuntu20.04的安装(VMware)

1.Ubuntu20.04.iso文件下载 下载网址&#xff1a;ubuntu-releases-20.04安装包下载_开源镜像站-阿里云 2.创建虚拟环境 2.1打开VMware与创建新虚拟机 点击创建新虚拟机 如果没下好可以点击稍后安装操作系统 选择linux版本选择Ubuntu 64位然后点击下一步。 注意这里需要选择一…

【论文阅读】LLaVA-OneVision: Easy Visual Task Transfer

LLaVA-OneVision: Easy Visual Task Transfer 原文摘要 研究背景与目标 开发动机&#xff1a; 基于LLaVA-NeXT博客系列对数据、模型和视觉表征的探索&#xff0c;团队整合经验开发了开源大型多模态模型 LLaVA-OneVision。 核心目标&#xff1a; 突破现有开源LMM的局限&#xf…

Spring Boot 项目多数据源配置【dynamic datasource】

前言&#xff1a; 随着互联网的发展&#xff0c;数据库的读写分离、数据迁移、多系统数据访问等多数据源的需求越来越多&#xff0c;我们在日常项目开发中&#xff0c;也不可避免的为了解决这个问题&#xff0c;本篇来分享一下在 Spring Boot 项目中使用多数据源访问不通的数据…

JAVA查漏补缺(2)

AJAX 什么是Ajax Ajax&#xff08;Asynchronous Javascript And XML&#xff09;&#xff0c;即是异步的JavaScript和XML&#xff0c;Ajax其实就是浏览器与服务器之间的一种异步通信方式 异步的JavaScript 它可以异步地向服务器发送请求&#xff0c;在等待响应的过程中&…

【Web前端】JavaScript入门与基础(二)

Javascript对象 什么是对象&#xff1f;对象&#xff08;object&#xff09;是 JavaScript 语言的核心概念&#xff0c;也是最重要的数据类型。简单说&#xff0c;对象就是一组“键值对”&#xff08;key-value&#xff09;的集合&#xff0c;是一种无序的复合数据集合。 var…