微信小程序pdf预览

1.示例图 

微信小程序pdf预览_第1张图片

2.代码

fileId:要预览的pdf文件的id 

 viewsFiles(fileId) {
    wx.showLoading({
      title: '加载中...'
    });
    var params = {
      url: "/common/getFile/" + fileId ,//后端提供的接口
      method: "GET",
      responseType: "arraybuffer",
      callBack: (res) => {
        console.log(res)
        const fs = wx.getFileSystemManager(); //获取全局唯一的文件管理器
        fs.writeFile({
          // 写文件
          filePath: wx.env.USER_DATA_PATH + "/" + '文件名.pdf', // wx.env.USER_DATA_PATH 指定临时文件存入的路径,后面字符串自定义
          data: res,
          encoding: "binary", //二进制流文件必须是 binary
          success(res) {
            wx.openDocument({
              // 新开页面打开文档
              filePath: wx.env.USER_DATA_PATH + "/" + '文件名.pdf', //拿上面存入的文件路径
              showMenu: true, // 是否显示右上角菜单(3个点)
              success: function (res) {
                setTimeout(() => {
                  wx.hideLoading();
                }, 500);
              },
            });
          },
        });
      }
    }
    http.request(params)
  },

console.log(res) 打印的内容:

微信小程序pdf预览_第2张图片

后端接口返回的内容样式:

微信小程序pdf预览_第3张图片

你可能感兴趣的:(微信小程序,pdf,小程序)