小程序打开pdf文件

wxml部分:
  <view class="content">
      <van-cell title="{{index+1}}.{{item.descripts}}" is-link wx:for="{{helpDetail}}" wx:key="index" bindtap="goToDetail" data-id="{{item.url}}" />
    </view>
 
 
js部分:
 
  goToDetail:function(e){
    if(this.data.loading){
      return
    }
    let url = e.currentTarget.dataset.id; //pdf图片地址
    let _this = this;
    if (url != "") {
      if ((this.data.platform != "") && (this.data.platform != "ios")) {
        //非IOS设备打开文档
        this.setData({
          loading: true
        }, () => {
          this.openDocument(url);
        })
      } else {
        if (this.data.platform == "") {
          //未获取到设备信息
          wx.getSystemInfo({
            success: function(res) {
              _this.setData({
                platform: res.platform
              })
              if (res.platform != 'ios') {
                _this.setData({
                  loading: true
                }, () => {
                  _this.openDocument(url);
                })
              }
            }
          })
        } else {
          //IOS 设备
          wx.navigateTo({
            url: '../questionDetail/detail?url='+url,
          })
        }
      }
    } else {
      wx.showToast({
        title: '无帮助文档',
        icon: false,
      })
    }



  },
    /**
   * 打开文档
   */
  openDocument: function(url) {
    let _this = this;
    wx.downloadFile({
      url: url,
      success: function(res) {
        console.log(res)
        var Path = res.tempFilePath //返回的文件临时地址,用于后面打开本地预览所用
        wx.openDocument({
          filePath: Path,
          success: function(res) {
            _this.setData({
              loading: false,
            })
          }
        })
      },
      fail: function(res) {
        wx.showToast({
          title: '文档加载失败',
          icon: 'none',
        })
        _this.setData({
          loading: false,
        })
      }
    })
  },
原文地址:https://www.cnblogs.com/zxm1993/p/12985595.html