当前位置: 首页 > news >正文

企业级-pdf预览-前后端

作者:fyupeng
技术专栏:☞ https://github.com/fyupeng
项目地址:☞ https://github.com/fyupeng/distributed-blog-system-api


留给读者

本文

一、介绍

对于PDF预览,有很多开发者都遇到过头疼的难题,今天给大家介绍一个比较成熟的浏览器直接提供的预览嵌入Vue组件。

二、代码

  • 快速使用:

html渲染:

    <el-dialog:lock-scroll="false"custom-class="imgPreview_dia":show-close="true"top="5vh"width="90vw"v-model="dialogVisible"><filePreview :fileUrl="fileUrl" width="90vw" height="90vh"></filePreview></el-dialog>

css样式:

.imgPreview_dia {border-radius: 0 !important;margin: 0;left: 5vw;
}

js 处理:

const dialogVisible = ref(false);const fileUrl = ref("");const handleYulan = (index: number, row: object) => {fileUrl.value =// showDocFile() +"http://localhost:8086/machine/commonFile/notice/showDocFile" +"?fileId=" +row.fileId +"&pripid=" +props.initData?.pripid +"&time=" +new Date().getTime();// fileUrl.value =//   showDocFile() +//   "?docId=2516&gid=4055fb556a9c440590464017f28aae0e&time=1691460960121";dialogVisible.value = true;
};
  • 模板:

后端处理:

 public Boolean showNoticeDocFile(String fileId, HttpServletResponse response) {FileInputStream input = null;OutputStream output = null;try {File file = getNoticeDocRowFile(fileId);if (file == null || !file.exists()) {return false;}input = new FileInputStream(file);int i = input.available(); // 得到文件大小byte[] data = new byte[i];input.read(data); // 读数据response.setContentType("application/pdf"); // 设置返回的文件类型response.addHeader("Content-Length", String.valueOf(data.length));  //文件大小output = response.getOutputStream(); // 得到向客户端输出二进制数据的对象output.write(data); // 输出数据output.flush();file.delete();return true;} catch (Exception e) {logger.error("==showFile==文件信息回显失败!fileId:{}", fileId, e);return false;} finally {try {if (input != null)input.close();if (output != null)output.close();} catch (IOException e) {logger.error(e.getMessage(), e);}}}

前端处理:
filePreview.vue

<template><div class="wrap" v-loading="isLoading"><div class="pdf-container"><iframeid="doc-iframe":src="url":style="{ width: prop.width, height: prop.height, background: '#fff' }"class="parintDetailDoc"ref="iframeRef"></iframe></div></div>
</template><script lang="ts" setup>
import { ref, reactive, watch, defineProps, defineEmits, onMounted } from "vue";
import { ElMessage } from "element-plus";
const emits = defineEmits(["loaded"]);
const prop = defineProps({fileUrl: {type: String,default: "",},width: {type: String,default: "800px",},height: {type: String,default: "800px",},
});
const isLoading = ref(true);
const url = ref("");watch(() => prop.fileUrl,() => {url.value = prop.fileUrl;console.log("--------prop.fileUrl", prop.fileUrl);},{ immediate: true }
);
onMounted(() => {let iframeDom = document.getElementById("doc-iframe") || {};iframeDom.onload = function () {isLoading.value = false;emits("loaded");};
});
</script><style scoped>
.parintDetailDoc::-webkit-scrollbar {display: none;
}
.parintDetailDoc {border: none;max-width: 100%;
}
</style>

三、总结

简洁、高效、实用!

相关文章:

  • Qt 常用数据类型
  • 【github remote: Access denied等问题的通用解决方案】
  • EHS管理系统设备安全设施安全监控模块
  • 目标检测-数据集
  • Win11家庭版找不到gpedit.msc文件怎么办
  • Apache APISIX学习(1):介绍、docker启动
  • docker - 迁移和备份
  • DasViewer浏览器中的格式转换,与网格大师的转换有什么区别?
  • 如何将 Apifox 的自动化测试与 Jenkins 集成?
  • iOS开发工程师面试
  • 使用 Llama 3.1 和 Qdrant 构建多语言医疗保健聊天机器人的步骤
  • 【Hadoop】一、Hadoop入门:基础配置、集群配置、常用脚本
  • 爬虫逆向学习(九):记录一个集cookie、请求参数、请求体、响应文本加密的站点反爬
  • Cpp内存管理(7)
  • 海信智能电视的使用心得
  • ----------
  • interface和setter,getter
  • JavaScript学习总结——原型
  • npx命令介绍
  • python大佬养成计划----difflib模块
  • seaborn 安装成功 + ImportError: DLL load failed: 找不到指定的模块 问题解决
  • Vue.js-Day01
  • Vultr 教程目录
  • WebSocket使用
  • 安装python包到指定虚拟环境
  • 开发基于以太坊智能合约的DApp
  • 前端技术周刊 2019-02-11 Serverless
  • 首页查询功能的一次实现过程
  • 我与Jetbrains的这些年
  • 与 ConTeXt MkIV 官方文档的接驳
  • [Shell 脚本] 备份网站文件至OSS服务(纯shell脚本无sdk) ...
  • Nginx惊现漏洞 百万网站面临“拖库”风险
  • 新年再起“裁员潮”,“钢铁侠”马斯克要一举裁掉SpaceX 600余名员工 ...
  • ( )的作用是将计算机中的信息传送给用户,计算机应用基础 吉大15春学期《计算机应用基础》在线作业二及答案...
  • (31)对象的克隆
  • (C++)八皇后问题
  • (delphi11最新学习资料) Object Pascal 学习笔记---第7章第3节(封装和窗体)
  • (SERIES12)DM性能优化
  • (ZT)出版业改革:该死的死,该生的生
  • (二)Eureka服务搭建,服务注册,服务发现
  • (四)Linux Shell编程——输入输出重定向
  • (算法)硬币问题
  • (转)详解PHP处理密码的几种方式
  • (轉貼) 資訊相關科系畢業的學生,未來會是什麼樣子?(Misc)
  • .net core开源商城系统源码,支持可视化布局小程序
  • .Net MVC + EF搭建学生管理系统
  • .Net MVC4 上传大文件,并保存表单
  • .NET Remoting学习笔记(三)信道
  • .NET Windows:删除文件夹后立即判断,有可能依然存在
  • .net 发送邮件
  • .NET单元测试
  • .NET应用架构设计:原则、模式与实践 目录预览
  • [ vulhub漏洞复现篇 ] Apache Flink目录遍历(CVE-2020-17519)
  • [ 环境搭建篇 ] 安装 java 环境并配置环境变量(附 JDK1.8 安装包)
  • [001-03-007].第07节:Redis中的管道