vue使用canvas渲染pdf

news/2024/7/10 22:38:43 标签: vue, 前端, es6

1.原生方法

<div>
	<div class="bottom" style="position: fixed;right: 0">
	  <input class="pdf-zoom" type="submit" value="+" @click="zoomPdf(1)"/>
	</br>
	<input class="pdf-zoom" type="submit" value="-" @click="zoomPdf(0)"/>
</div>
	<div id="canvasEle"></div>
</div>

pdfContent: {
       canvasEle: '', // 渲染pdf的元素
       detail: '', // pdf详情
       scale: 1 // 放缩比例
     },
     
     this.pdfContent['canvasEle'] = document.getElementById("canvasEle");
         this.pdfContent['detail'] = res.data;
         this.loadPdf(this.pdfContent, 0);
    /**
    * pdf放大/缩小
    */
   zoomPdf(num) {
     const pObjs = this.pdfContent.canvasEle.childNodes;
     for (let i = pObjs.length - 1; i >= 0; i--) { // 一定要倒序,正序是删不干净的,可自行尝试
       this.pdfContent.canvasEle.removeChild(pObjs[i]);
     }
     if (num) {
       this.pdfContent.scale = this.pdfContent.scale + 0.5;
     } else {
       this.pdfContent.scale >  1 ? this.pdfContent.scale = this.pdfContent.scale - 0.5 : ''
     }
     this.loadPdf(this.pdfContent);
   },
   /**
    * 加载pdf
    */
   async loadPdf(pdfContent, flag){
     const pdfData = atob(pdfContent.detail)
     pdfjsLib.GlobalWorkerOptions.workerSrc ='https://cdn.jsdelivr.net/npm/pdfjs-dist@2.4.456/build/pdf.worker.js';
     const loadingTask = pdfjsLib.getDocument({ data: pdfData, });
     const pdf = await loadingTask.promise;
     for (let i = 1; i < pdf.numPages + 1; i++) {
       const page = await pdf.getPage(i);
       let viewport = page.getViewport({ scale: pdfContent.scale });
       this.proportion = this.clientWidth / viewport.width;
       if (flag === 0) {
         this.pdfContent.scale = this.pdfContent.scale * this.proportion;
         viewport = page.getViewport({ scale: pdfContent.scale });
       }
       let canvas = document.createElement('canvas');
       let context = canvas.getContext('2d');
       canvas.height = viewport.height;
       canvas.width = viewport.width;
       const renderContext = {
         canvasContext: context,
         viewport,
       };
       pdfContent.canvasEle.appendChild(canvas);
       page.render(renderContext);
     }
   }

vuepdf_64">2.使用vue-pdf

(1)安装

npm install --save vue-pdf

(2)基本使用

<template>
<div class="pdf">
  <pdf 
    ref="pdf"
    :src="pdfUrl">
  </pdf>
</div>
</template>
<script>
import pdf from 'vue-pdf'
export default {
  name: 'Pdf',
  components:{
      pdf
  },
  data(){
      return {
          pdfUrl:"http://file.dakawengu.com/file/2018-05-29/20180527-tianfeng.pdf",
      }
  }
</script>

http://www.niftyadmin.cn/n/555213.html

相关文章

C语言函数指针

转自http://www.cnblogs.com/haore147/p/3647262.html 举例&#xff1a; 1 //信号注册函数2 type void(*sighandler_t)(int)//返回void,形参int的函数指针3 4 sighandler_t signal(int signum,sighandler_t handler);5 /*6 声明一个函数7 返回值类型&#xff1a;sighandler_t …

CSS实现多种图形

1、邮件虚线 .uni-grid-item-index {position: relative;overflow: hidden;border-top: 3rpx solid transparent;background: linear-gradient(white, white) padding-box,repeating-linear-gradient(-45deg, red 0, red 12.5%, transparent 0, transparent 25%, #58a 0, #58a…

[转载]分布式系统架构经典资料

基础理论 CAP 定理Fallacies of Distributed Computing经典资料 Distributed systems theory for the distributed systems engineerFLP Impossibility ResultAn introduction to distributed systemsDistributed Systems for fun and profitDistributed Systems: Principles a…

ucharts的引入和使用

1.官网 在线配置网址&#xff1a;https://www.ucharts.cn/ 进入首页后&#xff0c;点击“在线工具”&#xff0c;然后点击“在线生成工具”&#xff0c;如图: 这两个按钮都可以进入&#xff1a; 2.项目引入 hbuilderx的版本要是3.1以上&#xff0c;从uniapp的插件市场导入…

实现一个事件总线(vue.prototype.$bus)?

本质就是一个订阅发布模式的实现。 维护一个cache数组&#xff0c;即订阅者数组实现on函数&#xff0c;即增加订阅者实现off函数&#xff0c;即取消订阅实现emit函数&#xff0c;即发布消息&#xff0c;通知订阅中心有更新 class EventBus {constructor() {this.cache {};}o…

项目使用sqlite

1.sqlite语法学习 官网&#xff1a;https://www.sqlite.org/index.html 菜鸟教程&#xff1a;https://www.runoob.com/sqlite/sqlite-tutorial.html 2.sqlite的封装 module.exports {DBName: name,DBPath: c/path,/*** 打开数据库*/openDB: function() { // 打开数据库retu…

margin为负值怎么解释?

文章目录前言margin四值顺序margin表现margin为负值1.margin-left,margin-right为负值元素本身没有宽度元素本身有宽度2.margin-top为负值3.margin-bottom为负值前言 margin设置为负值往往用于实现双飞翼布局、圣杯布局中。我本人对这些布局实现的原理还有点懵&#xff0c;故整…

Java的AOP介绍

1.概念 AOP是Aspect Oriented Programming的缩写&#xff0c;意思是面向方面编程&#xff0c;与OOP(Object Oriented Programming)面向对象编程对等&#xff0c;都是一种编程思想。从OOP角度分析&#xff0c;我们关注业务的处理逻辑&#xff0c;是属于纵向的行为&#xff0c;从…