pdf.html
5.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>在线预览</title>
<link rel="stylesheet" href="./js/pdfh5.css">
<!--<link rel="stylesheet" href="./css/style.css">-->
<style>
#pdf {
width: 100%;
height: 100vh;
}
.btn {
display: flex;
justify-content: center;
align-items: center;
width: 160px;
height: 40px;
background-color: #007BFF;
/* 蓝色背景 */
color: white;
/* 白色文字 */
border-radius: 25px;
/* 圆角 */
font-size: 16px;
font-weight: bold;
cursor: pointer;
/* 鼠标指针样式 */
transition: background-color 0.3s ease;
/* 平滑的背景色过渡 */
}
.btn:hover {
background-color: #0056b3;
/* 鼠标悬停时的蓝色 */
}
.footer {
width: 96%;
position: fixed;
bottom: 10px;
display: flex;
align-items: center;
justify-content: space-evenly;
}
</style>
</head>
<body>
<div id="app">
<div id="pdf" style="padding-top: 50px;"></div>
<div class="footer">
<div class="btn" style="background-color:#d0d0d0;color:#1b1c1a" @click="back">返回</div>
<!-- <div class="btn" @click="submit">预览文件</div> -->
</div>
</div>
<script src="./js/pdf.js"></script>
<script src="./js/pdf.worker.js"></script>
<script src="./js/jquery-3.6.0.min.js"></script>
<script src="./js/pdfh5.js"></script>
<!--<script src="https://res.wx.qq.com/open/js/jweixin-1.3.2.js"></script>-->
<script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.4.0.js"></script>
<script src="./js/vue@2.js"></script>
<script type="text/javascript" src="./js/uni.webview.1.5.6.js"></script>
<script>
var app = new Vue({
el: '#app',
data: {},
methods: {
// submit() {
// console.log('wx.miniProgram',wx.miniProgram)
// uni.miniProgram.navigateBack()
// wx.miniProgram.postMessage({ data: 'done' })
// },
back() {
uni.postMessage({
data: {
name:"back"
}
});
}
},
mounted() {
let searchURL = window.location.search;
console.log('searchURL', searchURL);
searchURL = searchURL.substring(1, searchURL.length);
let pdfurl = searchURL.split("&")[0].split("=")[1];
// 对URL进行解码
if (pdfurl) {
pdfurl = decodeURIComponent(pdfurl);
}
console.log('pdfurl', pdfurl);
const pdfContainer = document.getElementById('pdf');
if (!pdfurl) {
if (pdfContainer) {
pdfContainer.innerHTML = '<div style="text-align:center;padding:50px;color:#999;">PDF地址不存在</div>';
}
return;
}
// 检查是否是 blob URL
if (pdfurl.startsWith('blob:')) {
// blob URL 需要转换为可以直接访问的格式
// 方法1: 尝试直接使用 iframe(某些浏览器支持)
// 方法2: 使用 fetch 获取 blob,然后转换为 data URL 或使用 pdf.js 加载
console.log('处理 blob URL:', pdfurl);
// 尝试使用 fetch 获取 blob 数据,然后使用 pdf.js 加载
fetch(pdfurl)
.then(response => {
if (!response.ok) {
throw new Error('无法获取 PDF 文件');
}
return response.blob();
})
.then(blob => {
// 将 blob 转换为 URL,供 pdf.js 使用
const blobUrl = URL.createObjectURL(blob);
console.log('转换后的 blob URL:', blobUrl);
// 使用 pdfh5 加载 blob URL
const pdfh5 = new Pdfh5('#pdf', {
pdfurl: blobUrl,
}, {
lazy: true
});
pdfh5.on("complete", (status) => {
if (status === 'error') {
console.error('PDF加载失败');
if (pdfContainer) {
pdfContainer.innerHTML = '<div style="text-align:center;padding:50px;color:#999;">PDF加载失败,请检查文件是否存在</div>';
}
// 清理 blob URL
URL.revokeObjectURL(blobUrl);
return;
}
console.log('PDF加载成功');
});
})
.catch(error => {
console.error('处理 blob URL 失败:', error);
// 如果 fetch 失败,尝试直接使用 iframe
if (pdfContainer) {
pdfContainer.innerHTML = '<iframe src="' + pdfurl + '" style="width:100%;height:100vh;border:none;"></iframe>';
}
});
return;
}
// 检查是否是 file:// 协议(通常浏览器不允许直接访问)
if (pdfurl.startsWith('file://')) {
if (pdfContainer) {
pdfContainer.innerHTML = '<div style="text-align:center;padding:50px;color:#999;">浏览器安全限制,无法直接访问本地文件。请使用网络地址。</div>';
}
return;
}
// 网络URL,使用 pdfh5 加载
console.log('加载网络 PDF:', pdfurl);
const pdfh5 = new Pdfh5('#pdf', {
pdfurl,
}, {
lazy: true
});
pdfh5.on("complete", (status) => {
if (status === 'error') {
console.error('PDF加载失败');
if (pdfContainer) {
pdfContainer.innerHTML = '<div style="text-align:center;padding:50px;color:#999;">PDF加载失败,请检查文件是否存在或网络连接</div>';
}
return;
}
console.log('PDF加载成功');
});
},
unmounted() {
this.timer && clearInterval(this.timer)
},
})
</script>
</body>
</html>