1.安装pdfh5
npm install pdfh5
2.引入pdfh5
import Pdfh5 from 'pdfh5';
import 'pdfh5/css/pdfh5.css';
3.页面
<div ref="refPdf" class="refPdf"></div>
4. js
import { defineComponent, onMounted, ref } from 'vue';
setup() {
const route = useRoute();
const refPdf = ref(null);
//页面加载
onMounted(() => {
let href = route.query.url;
const pdfh5 = new Pdfh5(refPdf.value, {
pdfurl: href
});
pdfh5.on('complete', (status, msg, time) => {
console.log(
'状态:' + status + ',信息:' + msg + ',耗时:' + time + '毫秒'
);
});
});
return {
refPdf
};
}
5.ios
//pdf预览
const previewPdf = (val) =>{
let u = navigator.userAgent;
let isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
if (isiOS) {
//a标签打开
var a = document.createElement("a");
let href = val;
a.setAttribute("href", href);
a.setAttribute("target", "_blank");
a.setAttribute("id", "startTelMedicine");
a.onclick = function () {
//关闭窗口的方法
window.opener = null;
window.open("", "_self", "");
window.close();
};
// 防止反复添加
if (document.getElementById("startTelMedicine")) {
document.body.removeChild(document.getElementById("startTelMedicine"));
}
document.body.appendChild(a);
a.click();
} else {
router.push({
path: '/pdf',
query: {
url: store.IMAGEURL + href
}
});
}
}
总结:ios手机自带pdf预览功能,a标签打开即可预览,安卓手机需要本文使用pdfh5进行预览(打开了新页面)