1
jhzh
2026-01-14 fa821ce7a7755dd0e13897cefc57071d4596431b
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
<template>
  <div class="pdf-viewer-wrapper">
    <div v-if="loading" class="pdf-loading">
      <div class="loading-spinner"></div>
      <p>加载中...</p>
    </div>
    <div v-else-if="error" class="pdf-error">
      <p>{{ error }}</p>
      <button @click="loadPdf" class="retry-btn">重试</button>
    </div>
    <div v-else class="pdf-container">
      <div class="pdf-toolbar">
        <button @click="zoomOut" class="toolbar-btn" :disabled="scale <= 0.5">
          <span class="icon">−</span>
        </button>
        <span class="zoom-info">{{ Math.round(scale * 100) }}%</span>
        <button @click="zoomIn" class="toolbar-btn" :disabled="scale >= 3">
          <span class="icon">+</span>
        </button>
        <div class="toolbar-divider"></div>
        <button @click="prevPage" class="toolbar-btn" :disabled="currentPage <= 1">
          <span class="icon">‹</span>
        </button>
        <span class="page-info">{{ currentPage }} / {{ totalPages }}</span>
        <button @click="nextPage" class="toolbar-btn" :disabled="currentPage >= totalPages">
          <span class="icon">›</span>
        </button>
      </div>
      <div class="pdf-content" ref="pdfContainer">
        <canvas ref="pdfCanvas" class="pdf-canvas"></canvas>
      </div>
    </div>
  </div>
</template>
 
<script>
import PDFJS from 'pdfjs-dist'
 
// 使用本地worker
PDFJS.GlobalWorkerOptions.workerSrc = require('pdfjs-dist/build/pdf.worker.min.js')
 
export default {
  name: 'PdfViewer',
  props: {
    pdfUrl: {
      type: String,
      required: true,
      default: ''
    }
  },
  data() {
    return {
      loading: true,
      error: null,
      pdfDoc: null,
      currentPage: 1,
      totalPages: 0,
      scale: 1.0,
      renderTask: null
    }
  },
  mounted() {
    this.loadPdf()
  },
  watch: {
    pdfUrl() {
      this.loadPdf()
    }
  },
  methods: {
    async loadPdf() {
      if (!this.pdfUrl) {
        this.error = 'PDF地址为空'
        this.loading = false
        return
      }
 
      this.loading = true
      this.error = null
 
      try {
        console.log('开始加载PDF,URL:', this.pdfUrl)
 
        // 获取PDF数据,确保携带认证信息
        const token = typeof window !== 'undefined' && window.localStorage ? window.localStorage.getItem('USERTOKEN') || '' : ''
        const url = this.pdfUrl + (this.pdfUrl.indexOf('?') > -1 ? '&' : '?') + 'token=' + encodeURIComponent(token)
 
        console.log('请求URL:', url)
 
        const response = await fetch(url, {
          credentials: 'include',
          headers: {
            'USERTOKEN': token
          }
        })
 
        if (!response.ok) {
          throw new Error('加载PDF失败: ' + response.status)
        }
 
        const arrayBuffer = await response.arrayBuffer()
        console.log('PDF数据大小:', arrayBuffer.byteLength)
 
        if (arrayBuffer.byteLength === 0) {
          throw new Error('PDF文件为空')
        }
 
        // 加载PDF文档
        const loadingTask = PDFJS.getDocument({
          data: arrayBuffer,
          verbosity: 0
        })
 
        this.pdfDoc = await loadingTask.promise
        this.totalPages = this.pdfDoc.numPages
        console.log('PDF加载成功,总页数:', this.totalPages)
 
        // 计算适合H5的初始缩放比例
        await this.calculateInitialScale()
 
        await this.renderPage(this.currentPage)
        this.loading = false
      } catch (error) {
        console.error('加载PDF失败:', error)
        this.error = '加载PDF失败,请稍后重试'
        this.loading = false
      }
    },
    async calculateInitialScale() {
      if (!this.pdfDoc) return
 
      try {
        const page = await this.pdfDoc.getPage(1)
        const viewport = page.getViewport({ scale: 1.0 })
 
        // 获取容器宽度(H5适配)
        const containerWidth = this.$refs.pdfContainer ? this.$refs.pdfContainer.clientWidth : window.innerWidth - 40
        const containerHeight = window.innerHeight - 200
 
        // 计算适合的缩放比例
        const scaleX = containerWidth / viewport.width
        const scaleY = containerHeight / viewport.height
        this.scale = Math.min(scaleX, scaleY, 1.2) // 初始缩放不超过1.2倍
        console.log('计算初始缩放比例:', this.scale)
      } catch (error) {
        console.error('计算缩放比例失败:', error)
        this.scale = 1.0
      }
    },
    async renderPage(pageNum) {
      if (!this.pdfDoc || !this.$refs.pdfCanvas) return
 
      // 取消之前的渲染任务
      if (this.renderTask) {
        this.renderTask.cancel()
      }
 
      try {
        const page = await this.pdfDoc.getPage(pageNum)
        const canvas = this.$refs.pdfCanvas
        const context = canvas.getContext('2d')
 
        const viewport = page.getViewport({ scale: this.scale })
 
        // 设置canvas尺寸(H5高清适配)
        const dpr = window.devicePixelRatio || 1
        canvas.width = viewport.width * dpr
        canvas.height = viewport.height * dpr
        canvas.style.width = viewport.width + 'px'
        canvas.style.height = viewport.height + 'px'
 
        context.scale(dpr, dpr)
 
        const renderContext = {
          canvasContext: context,
          viewport: viewport
        }
 
        this.renderTask = page.render(renderContext)
        await this.renderTask.promise
        this.currentPage = pageNum
        console.log('渲染第', pageNum, '页完成,缩放比例:', this.scale)
      } catch (error) {
        if (error.name !== 'RenderingCancelledException') {
          console.error('渲染PDF页面失败:', error)
        }
      }
    },
    zoomIn() {
      this.scale = Math.min(this.scale + 0.2, 3)
      this.renderPage(this.currentPage)
    },
    zoomOut() {
      this.scale = Math.max(this.scale - 0.2, 0.5)
      this.renderPage(this.currentPage)
    },
    prevPage() {
      if (this.currentPage > 1) {
        this.renderPage(this.currentPage - 1)
      }
    },
    nextPage() {
      if (this.currentPage < this.totalPages) {
        this.renderPage(this.currentPage + 1)
      }
    }
  },
  beforeDestroy() {
    if (this.renderTask) {
      this.renderTask.cancel()
    }
  }
}
</script>
 
