zhangwei
2025-08-28 06e4c8c1bad514992186d94a65c0dc6250e34716
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
<template>
  <div
    v-loading="isLoading"
    class="pdf-container"
    element-loading-text="加载中..."
  >
    <iframe
      id="printIframe"
      frameborder="0"
      style="width: 100%; height: 100%"
      @load="iframeLoaded"
    />
  </div>
</template>
 
<script setup lang="ts">
import { ref, defineProps } from "vue";
const props = defineProps({
  // fileInfo: {
  //   type: String,
  //   default: ""
  // }
  fileInfo: {
    type: Object,
    required: true
  }
});
let isLoading = ref(true);
const iframeLoaded = () => {
  console.log("jjjjjj");
 
  isLoading.value = false;
};
const pdfUrl = props.fileInfo.filePath;
// 用fetch获取文件流,强制转为预览格式
fetch(pdfUrl)
  .then(response => {
    // 检查响应是否成功
    if (!response.ok) throw new Error("文件获取失败");
    // 强制获取Blob对象(指定PDF类型)
    return response.blob();
  })
  .then(blob => {
    // 生成临时预览URL(浏览器本地临时链接,无下载触发)
    const blobUrl = URL.createObjectURL(
      new Blob([blob], { type: "application/pdf" }) // 明确指定PDF类型
    );
    // 赋值给iframe并显示
    const iframe = document.getElementById("printIframe");
    iframe.src = blobUrl;
    iframe.style.display = "block";
    document.getElementById("loading").style.display = "none";
  })
  .catch(error => {
    // document.getElementById("loading").innerText = `加载失败:${error.message}`;
  });
</script>
<style>
.pdf-container {
  height: 600px;
}
</style>