vue h5项目预览pdf文件+电子签名

8 篇文章 1 订阅
2 篇文章 0 订阅

vue h5项目预览pdf文件+电子签名
在这里插入图片描述

1、vue安装pdfh5

npm install pdfh5

2、示例代码
pdf文件预览页面

<template>
    <div class="pdf-container>
        <div id="pdfView"></div>
        <div v-if="SignPanelShow=='0'" class="pdfbtn" @click="openSignPanel">签名</div>
        <!-- 手签版弹出层-->
        <van-popup class="popup" v-model="signPopup" position="bottom" :style="{ height: '50%' }" closeable>
            <div class="popup-title">请手签您的姓名全称</div>
            <div class="popup-content">
            <canvas isee_sign ref="signCanvas"></canvas>
            </div>
            <div class="popup-panel">
            <van-cell>
                <van-button type="default" style="width: 49%;" @click="clear">清除</van-button>
                <van-button type="info" style="width: 49%;" @click="saveImageInfo">确认</van-button>
            </van-cell>
            </div>
        </van-popup>
        
    </div>
  </template>

js部分

 <script>
    import Pdfh5 from "pdfh5";
    import { initCanvas, clearArea } from '../assets/js/canvas.js';
    export default {
      name: 'App',
      data() {
        return {
            pdfh5: null,
            SignPanelShow:'', // 签字状态
            signPopup: false,
            /** 手签版校验 */
            drawed: false,
            uuid: '',
        };
      },
      components: {
          pdf,
      },
      mounted() {
        let o = this.getParam();
        this.uuid = o.uuid;
        // 获取pdf文件
        this.querypdf()
        // 获取签字状态
        this.getsignpanetype()
      },
      methods:{
      	// 获取地址栏参数
        getParam() {
          var urlStr = location.href;
          if (typeof urlStr == "undefined") {
            var url = decodeURI(location.search); //获取url中"?"符后的字符串
          } else {
            var url = "?" + urlStr.split("?")[1];
          }
          var theRequest = new Object();
          if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            var strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
              theRequest[strs[i].split("=")[0]] = decodeURI(strs[i].split("=")[1]);
            }
          }
          return theRequest;
        },
          querypdf(){
              this.$axios({
                  url:"api/propertyServer/view/"+ this.uuid,
                  method:"get",//get请求方式
                  params:'', //传给后台的参数
                  responseType: 'blob'
              }).then(res=>{
                  console.log('pdf',res)
                  if(res.status ==200){
                    this.pdfh5 = new Pdfh5("#pdfView", {
                        pdfurl: window.URL.createObjectURL(new Blob([res.data])),
                        zoomEnable:false, // 是否允许pdf手势缩放
                    });
                  }
              }).catch(error=>{});
  
          },
          getsignpanetype(){
            this.$axios({
                url:'api/propertyServer/Status/' + this.uuid,
                method:"get",//get请求方式
            }).then(res=>{
                if(res.status ==200){
                    this.SignPanelShow = res.data
                }
                console.log('签字状态',res,res.data)
            }).catch(error=>{});
          },
          // 打开签字板
          openSignPanel: function () {
            this.signPopup = true
            this.$nextTick(() => { initCanvas(this) })
          },
          // 清空签字
          clear: function () {
            clearArea(this)
            this.drawed = false
          },
          // 签字确认
          saveImageInfo(){
            if (!this.drawed) {
              this.$toast.fail('请签字')
              return
            }
                  this.$toast.loading({duration: 0, forbidClick: true, message: '正在加载', overlay: true})
                  this.signPopup = false
                  this.$axios({
                        url:"api/propertyServer/sign/"+ this.uuid,
                        method:"post",
                        data:{
                          image:this.canvas.toDataURL()
                        },
                  }).then(res=>{
                        if(res.status ==200){
                          // 更新签字状态
                          this.getsignpanetype()
                          this.$toast.clear()
                        }
                  }).catch(error=>{});
          }       
      }
    }
  </script>

css部分

<style >
	@import "pdfh5/css/pdfh5.css";
	*{
	padding: 0;
	margin: 0;
	}
	html,body,#app {
	width: 100%;
	height: 100%;
	padding-bottom: 7px;
	box-sizing: border-box;
	}
</style>
  <style scoped>
      .pdf-container{
          width: 100%;
          height: 100vh;
      }
      /* 签字按钮 */
	.pdfbtn{
		width: 100%;
		height: 45px;
		position: fixed;
		left: 0;
		bottom: 0;
		font-size: 18px;
		color: #fff;
		background-color: rgb(69, 135, 246);
		text-align: center;
		line-height: 45px;
	}
    /* 客户签字版 弹出层 */
	.popup {
	  display: flex;
	  flex-direction: column;
	  overflow-y: auto;
	}
    .popup-title {
	    font-size: 20px;
	    font-weight: 600;
	    text-align: center;
	    margin-top: 20px;
	  }
      .popup-content {
	    overflow-y: hidden;
	    height: 70%;
	  }
  </style>

