<!-- 文件预览组件(通过文件名获取文件url) --> <script lang="ts" setup name="FilePreview"> import VueOfficePdf from '@vue-office/pdf' // import * as pdfjsLib from 'pdfjs-dist' import { ElLoading } from 'element-plus' import { getPhotoUrl } from '@/api/file' // 打印文件url const props = defineProps({ printFileName: { type: String, }, }) const pdfUrl = ref('') /** * 初始化 */ function init() { const loading = ElLoading.service({ lock: true, text: '加载中...', background: 'rgba(255, 255, 255, 0.6)', }) getPhotoUrl(props.printFileName!).then((res: any) => { console.log('文件名称', props.printFileName) pdfUrl.value = res.data console.log('pdfurl', pdfUrl.value) // pdfjsLib.GlobalWorkerOptions.workerSrc = '/pdf.worker.js' // proxy.$refs.pdf.print() // download(url, row.printFileName as string) loading.close() }) } const rendered = () => { console.log('渲染完成') } const error = () => { console.log('渲染失败') } watch(() => props.printFileName, (newVal) => { console.log('newVal', newVal) if (newVal) { init() } }, { immediate: true }) onMounted(() => { // init() }) </script> <template> <!-- pdf预览 --> <vue-office-pdf :src="pdfUrl" class="docx-calss" @rendered="rendered" @error="error"/> </template> <style scoped> /* 这块是处理边上的灰边 */ :deep(.vue-office-pdf-wrapper) { padding-top: 0 !important; background-color: transparent !important;; } </style>