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

vue项目pdf文件的预览

1.下载
您可以在以下网址下载pdfjsLib:https://github.com/mozilla/pdf.js
pdfjsLib是一个开源项目,您可以在GitHub上找到其源代码和相关资源。
2.放置文件位置
在这里插入图片描述
3.进入
在index.html引入

<script src="<%= BASE_URL %>static/pdfjs-dist/build/pdf.js"></script>

4.获取pdf文件的信息并转换为链接

<div class="createModel"><pdf-viewer :url="pdfUrl" ref="pdf" /></div>
methods: {// 初始化数据initDate () {this.getPDFURL()},async getPDFURL () {if (this.fileObj.id) {try {const data = await uploadApi.getPDF(this.fileObj.id)const blob = new Blob([data]);this.pdfUrl = window.URL.createObjectURL(blob)this.$nextTick(() => {this.$refs.pdf.init()})} catch (error) {console.log(error)this.clearData()}}},clearData () {if (this.pdfUrl) {window.URL.revokeObjectURL(this.pdfUrl)}this.pdfUrl = ''this.$nextTick(() => {this.$refs.pdf?.destroyedData()})}},beforeDestroy () {this.clearData()},

5.在PdfViewer.vue组件中

methods: {init () {this.renderPDF();},async renderPDF () {if (!this.url) returnlet containerWidth = document.querySelector('.pdf-container').offsetWidththis.scale = containerWidth / 594const pdf = await pdfjsLib.getDocument(this.url).promise;console.log('pdf', pdf)this.numPages = pdf.numPages;this.$nextTick(async () => {for (let i = 1; i <= this.numPages; i++) {const canvasId = 'pdfCanvas-' + i;const canvasEl = this.$refs[canvasId][0];this.canvasEls.push(canvasEl);const page = await pdf.getPage(i);const viewport = page.getViewport({ scale: this.scale });canvasEl.height = viewport.height;canvasEl.width = viewport.width;const canvasContext = canvasEl.getContext('2d');await page.render({canvasContext,viewport,}).promise;}})},destroyedData () {this.numPages = 0this.canvasEls = []}},
http://www.lryc.cn/news/226247.html

相关文章:

  • 企业计算机中了mkp勒索病毒怎么办,服务器中了勒索病毒如何处理
  • Android拖放startDragAndDrop拖拽Glide加载堆叠圆角图,Kotlin(5)
  • 1994-2021年分行业二氧化碳排放量数据
  • 如何进行Go程序的打包发布
  • python工具HIKVISION视频编码设备接入网关任意文件下载
  • [NLP] 使用Llama.cpp和LangChain在CPU上使用大模型
  • 开发知识点-Ant-Design-Vue
  • 2022最新版-李宏毅机器学习深度学习课程-P50 BERT的预训练和微调
  • Android codec2 视频框架 之输入buffer
  • Python实现局部二进制算法(LBP)
  • 如何评价现在的CSGO游戏搬砖市场
  • ResourceQuota对象在K8s上的说明
  • 悟空crm二次开发 增加客户保护功能 (很久没有消息,但是有觉得有机会的客户)就进入了保护转态
  • k8s之配置资源管理
  • 赛氪助力全国大学生数学竞赛山东赛区圆满举办
  • pytorch基础语法问题
  • 【面试经典150 | 】颠倒二进制位
  • 十分钟了解自动化测试
  • Redis配置文件
  • [量化投资-学习笔记009]Python+TDengine从零开始搭建量化分析平台-KDJ
  • Activiti6工作流引擎:Form表单
  • Fortran 中的指针
  • 第七章 块为结构建模 P4|系统建模语言SysML实用指南学习
  • 提升中小企业效率的不可或缺的企业云盘网盘
  • Web 安全之时序攻击 Timing Attack 详解
  • 【objectarx.net】定时器的使用
  • C++:容器list的介绍及使用
  • 元核云亮相金博会,智能质检助力金融合规
  • Harmony 应用开发的知识储备
  • (层次遍历)104. 二叉树的最大深度