vue app手机端实现pdf、图片在ios或者安卓端预览下载功能

138 篇文章 7 订阅
65 篇文章 4 订阅

需求:手机端App实现pdf、图片在线预览下载功能,当前调用ios或者安卓提供的方法实现。

效果图:

 

代码实现: 

<div class="flex align-items just-cont m20">
        <div class="flex align-items">
          <img :src="invoiceUrlType!='pdf'?$baseImgUrl+dedutionObj.invoiceUrl:pdfUrl" class="leviedImg">
        </div>
        <div class="previewTxt" @click="lookShipImg">预览</div>
</div>

 data(){
    return {
        invoiceUrlType:"",
        pdfUrl:"",
    }
}

根据接口获取pdf或者图片地址显示 :

getDetail(invoiceId){
      verificationApplicationInfo({invoiceId}).then(res=>{
        if(res.code == 200){
          ......
          this.invoiceUrl = res.data.invoiceUrl;
          this.invoiceUrlType = res.data.invoiceUrl.split('.')[1];
          ......
        }else{
          this.$toast(res.msg);
        }
      })
}

 重点:根据ios或者安卓分别去调用相应的方法体来跳转第三方实现预览下载。方法体根据自己的需求调用实现,过程是一样的。本项目是调用的ios或者安卓提供的方法来实现。

    // 预览合同发票
    lookShipImg() {
      if(this.invoiceUrl.split(".")[1]=="pdf"){
        // pdf文件类型
         if (this.$utils.isEmpty(this.invoiceUrl)) {
          this.downloadPDF("合同发票",this.$route.query.invoiceId);
         }else{
            if (this.$utils.isAndroid_ios() == "andriod") {
                window.AndroidFunction.getPdfUrl(
                  "合同发票",
                  this.$baseImgUrl + this.invoiceUrl
                );
            }
            
            if (this.$utils.isAndroid_ios() == "ios") {
                window.webkit.messageHandlers.callNative.postMessage({
                  name: "下载PDF",
                  data: {
                    name: "合同发票",
                    url: this.$baseImgUrl + this.invoiceUrl,
                  },
                });
            }
         }
        
      }else{
        //图片类型
        ImagePreview({
          images: this.invoiceUrl.split(",").map(item=>this.$baseImgUrl+item),
          showIndex: false,
          loop: false, 
          startPosition: 0
        });
      }
    },

.leviedImg{
  width: 74px;
  height: 80px;
}
.previewTxt{
  color:#0050E9;
  font-size: 28px;
}

发文不易,点赞、评论、收藏、关注支持一下呗!

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要在vue3项目中实现pdf在线预览下载功能,可以使用pdf.js库。以下是基本的实现步骤: 1. 安装pdf.js库:可以使用npm安装,命令为npm install pdfjs-dist。 2. 引入pdf.js库:在需要使用的组件中引入pdf.js库,可以使用import语法。 3. 创建canvas元素:使用canvas元素来渲染pdf文件,需要在组件中创建一个canvas元素。 4. 加载pdf文件:使用pdf.js库提供的方法加载pdf文件,可以使用fetch或者axios等方式获取pdf文件。 5. 渲染pdf文件:使用pdf.js库提供的方法将pdf文件渲染到canvas元素中。 6. 实现下载功能:使用a标签的download属性或者使用FileSaver.js等库实现pdf文件下载功能。 以下是一个简单的示例代码: ``` <template> <div> <canvas ref="pdfCanvas"></canvas> <button @click="downloadPdf">下载</button> </div> </template> <script> import pdfjs from 'pdfjs-dist' import { saveAs } from 'file-saver' export default { data() { return { pdfUrl: 'http://example.com/example.pdf', pdfDoc: null, pdfPage: null } }, mounted() { this.loadPdf() }, methods: { async loadPdf() { try { const pdfData = await fetch(this.pdfUrl).then(res => res.arrayBuffer()) this.pdfDoc = await pdfjs.getDocument({ data: pdfData }).promise this.pdfPage = await this.pdfDoc.getPage(1) this.renderPdf() } catch (error) { console.error(error) } }, async renderPdf() { try { const canvas = this.$refs.pdfCanvas const context = canvas.getContext('2d') const viewport = this.pdfPage.getViewport({ scale: 1 }) canvas.width = viewport.width canvas.height = viewport.height await this.pdfPage.render({ canvasContext: context, viewport }).promise } catch (error) { console.error(error) } }, downloadPdf() { const blob = new Blob([this.pdfDoc], { type: 'application/pdf' }) saveAs(blob, 'example.pdf') } } } </script> ``` 以上代码实现pdf文件的加载和渲染,以及pdf文件下载功能。需要注意的是,pdf.js库的使用可能会比较复杂,可以参考pdf.js官方文档进行详细了解。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

船长在船上

您的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值