WXL
2025-12-28 1b736033f6d01b774d58b4c2d7cd2ce8607a44fa
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<template>
  <div class="pdf-preview-container" v-loading="loading">
    <!-- 控制工具栏 -->
    <div class="pdf-controls">
      <el-button-group>
        <el-button
          size="mini"
          @click="prevPage"
          :disabled="currentPage <= 1"
          icon="el-icon-arrow-left"
        >
          上一页
        </el-button>
        <el-button size="mini" disabled>
          第 {{ currentPage }} 页 / 共 {{ totalPages }} 页
        </el-button>
        <el-button
          size="mini"
          @click="nextPage"
          :disabled="currentPage >= totalPages"
          icon="el-icon-arrow-right"
        >
          下一页
        </el-button>
      </el-button-group>
 
      <el-button-group class="zoom-controls">
        <el-button size="mini" @click="zoomOut" :disabled="scale <= 50">
          <i class="el-icon-zoom-out"></i> 缩小
        </el-button>
        <el-button size="mini" disabled>{{ scale }}%</el-button>
        <el-button size="mini" @click="zoomIn" :disabled="scale >= 200">
          <i class="el-icon-zoom-in"></i> 放大
        </el-button>
        <el-button size="mini" @click="resetZoom">
          <i class="el-icon-refresh-left"></i> 重置
        </el-button>
      </el-button-group>
 
      <el-button size="mini" type="success" @click="handleDownload" icon="el-icon-download">
        下载
      </el-button>
    </div>
 
    <!-- PDF渲染区域 -->
    <div class="pdf-render-area">
      <pdf
        ref="pdf"
        :src="pdfUrl"
        :page="currentPage"
        @num-pages="totalPages = $event"
        @page-loaded="currentPage = $event"
        @loaded="loadPdfHandler"
        @error="pdfErrorHandler"
        :style="{
          width: scale + '%',
          transform: 'scale(' + scale / 100 + ')',
          transformOrigin: '0 0'
        }"
      ></pdf>
    </div>
  </div>
</template>
 
<script>
import pdf from 'vue-pdf'
 
export default {
  name: 'PdfPreview',
  components: {
    pdf
  },
  props: {
    pdfUrl: {
      type: String,
      required: true
    },
    fileName: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      loading: false,
      currentPage: 1,
      totalPages: 0,
      scale: 100
    }
  },
  methods: {
    loadPdfHandler() {
      this.loading = false;
    },
 
    pdfErrorHandler(error) {
      console.error('PDF加载失败:', error);
      this.loading = false;
      this.$message.error('PDF文件加载失败,请尝试下载后查看');
    },
 
    prevPage() {
      if (this.currentPage > 1) {
        this.currentPage--;
      }
    },
 
    nextPage() {
      if (this.currentPage < this.totalPages) {
        this.currentPage++;
      }
    },
 
    zoomIn() {
      if (this.scale >= 200) {
        this.$message.info("已放大到最大比例");
        return;
      }
      this.scale += 10;
    },
 
    zoomOut() {
      if (this.scale <= 50) {
        this.$message.info("已缩小到最小比例");
        return;
      }
      this.scale -= 10;
    },
 
    resetZoom() {
      this.scale = 100;
    },
 
    handleDownload() {
      const link = document.createElement('a');
      link.href = this.pdfUrl;
      link.download = this.fileName;
      link.style.display = 'none';
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
      this.$message.success('开始下载文件');
    }
  },
  mounted() {
    this.loading = true;
  },
  watch: {
    pdfUrl() {
      this.loading = true;
      this.currentPage = 1;
      this.scale = 100;
    }
  }
}
</script>
 
<style scoped>
.pdf-preview-container {
  height: 100%;
  display: flex;
  flex-direction: column;
}
 
.pdf-controls {
  padding: 15px 20px;
  background: #f5f7fa;
  border-bottom: 1px solid #ebeef5;
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
}
 
.pdf-render-area {
  flex: 1;
  overflow: auto;
  padding: 20px;
  background: #f8f9fa;
  display: flex;
  justify-content: center;
  align-items: flex-start;
}
 
.zoom-controls {
  margin: 0 15px;
}
 
@media (max-width: 768px) {
  .pdf-controls {
    flex-direction: column;
    gap: 10px;
  }
 
  .zoom-controls {
    margin: 10px 0;
  }
}
</style>