vue pdf预览禁止打印

这篇博客介绍了如何在Vue.js应用中利用pdfobject库来加载并展示PDF文件。首先通过npm安装pdfobject,然后在组件中导入并使用它。在创建钩子中,根据查询参数从API获取PDF文件路径,并使用pdfobject的embed方法将其嵌入到页面指定的div中。同时,博客还包含了阻止文本选择和鼠标右键功能的代码,以优化用户体验。
摘要由CSDN通过智能技术生成

1、肯定是先安装了:

npm i pdfobject --save

2、引入

import pdf from 'pdfobject'

<template>
  <div>
    <p class="over" />

    <div id="pdf-content">
      <el-container class="container" @contextmenu.native="handlePaste($event)" />
    </div>
  </div>
</template>
<script>
import pdf from 'pdfobject'
import {
  GetStudentExaminationBuild

} from '@/api/student_examination'
export default {
  name: 'ViewPDF',

  props: {
    pdfUrl: {
      type: String,

      default: ''

    }

  },

  data() {
    return {
      id: '',
      PdfWebPath: '',
      pdfTop: 40,

      showTips: true

    }
  },

  created() {
    // 禁止选择网页中的文字
    document.onselectstart = function() {
      return false
    }
    // 禁止鼠标右键功能
	            document.oncontextmenu = function() {
	                return false
	            }

    var query = this.$route.query
    this.id = query.id
    if (query.type == 'stuent_examination') {
      GetStudentExaminationBuild({
        id: this.id
      }).then(response => {
        // alert(response.WebPath)
        var options = {

          height: '10000px',

          pdfOpenParams: {
            scrollbars: '0',
            toolbar: '0',
            statusbar: '0'
          } // 禁用工具栏代码

        }

        pdf.embed(response.WebPath,
          '#pdf-content', options)
      })
    }
  },
  mounted() {

    // this.PdfWebPath = query.bean.studentExaminationBuild.webPath
  },
  methods: {
    handlePaste(event) {
      // 禁用鼠标右键
      event.preventDefault()
      return false
    }
  }

  // beforeMount() {
  // 	var that = this;

  // 	that.$nextTick(function() {
  // 		pdf.embed(that.pdfUrl, "#pdf-content");

  // 	})

  // },

}
</script>

<style>
	#pdf{
				width:100%;
				height: 100%;
				margin: 0;
				padding: 0;
			}
			.over{
			    width:100%;
			    height: 100%;
			    background-color: rgba(1,1,1,0);
			    position: fixed;
			    top:0px;
			    left:0px;

			}

</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值