fix(测试跟踪): PDF预览问题
--bug=1023487 --user=宋昌昌 【测试跟踪】功能用例,预览附件pdf文件,一直在转圈,console有报错 https://www.tapd.cn/55049933/s/1341230
This commit is contained in:
parent
26a0d25f1f
commit
eb0e6deadc
|
@ -1,8 +1,8 @@
|
|||
<template>
|
||||
<div @click="exe" class="recycle">
|
||||
<div style="padding: 9px 24px;" class="to-public">
|
||||
<div class="to-public">
|
||||
<svg-icon icon-class="icon_delete-trash_filled" style="width: 1.3em;height: 1.3em;"/>
|
||||
<span style="position: relative;top: -2px;left: 7px;">{{ $t('commons.trash') }}</span>
|
||||
<span style="position: relative;top: -0.5px;left: 11px;">{{ $t('commons.trash') }}</span>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
@ -72,4 +72,10 @@ i.el-icon-delete {
|
|||
margin-right: 4px;
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
.to-public {
|
||||
display: flex;
|
||||
padding: 9px 24px;
|
||||
min-width: 200px;
|
||||
}
|
||||
</style>
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
<img :src="url" :alt="$t('test_track.case.img_loading_fail')" style="width: 100%;height: 100%;"
|
||||
v-if="file.type === 'JPG' || file.type === 'JPEG' || file.type === 'PNG'">
|
||||
<div v-if="file.type === 'PDF'">
|
||||
<test-case-pdf :file-id="file.id"/>
|
||||
<test-case-pdf-view-base-js :pdf-url="'/attachment/preview/' + this.file.id + '/' + this.file.isLocal"/>
|
||||
</div>
|
||||
</div>
|
||||
</el-dialog>
|
||||
|
@ -12,12 +12,12 @@
|
|||
|
||||
<script>
|
||||
|
||||
import TestCasePdf from "@/business/case/components/TestCasePdf";
|
||||
import TestCasePdfViewBaseJs from "@/business/case/components/TestCasePdfViewBaseJs";
|
||||
import {generateModuleUrl} from "@/business/utils/sdk-utils";
|
||||
|
||||
export default {
|
||||
name: "TestCaseFiles",
|
||||
components: {TestCasePdf},
|
||||
components: {TestCasePdfViewBaseJs},
|
||||
props: {},
|
||||
data() {
|
||||
return {
|
||||
|
|
|
@ -0,0 +1,129 @@
|
|||
<template>
|
||||
<div ref="pdfViewer" class="pdfViewer" v-loading="loading"></div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import * as PdfJs from 'pdfjs-dist'
|
||||
import pdfJsWorker from 'pdfjs-dist/build/pdf.worker.entry'
|
||||
PdfJs.GlobalWorkerOptions.workerSrc = pdfJsWorker
|
||||
|
||||
export default {
|
||||
name: 'TestCasePdfViewBaseJs',
|
||||
props: {
|
||||
/**
|
||||
* pdf地址
|
||||
*/
|
||||
pdfUrl: {
|
||||
type: String,
|
||||
default: '',
|
||||
},
|
||||
// /**
|
||||
// * cMap的url地址(用于解决中文乱码或中文显示为空白的问题). 该资源的物理路径在: node_modules/pdfjs-dist/cmaps
|
||||
// */
|
||||
// cMapUrl: {
|
||||
// type: String,
|
||||
// default: '',
|
||||
// },
|
||||
/**
|
||||
* pdf缩放比例
|
||||
*/
|
||||
scale: {
|
||||
type: Number,
|
||||
default: 1.85,
|
||||
},
|
||||
},
|
||||
data() {
|
||||
return {
|
||||
loading: false,
|
||||
pdfDocRef: null,
|
||||
}
|
||||
},
|
||||
computed: {
|
||||
urlObj() {
|
||||
return { pdfUrl: this.pdfUrl, cMapUrl: this.cMapUrl }
|
||||
},
|
||||
},
|
||||
watch: {
|
||||
urlObj() {
|
||||
this.renderPdf()
|
||||
},
|
||||
scale() {
|
||||
this.renderPdf()
|
||||
},
|
||||
},
|
||||
mounted() {
|
||||
this.loading = true;
|
||||
this.renderPdf()
|
||||
},
|
||||
methods: {
|
||||
renderPdf() {
|
||||
const pdfViewerDom = this.$refs.pdfViewer;
|
||||
if (this.pdfUrl) {
|
||||
// 获取pdf文件
|
||||
const pdfLoadingTask = PdfJs.getDocument({
|
||||
url: this.pdfUrl,
|
||||
cMapUrl: this.cMapUrl
|
||||
})
|
||||
pdfLoadingTask.promise.then(pdfDoc => {
|
||||
if (pdfDoc && pdfViewerDom) {
|
||||
// 缓存pdf内容
|
||||
this.pdfDocRef = pdfDoc
|
||||
this.loading = false;
|
||||
this.renderPdfCanvas(pdfViewerDom, pdfDoc, this.scale)
|
||||
}
|
||||
})
|
||||
}
|
||||
},
|
||||
/**
|
||||
* 渲染pdf文件的指定页到画板
|
||||
* @param pdfViewerDom 承载pdf画板的dom容器
|
||||
* @param pdfDoc pdf文件
|
||||
* @param pageNum 需要渲染的页码
|
||||
* @param scale 缩放比例
|
||||
*/
|
||||
renderPdfOnePage(pdfViewerDom, pdfDoc, pageNum, scale) {
|
||||
// 创建画布
|
||||
const canvas = document.createElement('canvas')
|
||||
pdfViewerDom.appendChild(canvas)
|
||||
// 获取2d上下文
|
||||
const context = canvas.getContext('2d')
|
||||
pdfDoc.getPage(pageNum).then(page => {
|
||||
// 获取当前pdf页内容, 并设置缩放
|
||||
const viewport = page.getViewport({ scale: scale })
|
||||
const realCanvas = context.canvas
|
||||
realCanvas.width = viewport.width
|
||||
realCanvas.height = viewport.height
|
||||
// 将pdf当前页内容画到2d画板中
|
||||
// @ts-ignore
|
||||
page.render({ canvasContext: context, viewport })
|
||||
})
|
||||
},
|
||||
/**
|
||||
* 渲染pdf的画布
|
||||
* @param pdfViewerDom 承载pdf画布的dom容器
|
||||
* @param pdfDoc pdf文档
|
||||
* @param scale 缩放比例
|
||||
*/
|
||||
renderPdfCanvas(pdfViewerDom, pdfDoc, scale) {
|
||||
// 清除原来的pdf画布
|
||||
pdfViewerDom.innerHTML = ''
|
||||
// 获取总页数
|
||||
const totalPage = pdfDoc.numPages
|
||||
// 获取父元素宽度
|
||||
// let pdfViewer = document.querySelector(".pdfViewer");
|
||||
// let pdfViewerWidth = window.getComputedStyle(pdfViewer).getPropertyValue("width").replaceAll("px", "")
|
||||
// 获取显示容器, canvas占满
|
||||
for (let i = 1; i <= totalPage; i++) {
|
||||
// 循环处理pdf的每页
|
||||
this.renderPdfOnePage(pdfViewerDom, pdfDoc, i, scale);
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
canvas {
|
||||
width: 100%!important;
|
||||
}
|
||||
</style>
|
Loading…
Reference in New Issue