【微信小程序】实现打开并下载服务器上面的pdf文件到手机 ...

打印 上一主题 下一主题

主题 577|帖子 577|积分 1731

使用服务器

1. 获取pdf文件的url

要下载pdf文件,必须先获取到pdf文件的url地址。
假如你的pdf文件是在自己的服务器上面,那么可以直接使用该pdf文件的url,假如是在其它服务器上面,则必要接洽对应服务器的管理员获取该文件的url。获取到pdf文件的url后,你可以将其嵌入到一个 web-view 组件中,如下:
  1. <web-view src="pdf-url"></web-view>
复制代码
这样就可以在微信小程序中表现该pdf文件了。
2. 下载pdf文件

假如用户必要将该pdf文件下载到本地,可以使用 downloadFile 方法实现。
详细步调如下:

  • 首先,在相应的页面中添加一个下载按钮,用户点击该按钮即可触发下载操纵。
  1. <button bindtap="downloadPDF">下载PDF</button>
复制代码

  • 在js中界说downloadPDF函数,并在该函数中通过downloadFile方法将pdf文件下载到本地。
  1. downloadPDF: function() {
  2.   wx.downloadFile({
  3.     url: 'pdf-url',
  4.     success: function (res) {
  5.       // 下载成功后,调用saveImageToPhotosAlbum函数保存图片到本地
  6.       wx.saveFile({
  7.         tempFilePath: res.tempFilePath,
  8.         success: function (res) {
  9.           wx.showToast({
  10.             title: '下载成功',
  11.             icon: 'success'
  12.           })
  13.         },
  14.         fail: function (res) {
  15.           wx.showToast({
  16.             title: '下载失败',
  17.             icon: 'none'
  18.           })
  19.         }
  20.       })
  21.     },
  22.     fail: function (res) {
  23.       wx.showToast({
  24.         title: '下载失败',
  25.         icon: 'none'
  26.       })
  27.     }
  28.   })
  29. }
复制代码
这样用户点击下载按钮后,就可以将pdf文件下载到本地了。
3. 示例阐明

示例1

假设你的pdf文件在自己的服务器上,pdf文件的url为 http://example.com/pdf/file.pdf,那么你可以在相应页面的 web-view 组件中嵌入该pdf文件,如下:
  1. <web-view src="http://example.com/pdf/file.pdf"></web-view>
复制代码
用户可以直接通过该web-view组件在线查看pdf文件,也可以点击相应的下载按钮将文件下载到本地。
示例2

假设你的pdf文件不在自己的服务器上,而是在其它服务器上,且文件url为 http://other.com/file.pdf,那么在相应页面的js中添加如下代码:
  1. downloadPDF: function() {
  2.   wx.downloadFile({
  3.     url: 'http://other.com/file.pdf',
  4.     success: function (res) {
  5.       // 下载成功后,调用saveImageToPhotosAlbum函数保存图片到本地
  6.       wx.saveFile({
  7.         tempFilePath: res.tempFilePath,
  8.         success: function (res) {
  9.           wx.showToast({
  10.             title: '下载成功',
  11.             icon: 'success'
  12.           })
  13.         },
  14.         fail: function (res) {
  15.           wx.showToast({
  16.             title: '下载失败',
  17.             icon: 'none'
  18.           })
  19.         }
  20.       })
  21.     },
  22.     fail: function (res) {
  23.       wx.showToast({
  24.         title: '下载失败',
  25.         icon: 'none'
  26.       })
  27.     }
  28.   })
  29. }
复制代码
这样就可以将该pdf文件下载到本地了。
云开发

   项目场景:
最近有小伙伴问了一个问题,那就是微信小程序云开发怎样将页面天生为pdf?但是在没有后端工程师的帮助下,我们要怎么办了?
  解决方案

wxml→canvas→img→pdf,
wxml转化转化成canvas之后在天生img,微信官方已经给出了组件wxml-to-canvas可以解决,那么img转化成pdf要怎么办呢?
可以用pdf-lib,幸亏看到了黄宝~这位大佬的文章,省去了许多时间去理解文档。
效果图

可以从GIF中看到,我已经把要用到的功能都写完善了,点击天生pdf后会天生一张图片存到云服务器,之后会调用云函数把图片转换成pdf文件并上传到云服务器(转换后其实可以把多余的图片删了,但是为了直观一点我就不处理了),这样我们就可以拿云服务器上https链接来用了。
由于小程序还不支持把pdf文件保存到手机上,所以只能自己复制链接去欣赏器下载了。

在实机上,微信会提供预览功能但是不可以直接下载,自己复制下载链接到欣赏器下载就好。

Demo部门

代码我就不粘出来了,有点多,必要的话有积分可以下载CSDN的,没有的话下载百度云的就好。
CSDN -> 传送门
百度网盘=》提取码:agvk =》传送门
下载代码 Demo后,导入项目时用自己开通过云开发的AppID就好
接着把app.js里的云环境id换成你自己的

接着打开img-to-pdf云函数里的index.js文件,把云环境id换成你自己的

末了就是上传云函数了,务必记得上传,上传完就可以用了。

另外img-to-pdf云函数里的node包里安装了pdf-lib,为了方便本地测试,我就不删除了。代码的话主要部门都写相识释,问题不大,应该都能看懂。
动态设置参数

