vue实现一个pdf在线预览,pdf选择文本并提取复制文字触发弹窗效果

TOC\]![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/1056756dbaaf4f1a8139d051f4d497f7.png) ### 一、文件预览 #### 1、安装依赖包 这里安装了[email protected]版本,安装过程中报错缺少worker-loader > npm i [email protected] [email protected] #### 2、模板部分 #### 3、js部分(核心) 核心代码如下: 1. 利用 PDF.getDocument获取pdf基础数据 2. 通过canvas将pdf渲染到canvas画布上 ##### 可能出现的问题: (1) 页面文字可选中,但文本不可见 通过测试发现,将 pdfjs-dist/web/pdf_viewer.css 路径下的 color 属性注释后可显示文本。 .textLayer span, .textLayer br { /* color: transparent; */ position: absolute; white-space: pre; cursor: text; transform-origin: 0% 0%; } ##### pdf多页面处理 1. 模板处理id作为唯一标识 2. 修改canvas渲染逻辑,主要通过递归的方式逐一渲染 renderPage(num) { this.pdfDoc.getPage(num).then((page) => { const canvas = document.getElementById(`page-${num}`); const ctx = canvas.getContext("2d"); const viewport = page.getViewport({ scale: this.pdfScale }); canvas.width = viewport.width; canvas.height = viewport.height; const renderContext = { canvasContext: ctx, viewport, }; page.render(renderContext); if (num < this.pdfPages) { this.renderPage(num + 1); } }); }, ### 二、文本选中与弹窗(核心代码) Promise.all([getTextContentPromise, renderPagePromise]) .then(([textContent]) => { const textLayerDiv = document.createElement("div"); // 注意:此处不要修改该元素的class名称,该元素的样式通过外部导入,名称是固定的 textLayerDiv.setAttribute("class", "textLayer"); // 设置容器样式 textLayerDiv.setAttribute( "style", ` z-index: 1; opacity: .2; // background-color:#fff; // transform: scale(1.1); width: 100%, height: 100%, `, ); // 设置容器的位置和宽高 textLayerDiv.style.left = canvas.offsetLeft + "px"; textLayerDiv.style.top = canvas.offsetTop + "px"; textLayerDiv.style.height = canvas.offsetHeight + "px"; textLayerDiv.style.width = canvas.offsetWidth + "px"; const textView = document.querySelector("#text-view"); textView.appendChild(textLayerDiv); const textLayer = new TextLayerBuilder({ // container: , textLayerDiv: textLayerDiv, pageIndex: page.pageIndex, viewport: viewport, eventBus, // textDivs: [] }); textLayer.setTextContent(textContent); textLayer.render(); // 当选择文本后鼠标取消点击时触发 textLayerDiv.addEventListener("mouseup", () => { // // 隐藏文本层 // textLayerDiv.style.display = 'none'; // 是否选择了文本 const isTextSelected = window.getSelection().toString().trim() !== ""; if (isTextSelected) { //选择的文本内容 const selectedText = window.getSelection().toString(); console.log("Selected text:", selectedText); if (selectedText) { alert(selectedText); } } }); }) .catch((error) => { console.error("Error rendering page:", error); }); ### 三、完整代码如下

相关推荐
编码七号7 分钟前
【axios取消请求】如何在token过期后取消未响应的请求
java·前端·javascript
张开心_kx8 分钟前
面试官又问我是否了解React的单向数据流
前端·javascript·react.js
Web漫游9 分钟前
🔥2025年了你还不会使用Vue3?
javascript·vue.js
残轩11 分钟前
Win10 家庭版 Docker 环境搭建详解(基于 WSL2)
前端·后端·docker
palpitation9712 分钟前
Flutter分解布局选择辅助方法还是Widget?
前端
工呈士13 分钟前
HTML响应式网页设计与跨平台适配
前端·html
作曲家种太阳13 分钟前
第六章节 响应式的 computed 实现【手摸手带你实现一个vue3】
前端
腊月廿二15 分钟前
跨项目频繁切换node版本号(nvm-windows)
前端
策码16 分钟前
MutationObserver监听网页二次渲染和子节点变化
前端·javascript
前端大白话17 分钟前
前端必知!HTML中`<a>`标签target属性全攻略:新窗口、当前窗口、指定框架一网打尽
前端·架构·html