首页 > 其他分享 >vue+pdfh5实现将pdf渲染到页面上

vue+pdfh5实现将pdf渲染到页面上

时间:2023-11-17 12:22:34浏览次数:40  
标签:pdfh5 vue return filePath pdfurl blob pdf true

版本:[email protected] vue2+.net Core 6.0webapi

方法一:通过访问后端获取二进制数据来渲染

前端渲染

<template>
  <vol-box ref="box" :width="width" :height="height">
    <div id="demo" ref="render"></div>
  </vol-box>
</template>
​
<script>
import Pdfh5 from "pdfh5"; 
import "pdfh5/css/pdfh5.css";
let _this
  export default {
    name:'',
    data () {
      return {
        width: document.documentElement.clientWidth,    // 当前窗口宽度
        height: document.documentElement.clientHeight,  // 当前窗口高度
        pdfh5: null,
      };
    },
    mounted() {
        _this = this
        this.open(pdfurl)
    },
    methods: {
        open(pdfurl) {
            //清除上一次渲染
            this.$nextTick(() => {
                let render = this.$refs.render
                render.innerHTML = ''
            })
            this.loadpdf(pdfurl)
        },
        async loadpdf(pdfurl){
            //获取二进制文件流
            let param = {
                filePath: pdfurl.replace(this.http.ipAddress, ''),
            }
            let blob = await this.http.get('api/RDQ_Report/GetPdf',param,true)
            if (!blob || (blob && blob.status === false)) return
            //实例化
            this.pdfh5 = new Pdfh5("#demo", {
                data:atob(blob),
                //cMapUrl:"https://unpkg.com/[email protected]/cmaps/",
                responseType: "arraybuffer", // blob arraybuffer
                // withCredentials: true,
                // renderType: "svg",
                // maxZoom: 3, //手势缩放最大倍数 默认3
                // scrollEnable: true, //是否允许pdf滚动
                // zoomEnable: true, //是否允许pdf手势缩放
            });
            //监听完成事件
            this.pdfh5.on("complete", function (status, msg, time) {
                console.log("状态:" + status + ",信息:" + msg + ",耗时:" + time + "毫秒,总页数:" + this.totalNum)
                _this.$refs.render.style.height = `${_this.height}px`
            })
        }
    },
  }
</script>
<style lang='less' scoped>
#demo{
    width: 100%;
    height: 508px;
}</style>

 

后端接口

[HttpGet, Route("[action]"), AllowAnonymous]
public IActionResult GetPdf(string filePath)
{
    var rc = WebResponseContent.Instance;
    if (string.IsNullOrWhiteSpace(filePath)) return JsonNormal("error");
    var fullPath = filePath.MapPath(true);
    if (!File.Exists(fullPath)) return JsonNormal("文件不存在!");
    return Json(File.ReadAllBytes(fullPath));
}
方法二:通过访问后端资源地址 暂时没搞明白(说我跨域无法访问,但是明明设置跨域了)   详细api参数参考:https://blog.csdn.net/fade999/article/details/98869904

标签:pdfh5,vue,return,filePath,pdfurl,blob,pdf,true
From: https://www.cnblogs.com/xxeeblog/p/17838404.html

相关文章

  • vue3 使用 store
    在script中使用storehttps://blog.csdn.net/SubStar/article/details/116077737<script>import{getCurrentInstance}from"vue";import{useStore}from"vuex";exportdefault{setup(){//第一种方法:获取路由对象router的方法1constv......
  • vue本地开发的时候自定义域名端口
    找到如下文件以记事本方式打开并插入以下代码127.0.0.1localhost127.0.0.1 test.hhProject.com #将此域名指向127.0.0.1本地环境再在项目的配置文件中配置域名和端口即可exportdefaultdefineConfig({plugins:[vue()],server:{host:'test.hhProject.com',......
  • vue pinia sessionstorage 数据存储不上的原因
    vuepiniasessionstorage的坑默认的配置是开始localStorage如果用sessionstorage则发现数据存储不上,是因为缺少了序列化和反序列化import{parse,stringify}from'zipson'exportconstuseCounterStore=defineStore('counter',()=>{constcount=ref(0)......
  • Vue3 Pinia对state的订阅监听($subscribe,$onAction)数据监听
    <template><divclass="main-container":class="{'show-scroll':targetIsVisible}"><div:style="{height:frameHeight+'px'}"class="main-content":class="{'show-......
  • 【开源】基于Vue.js的计算机机房作业管理系统的设计和实现
    一、摘要1.1项目介绍基于Vue+SpringBoot+MySQL的计算机机房作业管理系统包含课程档案模块、课时档案模块、学生作业模块,还包含系统自带的用户管理、部门管理、角色管理、菜单管理、日志管理、数据字典管理、文件管理、图表展示等基础模块,计算机机房作业管理系统基于角色的访问控制......
  • 【开源】基于Vue.js的车险自助理赔系统的设计和实现
    一、摘要1.1项目介绍基于Vue+SpringBoot+MySQL的车险自助理赔系统包含车辆管理模块、车险理赔模块、理赔审核模块,还包含系统自带的用户管理、部门管理、角色管理、菜单管理、日志管理、数据字典管理、文件管理、图表展示等基础模块,车险自助理赔系统基于角色的访问控制,给车险管理......
  • vue2 项目打包后自动压缩成zip文件
    安装依赖1pnpminstallfilemanager-webpack-plugin--save-dev在vue.config.js中添加如下代码constFileManagerPlugin=require('filemanager-webpack-plugin')//引入插件1configureWebpack:{2plugins:[3newFileManagerPlugin({......
  • Cannot read properties of undefined (reading 'indexOf') at VueComponent.resetFie
    Cannotreadpropertiesofundefined(reading'indexOf')atVueComponent.resetField(index.js:1:370572) elementUI源码报错,原因竟然是form-item没加prop,折腾了一两个小时,真是服了。rules按照表单的prop来的用了那个resetfieds必须每个<el-form-item>都加prop!! ......
  • 使用js添加按钮,vue页面 el-calendar 添加自定义按钮
    html代码:<divclass="schedule"><divclass="title">今日日程</div><divclass="allSchedule"><el-rowclass="addSchedule"type="flex"align="......
  • vue2 前后端分离的项目,用宝塔部署,出现域名访问根目录能访问,加任何网址就404
    https://admin.xxx.com/ 访问正常https://admin.xxx.com/index  访问404location/{root/home/ruoyi/projects/ruoyi-ui;try_files$uri$uri//index.html;indexindex.htmlindex.htm;} nginx加上上面这段......