惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

diss带码

datart系列04:基于threejs自定义插件3D-MAP datart系列03:图表插件开发 datart系列02:图表插件开发作品 datart系列01:图表插件开发作品大赛 我的2021年新Mac设置 小程序:Painter画布 小程序:mqtt+webview控制显示内容 小程序:数十年Lite 验证码:五福临门 开源论坛:社区选择Discourse 牛年开篇:网站拜年 github自定义主页秀 Davinci-二次开发系列08:mongoDB(JDBC查询)四种解决方案 Davinci-二次开发系列07:BI新元素尝试 Davinci-二次开发系列06:导出excel合并单元格 Metabase-BI系列12:0.35版本表达式是真的香 Davinci-二次开发系列05:echarts-gl map3D扩展 Davinci-二次开发系列04:自定义水印扩展 Davinci-二次开发系列03:区域地图下钻与选择
小程序:webview + PDF预览
2021-07-20 · via diss带码

发表于 | 分类于 | |

一般文件预览除了图片基本主要指office的文件预览,不同文件(word、Excel、pdf)格式差异大,所以很难有共性。相对来说PDF的预览会相对简单一些,而且大都能转换成pdf,我们就已pdf为例。小程序官方未提供,目前能找到和想到的方式,如下:

第一:采用wx.openDocument

1
2
3
4
5
6
7
8
9
10
11
12
wx.downloadFile({
url: 'www.file.com/file.ppt',//可以是后台传过来的路径
success: function(res) {
const filePath = res.tempFilePath
wx.openDocument({
filePath: filePath,
success: function(res) {
//成功
}
})
}
})

效果:

说明:这种实际上是先下载了临时路径,好处是pdf、word、Excel都能预览,坏处是这种方式不一定能接受。

001

第二:webview+第三方pdf库

pdfjs是个很好的pdf预览的js库,可以不用改造直接使用,简单部署个服务,或者通过nginx配置一下即可,这里我们直接通过cdn阿里oss文件服务器来运行

pdfjs获取地址:https://github.com/mozilla/pdf.js

oss服务demo:

1
https://byfile.disscode.cn/pdfjs-2.8/web/viewer.html?file=https://byfile.disscode.cn/blog/2021/wx/pdf/01.pdf

小程序配置就很简单了,就是通过webview的方式调用pdfjs服务,只需传递pdf文件地址即可

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

002

备注:PDF.js访问远程服务器(非同域名下)报file origin does not match viewer’s

需要将pdfjs下面的viewer.js中注释掉代码

1
2
3
// if (origin !== viewerOrigin && protocol !== 'blob:') {
// throw new Error('file origin does not match viewer\'s');
// }

第三:延伸markdown文件渲染

markdown格式的内容渲染已有组件可以支持,尝试markdown的文件直接进行渲染,这里直接找到第三方库marked.js进行尝试

github地址:https://github.com/markedjs/marked

同样制作oss服务的demo

1
https://byfile.disscode.cn/marked/marked.html?file=https://byfile.disscode.cn/blog/2021/wx/mk/%E3%80%90BLOG%E3%80%91hexo%E6%90%AD%E5%BB%BAblog%E6%95%99%E7%A8%8B.md

小程序同样是webview的方式:

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

mk01

但是,markdown采用这种方式直接渲染文件,发现图片、换行等样式渲染还是有问题的,所以期待更多尝试。

备注:测试去掉域名的校验,要么就添加到小程序业务域名里面,否则不能正常访问

第四:代码

diss

已提交github,扫码关注公众号(diss带码),回复:webview,获得源码github地址