<style scoped>
.pdf-viewer-wrapper {
  width: 100%;
  height: 100%;
  background: #f5f5f5;
}
 
.pdf-loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 60vh;
  color: #666;
}
 
.loading-spinner {
  width: 40px;
  height: 40px;
  border: 4px solid #f3f3f3;
  border-top: 4px solid #f11514;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 15px;
}
 
@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}
 
.pdf-error {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 60vh;
  color: #f56c6c;
  padding: 20px;
  text-align: center;
}
 
.retry-btn {
  margin-top: 15px;
  padding: 10px 20px;
  background: linear-gradient(-55deg, rgb(241, 22, 20), rgb(240, 40, 37));
  color: #fff;
  border: none;
  border-radius: 5px;
  font-size: 14px;
  cursor: pointer;
}
 
.pdf-container {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  background: #525252;
}
 
.pdf-toolbar {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 10px 5px;
  background: #fff;
  border-bottom: 1px solid #e0e0e0;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
  position: sticky;
  top: 0;
  z-index: 10;
  flex-shrink: 0;
}
 
.toolbar-btn {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f5f5f5;
  border: 1px solid #e0e0e0;
  border-radius: 4px;
  margin: 0 3px;
  cursor: pointer;
  font-size: 20px;
  color: #333;
  transition: all 0.2s;
  -webkit-tap-highlight-color: transparent;
}
 
.toolbar-btn:active {
  background: #e0e0e0;
  transform: scale(0.95);
}
 
.toolbar-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}
 
.toolbar-btn .icon {
  line-height: 1;
  font-weight: bold;
  user-select: none;
}
 
.zoom-info,
.page-info {
  min-width: 55px;
  text-align: center;
  font-size: 13px;
  color: #666;
  margin: 0 6px;
  user-select: none;
}
 
.toolbar-divider {
  width: 1px;
  height: 24px;
  background: #e0e0e0;
  margin: 0 6px;
}
 
.pdf-content {
  flex: 1;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  background: #525252;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 15px 10px;
  min-height: calc(100vh - 280px);
}
 
.pdf-canvas {
  display: block;
  margin: 0 auto;
  background: #fff;
  box-shadow: 0 2px 8px rgba(0,0,0,0.3);
  max-width: 100%;
  height: auto;
  touch-action: pan-x pan-y pinch-zoom;
}
 
/* H5移动端优化 */
@media (max-width: 768px) {
  .pdf-toolbar {
    padding: 8px 3px;
  }
 
  .toolbar-btn {
    width: 32px;
    height: 32px;
    margin: 0 2px;
    font-size: 18px;
  }
 
  .zoom-info,
  .page-info {
    font-size: 12px;
    margin: 0 4px;
    min-width: 50px;
  }
 
  .pdf-content {
    padding: 10px 5px;
    min-height: calc(100vh - 160px);
  }
}
 
/* 横屏适配 */
@media (orientation: landscape) {
  .pdf-content {
    min-height: calc(100vh - 140px);
  }
}
</style>