Vue+ElementUI使用vue

编程入门 行业动态 更新时间:2024-10-10 12:23:59
这篇文章主要为大家详细介绍了Vue+ElementUI使用vue-pdf实现预览功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

Vue + ElementUI项目中使用vue-pdf实现简单预览,供大家参考,具体内容如下

1、安装 vue-pdf

npm install --save vue-pdf

2、在vue页面中导入对应的组件

我这是通过点击 预览 按钮 获取id打开一个dialog来实现

<!--PDF 预览--> <el-dialog :title="PDF 预览" :visible.sync="viewVisible" width="80%" center @close='closeDialog'> <div style="margin-bottom: 15px; text-align: right"> <el-button type="primary" size="small" @click.stop="previousPage"> 上一页 </el-button> <el-button type="primary" size="small" @click.stop="nextPage"> 下一页 </el-button> <span>当前第{{pdfPage}}页 / 共{{pageCount}}页</span> </div> <div > <pdf :src="src" :page="pdfPage" @num-pages="pageCount = $event" @page-loaded="pdfPage = $event" style="display: inline-block; width: 100%" ></pdf> </div></el-dialog><script> import pdf from 'vue-pdf'; export default { components: { pdf }, data() { return { //PDF预览 viewVisible: false, src: null, pdfPage : 1, pageCount: 0, } }, methods:{ //PDF预览 previewPDF(row){ this.src = pdf.createLoadingTask(documentConstants.previewPDFUrl+row.contractId); this.src.then(pdf => { this.viewVisible = true; }); }, //关闭窗口初始化PDF页码 closeDialog(){ this.pdfPage = 1; }, //PDF改变页数 previousPage(){ var p = this.pdfPage p = p>1?p-1:this.pageCount this.pdfPage = p }, nextPage(){ var p = this.pdfPage p = p<this.pageCount?p+1:1 this.pdfPage = p } } }</script>

3、Controller的返回

@RequestMapping(value = "/previewPDF/{contractId}") public ResponseEntity<byte[]> previewPDF(@PathVariable Long contractId) throws TException, IOException { ContractAttachmentTmpModelDTO model = contractScanManagementRpcService.queryContractAttachmentTmp(contractId); HttpHeaders headers = new HttpHeaders(); headers.setContentDispositionFormData("attachment", model.getAttachmentName()); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); return new ResponseEntity<byte[]>(model.getAttachmentData(), headers, HttpStatus.OK); }

总结:这个是实现一个简单的预览功能。

以上就是本文的全部内容,希望对大家的学习有所帮助,

  • 0
  • 0
  • 0
  • 0
  • 0

更多推荐

Vue+ElementUI使用vue

本文发布于:2023-06-02 22:42:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/457403.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Vue   ElementUI   vue

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!