Vue-pdf实现在线预览PDF文件

摘要:
前言在大多数项目中,都会遇到在线预览PDF文件。该项目使用元素ui,该元素使用vue-pdf实现。安装取决于npminstall--savevue-pdf。参数介绍:url:pdf文件的路径,可以是本地或在线。页数:总页数错误:加载错误回调链接单击:将触发独立pdf中的链接。实现<template><div><el row><el-button@click=“OnPreview”size=“small”˃预览˂el dialogtitle=“预览合同附件”:可见。sync=“viewVisible”center@close总共有{{pageCount}}}}页,当前的{{pdfPage}}页上一页下一页importpdffrom'vue-pdf'importstorefrom'@/store/'exportdefault{components:{pdf},data(){return{viewVisible:false,src:null,pdfPage:1,pageCount:0,token:store.getters.access_token,}},方法:{onPreview(){this.src=pdf.createLoadingTask;this.src.promise;then;},closePreview(){this.pdfPage=1;},PreviousPage(){letp=this.pdfPagep=p˃1?p+1:1this.pdf Page=p}}效果说明1.URLur是文件地址路径。src=pdf createLoadingTask;2.可以通过httpHeaders设置请求头,以设置令牌httpHeaders:{Authorization:'Bearer'+this.token}3.src更重要。许多在线帖子都是这样。这src中。则将报告错误TypeError:this。src中。theinotafunctionTypeError:这个。src中。VueComponent没有任何功能。onPreview(index.vue?

前言

在大多数项目中都会遇到在线预览PDF文件,项目使用的是element ui,使用vue-pdf实现。

安装依赖

npm install --save vue-pdf

相关参数

参数介绍:

  • url :pdf 文件的路径,可以是本地路径,也可以是在线路径。
  • page: 当前显示的页数,比如第一页page=1
  • rotate : 旋转角度,比如0就是不旋转,+90,-90 就是水平旋转。
  • progress :当前页面的加载进度,范围是0-1 ,等于1的时候代表当前页已经完全加载完成了。
  • page-loaded :页面加载成功的回调函数,不咋能用到。
  • num-pages :总页数
  • error :加载错误的回调
  • link-clicked:单机pdf内的链接会触发。
  • print 这个是打印函数。 注意:谷歌浏览器会出现乱码,这个和字体有关系。

实现

<template>
    <div>
        <el-row>
            <el-button @click="onPreview" size="small">预览</el-button>
        </el-row>
        <el-dialog title="预览合同附件" :visible.sync="viewVisible" center   @close='closePreview'>
            <el-row :gutter="20">
                <span>共{{pageCount}}页, 当前第 {{pdfPage}} 页 </span>
                <el-button type="text" size="mini" @click.stop="previousPage">上一页</el-button>
                <el-button type="text" size="mini" @click.stop="nextPage">下一页</el-button>
            </el-row>
            <div>
                <pdf :src="http://t.zoukankan.com/src" :page="pdfPage" @num-pages="pageCount = $event" @page-loaded="pdfPage = $event" style="display: inline-block;  100%"></pdf>
            </div>
        </el-dialog>
    </div>
</template>
<script>
import pdf from 'vue-pdf'
import store from '@/store/'
export default {
    components:{
        pdf
    },
    data(){
        return {
            viewVisible: false,
            src: null,
            pdfPage : 1,
            pageCount: 0,
            token: store.getters.access_token,
        }
    },
    methods:{
        onPreview(){
            this.src = pdf.createLoadingTask({
                url: 'http://localhost:8082/file/demo.pdf',
                httpHeaders: {Authorization:'Bearer '+ this.token}
            });
            this.src.promise.then(pdf => {
                this.viewVisible = true;
            });
        },
        closePreview(){
            this.pdfPage = 1;
        },
        previousPage(){
            let p = this.pdfPage
            p = p > 1 ? p-1 : this.pageCount
            this.pdfPage = p
        },
        nextPage(){
            let p = this.pdfPage
            p = p < this.pageCount ? p+1 : 1
            this.pdfPage = p
        }
    }
}
</script>   

效果

效果

注意点

1、URL

url为文件地址路径

this.src = pdf.createLoadingTask({
    url: 'http://localhost:8082/file/demo.pdf',
});

2、设置请求头

可以通过httpHeaders来设置token等参数

 httpHeaders: {Authorization:'Bearer '+ this.token}

3、src

这点比较重要,网上很多帖子都是这样的

this.src.then(pdf => {
    this.viewVisible = true;
})

会报错 TypeError: this.src.then is not a function

TypeError: this.src.then is not a function
    at VueComponent.onPreview (index.vue?6ced:241)
    at click (index.vue?aaff:261)
    at invokeWithErrorHandling (vue.runtime.esm.js?2b0e:1854)
    at VueComponent.invoker (vue.runtime.esm.js?2b0e:2179)
    at invokeWithErrorHandling (vue.runtime.esm.js?2b0e:1854)
    at VueComponent.Vue.$emit (vue.runtime.esm.js?2b0e:3888)
    at VueComponent.handleClick (element-ui.common.js?5c96:9413)
    at invokeWithErrorHandling (vue.runtime.esm.js?2b0e:1854)
    at HTMLButtonElement.invoker (vue.runtime.esm.js?2b0e:2179)
    at HTMLButtonElement.original._wrapper (vue.runtime.esm.js?2b0e:6917)

正确的是这样的

this.src.promise.then(pdf => {
    this.viewVisible = true;
});

免责声明:文章转载自《Vue-pdf实现在线预览PDF文件》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇关于@Scheduled cron表达式中不支持每月最后一天`L`的解决方案玩转Android Camera开发(二):使用TextureView和SurfaceTexture预览Camera 基础拍照demo下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

vue+jspdf+html2canvas导出PDF文件

    今天开始实现日结单这个功能,日结单是允许打印的,所以我们需要将日结单以PDF的形式导出,我做的是Vue单页应用,于是查找了一番Vue如何导出PDF,看了几篇博客,实现了PDF的导出。     不废话了,先看一下最终的打印结果        我丑话说在前面,最终打印出来的pdf文件就是这个样子,通过对jspdf的设置实现了pdf文件的分页,但文件的...

Java操作PDF,在PDF文件指定位置输出水印

需要参考我的上一篇博客,定位PDF中的关键字,找出需要打印水印的坐标位置。 先说测试结果(PDF原件也是上一篇中的图片所示): 新生成的带有水印的PDF文件如下所示: junit测试代码及输出: maven配置文件 <!--引入pdf --> <dependency> <groupId>com.i...

Java中Error和Exception的异同以及运行时异常(Runtime exception)与检查型异常(checked exception)的区别

一:Error和Exception的基本概念: 首先Exception和Error都是继承于Throwable 类,在 Java 中只有 Throwable 类型的实例才可以被抛出(throw)或者捕获(catch),它是异常处理机制的基本组成类型。 Exception 和 Error 体现了 Java 平台设计者对不同异常情况的分类,Exception和...

NVelocity系列 → NVelocity配置详解

在VelocityEngine初始化前,可以通过ExtendedProperties配置NVelocity的运行环境参数,当执行VelocityEngine的Init(ExtendedProperties)后,NVelocity会合并自定义配置和默认配置。NVelocity在NVelocity.Runtime.RuntimeConstants中定义了默认配...

Remoting异常处理:在客户端获取服务器内部错误

Remoting异常处理:在客户端获取服务器内部错误 (转载于http://blog.csdn.net/oneiter/article/details/1616369) 用Remoting技术进行开发有些日子了,其中有个问题一直困扰着我,那就是有关在客户端获取服务器内部错误的问题。当服务器和客户端部署在同一台机器上时,服务器端的错误能够在客户端成功地捕获...

js实现html转pdf+html2canvas.js截图不全的问题

最近做项目中遇到要把整个页面保存为PDF文件,网上找了一下实现的方法都是 html2canvas.js+jsPdf.js 来实现。实现的过程是 先用html2canvas.js把html页面转成图片,再用jsPdf.js把图片导出为pdf。 于是做了个小案例来测试这个功能。 <body> <!-- PDF -->...