qx
qx
12 小时以前 495bf3ca62536c52cf78895501205965cae05828
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<template>
  <div>
    <el-button
      size="mini"
            type="text"
            icon="el-icon-document"
      class="btn"
      @click="dialogVisible = true"
      >{{ btnText }}</el-button
    >
 
    <el-dialog
      title="PDF 预览"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
    >
      <div class="main">
        <iframe
          id="printIframe"
          :src=" src"
          frameborder="0"
          style="width: 100%; height: 100%"
        ></iframe>
      </div>
    </el-dialog>
  </div>
</template>
 
<script>
export default {
  name: "home",
  data() {
    return {
      dialogVisible: false,
      btnText: "查看报告",
      src: "/test.pdf",
      url: "",
    };
  },
  methods: {
    downLoadFileImg(fileUrl, fileName) {
      // 后端文件地址和名称
 
      // 可下载,名称也有效 -- 推荐
      // const x = new window.XMLHttpRequest()
      // x.open('GET', fileUrl, true)
      // x.responseType = 'blob' // 选择返回格式为blob --- 一般后端返回的是看不懂的文件流 故需要转成blob
      // x.onload = () => {
      //   this.url = window.URL.createObjectURL(x.response) //将后端返回的blob文件读取出url
 
      // console.log('blob====',x.response)   //Blob {size: 38617, type: 'application/pdf'}
      // console.log('url====',this.url)   //blob:http://localhost:7197/cb047277-e5e6-4905-bf8c-dbecd86a0105
 
      // url可以预览和下载------如果想要下载就把下方注释打开即可
      // const a = document.createElement('a')
      // a.href = this.url
      // a.download = fileName
      // a.click()
      // }
      // x.send()
 
      // // 或者下方的方式
      axios({
        url: `http://localhost:81/dev-api/system/report/toPdf + "?tjNumber=" + this.row.tjNumber`,
        method: "get",
        timeout: 100000,
        responseType: "blob",
        headers: {
          type: "application/pdf",
        },
      })
        .then((res) => {
          // console.log("res.data", res.data);
          this.url = window.URL.createObjectURL(res.data); //将后端返回的blob文件读取出url
          // console.log("通过读取blob文件得到url地址===", this.url);
        })
        .catch((error) => {
          console.log(error);
        });
    },
  },
};
</script>
 
<style scoped>
.btn {
  margin: 20px 0px;
}
.main {
  height: 700px;
  overflow: hidden;
}
#printIframe::-webkit-scrollbar {
  width: 6px;
}
/* 修改 滚动条的 下面 的 样式 */
#printIframe::-webkit-scrollbar-track {
  background-color: white;
  -webkit-border-radius: 2em;
  -moz-border-radius: 2em;
  border-radius: 2em;
}
/* 修改 滑块 */
#printIframe::-webkit-scrollbar-thumb {
  background-color: #dcdfe6;
  -webkit-border-radius: 2em;
  -moz-border-radius: 2em;
  border-radius: 2em;
}
.el-dialog {
  width: 1000px;
  height: 700px;
}
.el-dialog__header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.el-dialog__body {
  padding: 0;
}
</style>