|
@@ -1,8 +1,18 @@
|
|
<template>
|
|
<template>
|
|
<div class="pdf-view-example j-contanter">
|
|
<div class="pdf-view-example j-contanter">
|
|
- <div id="pdf-view-container" class="j-main" />
|
|
|
|
|
|
+ <div class="j-main">
|
|
|
|
+ <PDF
|
|
|
|
+ v-if="pdfUrl"
|
|
|
|
+ :useLoading="false"
|
|
|
|
+ :usePagination="false"
|
|
|
|
+ :url="pdfUrl"
|
|
|
|
+ ></PDF>
|
|
|
|
+ </div>
|
|
<div v-if="!getUserId" class="j-footer">
|
|
<div v-if="!getUserId" class="j-footer">
|
|
- <a href="/jyapp/free/login?url=/jy_mobile/order/create/creditreport&activity=bidCreditReportPreview" class="adsense-container">
|
|
|
|
|
|
+ <a
|
|
|
|
+ href="/jyapp/free/login?url=/jy_mobile/order/create/creditreport&activity=bidCreditReportPreview"
|
|
|
|
+ class="adsense-container"
|
|
|
|
+ >
|
|
<AdSingle
|
|
<AdSingle
|
|
ad="app-credit-report-sample"
|
|
ad="app-credit-report-sample"
|
|
:show-tag="false"
|
|
:show-tag="false"
|
|
@@ -18,11 +28,13 @@
|
|
import { mapGetters } from 'vuex'
|
|
import { mapGetters } from 'vuex'
|
|
import AdSingle from '@/components/ad/Ad'
|
|
import AdSingle from '@/components/ad/Ad'
|
|
import { getAssetsFile } from '@/utils'
|
|
import { getAssetsFile } from '@/utils'
|
|
|
|
+import PDF from '@/views/article/components/PDF.vue'
|
|
|
|
|
|
export default {
|
|
export default {
|
|
name: 'PdfViewExample',
|
|
name: 'PdfViewExample',
|
|
components: {
|
|
components: {
|
|
- AdSingle
|
|
|
|
|
|
+ AdSingle,
|
|
|
|
+ PDF
|
|
},
|
|
},
|
|
data() {
|
|
data() {
|
|
return {
|
|
return {
|
|
@@ -51,22 +63,21 @@ export default {
|
|
const { pdfUrl } = this.$route.query
|
|
const { pdfUrl } = this.$route.query
|
|
if (pdfUrl) {
|
|
if (pdfUrl) {
|
|
this.pdfUrl = decodeURIComponent(pdfUrl)
|
|
this.pdfUrl = decodeURIComponent(pdfUrl)
|
|
- }
|
|
|
|
- else {
|
|
|
|
|
|
+ } else {
|
|
const pdfAssets = getAssetsFile('example-min.pdf')
|
|
const pdfAssets = getAssetsFile('example-min.pdf')
|
|
// this.pdfUrl = 'https://cdn-common.jianyu360.cn/cdn/assets/file/example.pdf'
|
|
// this.pdfUrl = 'https://cdn-common.jianyu360.cn/cdn/assets/file/example.pdf'
|
|
this.pdfUrl = pdfAssets
|
|
this.pdfUrl = pdfAssets
|
|
}
|
|
}
|
|
},
|
|
},
|
|
mounted() {
|
|
mounted() {
|
|
- const container = document.getElementById('pdf-view-container')
|
|
|
|
-
|
|
|
|
- this.loadPdf(this.pdfUrl, container)
|
|
|
|
- // 监听窗口的 resize 事件,实现自适应效果
|
|
|
|
- window.addEventListener('resize', () => {
|
|
|
|
- container.innerHTML = ''
|
|
|
|
- this.loadPdf(this.pdfUrl, container)
|
|
|
|
- })
|
|
|
|
|
|
+ // const container = document.getElementById('pdf-view-container')
|
|
|
|
+ //
|
|
|
|
+ // this.loadPdf(this.pdfUrl, container)
|
|
|
|
+ // // 监听窗口的 resize 事件,实现自适应效果
|
|
|
|
+ // window.addEventListener('resize', () => {
|
|
|
|
+ // container.innerHTML = ''
|
|
|
|
+ // this.loadPdf(this.pdfUrl, container)
|
|
|
|
+ // })
|
|
},
|
|
},
|
|
methods: {
|
|
methods: {
|
|
async loadPdf(pdfUrl, container) {
|
|
async loadPdf(pdfUrl, container) {
|
|
@@ -97,15 +108,17 @@ export default {
|
|
this.$toast.clear()
|
|
this.$toast.clear()
|
|
// 监听滚动事件
|
|
// 监听滚动事件
|
|
container.addEventListener('scroll', () => this.handleScroll(container))
|
|
container.addEventListener('scroll', () => this.handleScroll(container))
|
|
- }
|
|
|
|
- catch (error) {
|
|
|
|
|
|
+ } catch (error) {
|
|
console.error('Error loading PDF:', error)
|
|
console.error('Error loading PDF:', error)
|
|
}
|
|
}
|
|
},
|
|
},
|
|
async loadBatchPages(container) {
|
|
async loadBatchPages(container) {
|
|
// 加载当前页码开始的连续多页,这里以5页为例
|
|
// 加载当前页码开始的连续多页,这里以5页为例
|
|
// 分批加载,从当前页码开始,最多加载5页
|
|
// 分批加载,从当前页码开始,最多加载5页
|
|
- const endPage = Math.min(this.currentPage + this.batchSize - 1, this.totalPages)
|
|
|
|
|
|
+ const endPage = Math.min(
|
|
|
|
+ this.currentPage + this.batchSize - 1,
|
|
|
|
+ this.totalPages
|
|
|
|
+ )
|
|
for (let pageNum = this.currentPage; pageNum <= endPage; pageNum++) {
|
|
for (let pageNum = this.currentPage; pageNum <= endPage; pageNum++) {
|
|
const page = await this.pdf.getPage(pageNum)
|
|
const page = await this.pdf.getPage(pageNum)
|
|
const viewport = page.getViewport({ scale: this.calculateScale(page) })
|
|
const viewport = page.getViewport({ scale: this.calculateScale(page) })
|
|
@@ -117,14 +130,15 @@ export default {
|
|
|
|
|
|
await page.render({
|
|
await page.render({
|
|
canvasContext: ctx,
|
|
canvasContext: ctx,
|
|
- viewport,
|
|
|
|
|
|
+ viewport
|
|
}).promise
|
|
}).promise
|
|
}
|
|
}
|
|
this.currentPage = endPage + 1 // 更新当前页码
|
|
this.currentPage = endPage + 1 // 更新当前页码
|
|
},
|
|
},
|
|
handleScroll(container) {
|
|
handleScroll(container) {
|
|
const { scrollTop, scrollHeight, clientHeight } = container
|
|
const { scrollTop, scrollHeight, clientHeight } = container
|
|
- if (scrollTop + clientHeight >= scrollHeight - 100) { // 当滚动到底部附近时
|
|
|
|
|
|
+ if (scrollTop + clientHeight >= scrollHeight - 100) {
|
|
|
|
+ // 当滚动到底部附近时
|
|
if (this.currentPage <= this.totalPages) {
|
|
if (this.currentPage <= this.totalPages) {
|
|
this.loadBatchPages(container)
|
|
this.loadBatchPages(container)
|
|
}
|
|
}
|
|
@@ -132,7 +146,8 @@ export default {
|
|
},
|
|
},
|
|
// 计算缩放比例以适应容器宽度
|
|
// 计算缩放比例以适应容器宽度
|
|
calculateScale(page) {
|
|
calculateScale(page) {
|
|
- const containerWidth = document.getElementById('pdf-view-container').clientWidth
|
|
|
|
|
|
+ const containerWidth =
|
|
|
|
+ document.getElementById('pdf-view-container').clientWidth
|
|
const viewport = page.getViewport({ scale: 1 })
|
|
const viewport = page.getViewport({ scale: 1 })
|
|
// 增加一个倍数来提高清晰度,例如 1.5
|
|
// 增加一个倍数来提高清晰度,例如 1.5
|
|
return (containerWidth / viewport.width) * 3
|
|
return (containerWidth / viewport.width) * 3
|
|
@@ -140,3 +155,12 @@ export default {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
</script>
|
|
</script>
|
|
|
|
+<style lang="scss" scoped>
|
|
|
|
+.pdf-view-example {
|
|
|
|
+ ::v-deep {
|
|
|
|
+ .pdf-box {
|
|
|
|
+ height: 100%;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+</style>
|