当前位置: 首页 > 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>

三、总结

简洁、高效、实用!

http://www.lryc.cn/news/446068.html

相关文章:

  • 为什么 qt 成为 c++ 界面编程的第一选择?
  • Day1-顺序表
  • PostgreSQL - pgvector 插件构建向量数据库并进行相似度查询
  • UR机器人坐标系转化
  • 【每日一题】LeetCode 2306.公司命名(位运算、数组、哈希表、字符串、枚举)
  • 240922-chromadb的基本使用
  • 工厂模式和抽象工厂模式的实验报告
  • C标准库<string.h>-str、strn开头的函数
  • Anaconda/Miniconda的删除和安装
  • 【Harmony】轮播图特效,持续更新中。。。。
  • Go 并发模式:管道的妙用
  • CAN通信详解
  • 52 文本预处理_by《李沐:动手学深度学习v2》pytorch版
  • 【python】字符串扩展-格式化的精度控制
  • C++第一次练习
  • 计算机毕业设计 基于Python的医疗预约与诊断系统 Django+Vue 前后端分离 附源码 讲解 文档
  • JAVA基础:正则表达式,String的intern方法,StringBuilder可变字符串特点与应用,+连接字符串特点
  • 前端接口报错302 [已解决]
  • 【网络安全】利用未授权API接口实现创建Support Ticket
  • 气压高度加误差的两种方法(直接添加 vs 换算到气压误差),附MATLAB程序
  • Word 制作会议名牌教程
  • 浮动静态路由
  • JavaWeb初阶 day1
  • OpenAPI鉴权(二)jwt鉴权
  • 【Rust练习】16.模式
  • 深度学习(4):torch.nn.Module
  • (14)关于docker如何通过防火墙做策略限制
  • 新React开发人员应该如何思考
  • 解密.bixi、.baxia勒索病毒:如何安全恢复被加密数据
  • 开源 AI 智能名片与 S2B2C 商城小程序:嫁接权威实现信任与增长