由于这的wxml是一串字符串,所以不能向样式那样通过点的形式去更新,所以假如必要动态设置参数的话,必要把字符串标签提取出来写在renderToCanvas方法里,假如数据是数组对象范例,那么用for循环拼接一下字符就好了,字符串拼接可以参考这篇文章=》传送门
  1. const {style} = require('./wxml.js');
  2. Page({
  3.   data: {
  4.     src: ''
  5.   },
  6.   onLoad() {
  7.     // 为了美观,把画布的宽度设置成屏幕的100%
  8.     style.container.width = wx.getSystemInfoSync().windowWidth;
  9.     // 获取wxml-to-canvas组件的节点
  10.     this.widget = this.selectComponent('.widget')
  11.     // 模拟调用接口
  12.     setTimeout(() => {
  13.       // 假设从后端获取到了数据
  14.       let name = "weianl"
  15.       this.renderToCanvas(name)
  16.     })
  17.   },
  18.   // 把页面数据渲染到canvas
  19.   renderToCanvas(name) {
  20.     const wxml = `<view class="container">
  21.                     <view class="item-box red">
  22.                     <text class="text">${name}</text>
  23.                     </view>
  24.                     <view class="arrows">
  25.                       <text class="text">↓</text>
  26.                     </view>
  27.                     <view class="item-box green">
  28.                       <text class="text">cavans ☛ img</text>
  29.                     </view>
  30.                     <view class="arrows">
  31.                       <text class="text">↓</text>
  32.                     </view>
  33.                     <view class="item-box blue">
  34.                       <text class="text">img ☛ pdf</text>
  35.                     </view>
  36.                   </view>`
  37.     this.widget.renderToCanvas({
  38.       wxml,
  39.       style
  40.     }).then((res) => {
  41.       // console.log('container', res.layoutBox)
  42.       this.container = res
  43.     }).catch(err => {
  44.       // console.log(err)
  45.       // 防止canvas组件没有初始化完成
  46.       setTimeout(() => {
  47.         this.renderToCanvas(name)
  48.       }, 100);
  49.     })
  50.   },
  51.   // 生成pdf
  52.   createPDF() {
  53.     wx.showLoading({
  54.       title: '生成中',
  55.     })
  56.     this.widget.canvasToTempFilePath().then(res => {
  57.       console.log(res)
  58.       let tempFilePath = res.tempFilePath;
  59.       // 定义存放到云服务器的文件夹及图片名字
  60.       const cloudPath = 'wxmlToPdf-img/' + parseInt(new Date().getTime() / 1000) + '.png';
  61.       // 把图片上传到云服务器
  62.       wx.cloud.uploadFile({
  63.         cloudPath: cloudPath,
  64.         filePath: tempFilePath, // 文件路径
  65.       }).then(res => {
  66.         console.log(res)
  67.         // 调用图片转化PDF云函数
  68.         wx.cloud.callFunction({
  69.           name: 'img-to-pdf',
  70.           data: {
  71.             width: this.container.layoutBox.width,
  72.             height: this.container.layoutBox.height,
  73.             fileID_img: res.fileID
  74.           }
  75.         }).then(res => {
  76.           console.log(res)
  77.           wx.hideLoading()
  78.           this.setData({
  79.             fileID_pdf: res.result.data.fileID,
  80.             https_pdf: res.result.data.pdf
  81.           })
  82.           wx.showToast({
  83.             title: '生成成功!',
  84.             icon: 'none'
  85.           })
  86.         })
  87.       }).catch(error => {
  88.         // handle error
  89.       })
  90.     })
  91.   },
  92.   // 预览PDF
  93.   previewPDF() {
  94.     // pdf文件在云服务器的id
  95.     let fileID_pdf = this.data.fileID_pdf;
  96.     // console.log(fileID_pdf)
  97.     if (!fileID_pdf) {
  98.       wx.showToast({
  99.         title: '请先生成PDF',
  100.         icon: 'none'
  101.       })
  102.       return false;
  103.     }
  104.     wx.showLoading({
  105.       title: '加载中',
  106.     })
  107.     wx.cloud.downloadFile({
  108.       fileID: fileID_pdf
  109.     }).then(res => {
  110.       // console.log(res)
  111.       wx.openDocument({
  112.         filePath: res.tempFilePath,
  113.         success: function (res) {
  114.           wx.hideLoading();
  115.         }
  116.       })
  117.     }).catch(error => {
  118.       // handle error
  119.     })
  120.   },
  121.   // 复制PDF链接
  122.   copyPDF() {
  123.     // PDF文件在云服务器上的https链接
  124.     let https_pdf = this.data.https_pdf;
  125.     // console.log(https_pdf)
  126.     if (!https_pdf) {
  127.       wx.showToast({
  128.         title: '请先生成PDF',
  129.         icon: 'none'
  130.       })
  131.       return false;
  132.     }
  133.     wx.setClipboardData({
  134.       data: https_pdf,
  135.       success(res) {
  136.         wx.showToast({
  137.           title: '复制成功,快到浏览器打开/下载!',
  138.           icon: 'none',
  139.           duration: 3000
  140.         })
  141.       }
  142.     })
  143.   }
  144. })
复制代码
末了想补习云函数的可以点击这里哦!! 传送门

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

勿忘初心做自己

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表