签字板canvas.js

/** 初始化canvas画布 */
export const initCanvas = that => {
  /** canvas初始化 */
  let canvas = that.$refs.signCanvas
  that.canvas = canvas
  that.ctx = canvas.getContext('2d')
  let winW = window.innerWidth
  let winH = window.innerHeight / 3
  canvas.width = winW
  canvas.height = winH
  /** 按下屏幕事件 */
  that.canvas.addEventListener(
    'touchstart',
    (event) => {
      if (event.targetTouches.length === 1) {
        const offset = (window.innerHeight / 2) // 起始偏移量由canvas画布在屏幕中的位置决定 这里是50%
        event.preventDefault() // 阻止浏览器默认事件,重要
        var touch = event.targetTouches[0]
        that.touchPressed = true
        draw(touch.pageX - that.canvas.offsetLeft, touch.pageY - that.canvas.offsetTop - offset, false, that)
      }
    }, false)
  /** 拖动屏幕事件 */
  that.canvas.addEventListener(
    'touchmove',
    (event) => {
      if (event.targetTouches.length === 1) {
        const offset = (window.innerHeight / 2) // 起始偏移量由canvas画布在屏幕中的位置决定 这里是50%
        event.preventDefault() // 阻止浏览器默认事件,重要
        var touch = event.targetTouches[0]
        if (that.touchPressed) {
          draw(touch.pageX - that.canvas.offsetLeft, touch.pageY - that.canvas.offsetTop - offset, true, that)
        }
      }
    }, false)
  /** 结束拖动屏幕事件 */
  that.canvas.addEventListener(
    'touchend',
    (event) => {
      if (event.targetTouches.length === 1) {
        event.preventDefault() // 阻止浏览器默认事件,防止手写的时候拖动屏幕,重要
        that.touchPressed = false
      }
      that.drawed = true
    }, false)
}

/** 根据坐标与偏移量绘制图案 */
function draw (x, y, isDown, that) {
  let ctx = that.ctx
  if (isDown) {
    ctx.beginPath()
    ctx.strokeStyle = that.strokeStyle
    ctx.lineWidth = that.lineWidth
    ctx.lineJoin = 'round'
    ctx.moveTo(that.lastX, that.lastY)
    ctx.lineTo(x, y)
    ctx.closePath()
    ctx.stroke()
  }
  that.lastX = x
  that.lastY = y
}

/** 清空画板 */
export const clearArea = that => {
  that.ctx.setTransform(1, 0, 0, 1, 0, 0)
  that.ctx.clearRect(0, 0, that.ctx.canvas.width, that.ctx.canvas.height)
}

参考文章链接 https://pdfh5.gjtool.cn/#/pages/index/detail?id=646781ec0c801ca87845c820

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue H5可以通过使用第三方库来实现PDF文件预览。一个常用的库是pdf.js,它是一个由Mozilla开发的JavaScript库,用于在网页上渲染PDF文件。 以下是实现PDF文件预览的步骤: 1. 安装pdf.js库:可以通过npm或者直接引入CDN来安装pdf.js库。 2. 创建一个Vue组件:在Vue项目中创建一个组件,用于显示PDF文件。 3. 引入pdf.js库:在组件中引入pdf.js库。 4. 加载PDF文件:使用pdf.js提供的API,加载需要预览PDF文件。 5. 渲染PDF文件:使用pdf.js提供的API,将PDF文件渲染到页面上。 下面是一个简单的示例代码: ```vue <template> <div> <canvas ref="pdfCanvas"></canvas> </div> </template> <script> import pdfjsLib from 'pdfjs-dist' export default { mounted() { this.loadPDF() }, methods: { async loadPDF() { const pdfUrl = 'path/to/your/pdf/file.pdf' const loadingTask = pdfjsLib.getDocument(pdfUrl) const pdf = await loadingTask.promise const page = await pdf.getPage(1) const canvas = this.$refs.pdfCanvas const context = canvas.getContext('2d') const viewport = page.getViewport({ scale: 1 }) canvas.height = viewport.height canvas.width = viewport.width const renderContext = { canvasContext: context, viewport: viewport } await page.render(renderContext) } } } </script> ``` 在上面的示例中,我们使用了pdf.js库来加载和渲染PDF文件。首先,我们通过`getDocument`方法加载PDF文件,然后获取第一页的内容,并将其渲染到一个canvas元素上。 请注意,上述示例仅演示了基本的PDF文件预览功能,你可以根据自己的需求进行扩展和定制。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值