fix(测试跟踪): PDF预览问题

--bug=1023487 --user=宋昌昌 【测试跟踪】功能用例,预览附件pdf文件,一直在转圈,console有报错 https://www.tapd.cn/55049933/s/1341230
This commit is contained in:
song-cc-rock 2023-02-22 18:45:30 +08:00 committed by jianxing
parent 26a0d25f1f
commit eb0e6deadc
3 changed files with 140 additions and 5 deletions

View File

@ -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>

View File

@ -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 {

View File

@ -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: '',
},
// /**
// * cMapurl(). : 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
// pdf2d
// @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>