1.下载pdfs压缩包 http://mozilla.github.io/pdf.js/getting_started/

2.引入pdfs的文件,注:必须放在assets目录下,不然无效 ,在要预览的vue页面通过iframe标签预览,见下图

<template>
  <div class="app-container">
    <van-cell class="messageDialog" v-for="(item, index) in form" :key="index">
      <span class="custom-title">
        {{ item.signingPlatform }}
      </span>
      <van-cell>
        <!-- IOS的pdf预览直接用a标签链接到后端返回的地址 -->
        <a v-if="isIos" :href="url"
          ><van-button class="custom-contact" type="primary" size="mini"
            >查看</van-button
          ></a
        >
        <!-- Android的pdf预览用iframe标签 -->
        <van-button
          v-else
          class="custom-contact"
          type="primary"
          size="mini"
          @click="detail(item)"
          >查看</van-button
        >
      </van-cell>
    </van-cell>
  </div>
</template>

<script>
  import {
    userContract,
    contractGuid,
    detailContract
  } from '@/api/task/task.js';
  // 引入原生的文件
  import Bridge from '@/utils/JSbridge';
  import store from '@/store';
  export default {
    data() {
      return {
        list: {
          pageSize: 5,
          currentPage: 1,
          enterpriseid: null,
          appid: ''
        },
        isIos: false,
        form: [],
        url: '',
        guid: '',
        version:''
      };
    },
    created() {
      this.getMessage();
    },
    methods: {
       async getMessage() {
          const res = await userContract(this.list);
          this.form = res.data.result.list;
       },
       //  查看PDF事件
       async detail(item, query) {
        const res = await contractGuid({ guid: item.guid });
        this.guid = res.data.result;
        // detailContract  查看PDF文件的接口
        const resl = await detailContract({ guid: res.data.result });
        // this.url 后端返回PDF带参数的文件地址
        this.url = resl.data.result.fullPath;
        //  判断手机是iOS或者Android
        let isAndroid =
          navigator.userAgent.indexOf('Android') > -1 ||
          navigator.userAgent.indexOf('Adr') > -1;
        if (isAndroid) {
         // 调用原生的PDF封装好的方法
          Bridge.showPdf({
            fileName: '我的合同',
            url: resl.data.result.fullPath // pdf地址
          });
        } else {
          this.isIos = true;
        }
        
      }
 
    }
  };
</script>

<style lang="scss" scoped>
  .custom-title {
    margin-left: 14px;
    vertical-align: middle;
  }

  .custom-contact {
    float: right;
  }

  .messageDialog {
    margin-top: 10px;
    border-radius: 10px;
  }

</style>

PDF文件页面 

<template>
  <div class="app-container">
    <!-- 本地 -->
    <!-- <iframe src="/src/assets/pdfs/web/viewer.html?file=/public/合同.pdf" width="100%" height="800"></iframe> -->
    <!-- 线上 -->
    <iframe :src="'/src/assets/pdfs/web/viewer.html?file='+url" width="100%" height="800"></iframe>
  </div>
</template>

<script>
import { detailContract } from '@/api/task/task.js';
export default {
  data() {
    return {
      url: ''
    };
  },
  created() {
    this.getMessage();
  },
  mounted() {

  },
  methods: {
    async getMessage() {
      const response = await detailContract({ guid: this.$route.query.guid });
      this.url = response.data.result.fullPath; 
    }
  }
};
</script>

<style lang="scss" scoped>
.custom-title {
  margin-right: 4px;
  vertical-align: middle;
}

.custom-contact {
  float: right;
}

.messageDialog {
  margin-top: 10px;
  border-radius: 10px;
}
</style>

 原生JSbridge文件

// 原生封装好的pdf方法
export default {
    // pdf查看
  pdfHandler (name, data, callback) {
    return new Promise((resolve, rejected) => {
      setupWebViewJavascriptBridge(function (bridge) {
        bridge.callHandler(name, data, response => {
          if (!response) rejected()
          if (callback) {
            callback && callback(response)
          } else {
            console.log(response, 'callHandler')
            if (typeof response === 'string' && response) {
              response = JSON.parse(response)
            }
            // 调用成功,使用保留的resolve改变返回的promise状态
            resolve(response)
          }
        })
      })
    })
  },

   // 展示pdf格式
  async showPdf(param, callback) {
    console.log(333333333)
    await this.pdfHandler('showPdf', param, callback)
  },

}

Logo

前往低代码交流专区

更多推荐