微信小程序展示长图与pdf

一、效果

二、代码部分

实现思路,就是使用常用控件view ,但是会遇到的问题是,长图大小超过屏幕大小时,就会出现图片被压缩的情况。最后,可以加上scroll-view来解决这个问题,实现代码如下:

xml部分

http://192.168.98.1:8080/web/xx.jpg说明,这个地址是清明上河图地址,本人测试的时候是存放在本地的tomcat目录下,通过启用tomcat应用来部署实现链接访问。

<!-- 图片展示测试 -->
<view wx:if="{{true}}">
<scroll-view scroll-y="{{true}}" style="width:100%;height:{{sysheight}}px;">
     <view class="img-size">
        <image src="http://192.168.98.1:8080/web/xx.jpg" mode="widthFix" class="liucheng-img"></image>
     </view>
 </scroll-view>
</view> 

js部分

  //获取设备屏幕高度
    wx.getSystemInfo({
      success: (result) => {
        this.setData({
          sysheight:result.windowHeight
        })
      },
    });

css部分

page{
  width: 100%;
  height: 100%;
  }
  
  .img-size {
    width: 100%;
    margin: auto;
  }
  
  .liucheng-img {
    width: 100%;
    height: 100%;
  }

三、展示pdf

展示方式,第一可以直接使用webview

<web-view src="{{pathData}}" </web-view>

或者使用第三方库pdf.js

<web-view src="https://byfile.disscode.cn/pdfjs-2.8/web/viewer.html?file={{url}}"></web-view>

还可以使用微信小程序自带api先下载后打开

wx.downloadFile({
      url: "http://192.168.43.101:8080/web/yu.pdf",//可以是后台传过来的路径
      success: function(res) {
          const filePath = res.tempFilePath
          console.log("下载文件成功=="+JSON.stringify(res.data))
          wx.openDocument({
              filePath: filePath,
              success: function(res) {
                  //成功
                  console.log("打开文件成功=="+JSON.stringify(res.data))
              },
              fail:function(res){
                console.log("打开文件失败=="+JSON.stringify(res.data))
              }
          })
      },
      fail:function(res){
        console.log("下载文件失败=="+JSON.stringify(res.data))
      }
  })

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值