index.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <!--
  2. * @Description: pdf预览弹框
  3. * @Author: Rockery
  4. * @Date: 2021-12-13 17:40:22
  5. * @LastEditors: Rockery
  6. * @LastEditTime: 2022-01-19 10:01:57
  7. * @FilePath: \party_construct_web\src\components\RocVuePdfDialog\index.vue
  8. * @Copyright: Copyright (c) 2016~2021 Rockery(1113269755@qq.com)
  9. -->
  10. <template>
  11. <div class="rocvuepdfdialog">
  12. <el-dialog
  13. :title="dialogTitle"
  14. :visible.sync="currentVisible"
  15. width="60%"
  16. append-to-body
  17. class="rocvuepdfdialog-dialog"
  18. @close="handleCloseDialogClick"
  19. >
  20. <div v-loading="loadedRatio !== 1">
  21. <el-button-group>
  22. <el-button size="mini" @click="handleHomePage">首 页</el-button>
  23. <el-button type="primary" icon="el-icon-arrow-left" size="mini" @click="handlePrePage">上一页</el-button>
  24. <el-button type="primary" size="mini" @click="handleNextPage">
  25. 下一页
  26. <i class="el-icon-arrow-right el-icon--right"></i>
  27. </el-button>
  28. <el-button size="mini" @click="handleLastPage">尾 页</el-button>
  29. </el-button-group>
  30. <div class="rocvuepdfdialog-dialog-page">当前页码:{{ pageNum }} / 总页码:{{ numPages }}</div>
  31. <div
  32. v-show="loadedRatio === 1"
  33. ref="rocVuePdfContainerRef"
  34. class="rocvuepdfdialog-dialog-container"
  35. >
  36. <vue-pdf
  37. :src="pdfUrl"
  38. ref="vuePdfRef"
  39. :page="pageNum"
  40. @num-pages="numPages = $event"
  41. @progress="loadedRatio = $event"
  42. @page-loaded="pageNum = $event"
  43. @loaded="loadPdfHandler"
  44. @link-clicked="pageNum = $event"
  45. ></vue-pdf>
  46. </div>
  47. <template v-if="loadedRatio !== 1">
  48. <div class="rocvuepdfdialog-dialog-nodata">文件预览失败</div>
  49. </template>
  50. </div>
  51. <div slot="footer" class="dialog-footer">
  52. <el-button @click="handleCloseDialogClick">关 闭</el-button>
  53. </div>
  54. </el-dialog>
  55. </div>
  56. </template>
  57. <script>
  58. import vuePdf from 'vue-pdf';
  59. export default {
  60. name: 'Rocvuepdfdialog',
  61. components: {
  62. 'vue-pdf': vuePdf
  63. },
  64. props: {
  65. // 是否显示对话框
  66. visible: {
  67. type: Boolean,
  68. required: true,
  69. default: true
  70. },
  71. // 对话框标题
  72. dialogTitle: {
  73. type: String,
  74. required: false,
  75. default: 'PDF文件预览'
  76. },
  77. // PDF 文件访问路径
  78. pdfUrl: {
  79. type: String,
  80. required: true,
  81. default: ''
  82. }
  83. },
  84. data() {
  85. return {
  86. numPages: 0, // pdf 总页数
  87. pageNum: 1,
  88. loadedRatio: 0, // 当前页面的加载进度,范围是0-1 ,等于1的时候代表当前页已经完全加载完成了
  89. };
  90. },
  91. computed: {
  92. currentVisible: {
  93. get() {
  94. return this.visible;
  95. },
  96. set(val) {
  97. this.$emit('update:visible', val);
  98. }
  99. }
  100. },
  101. methods: {
  102. /**
  103. * pdf加载时
  104. */
  105. loadPdfHandler(e) {
  106. // 加载的时候先加载第一页
  107. this.pageNum = 1;
  108. },
  109. /**
  110. * 首页按钮事件
  111. */
  112. handleHomePage() {
  113. this.pageNum = 1;
  114. this.toTop();
  115. },
  116. /**
  117. * 上一页按钮事件
  118. */
  119. handlePrePage() {
  120. let page = this.pageNum;
  121. page = page > 1 ? page - 1 : this.numPages;
  122. this.pageNum = page;
  123. this.toTop();
  124. },
  125. /**
  126. * 下一页按钮事件
  127. */
  128. handleNextPage() {
  129. let page = this.pageNum;
  130. page = page < this.numPages ? page + 1 : 1;
  131. this.pageNum = page;
  132. this.toTop();
  133. },
  134. /**
  135. * 尾页按钮事件
  136. */
  137. handleLastPage() {
  138. this.pageNum = this.numPages;
  139. this.toTop();
  140. },
  141. /**
  142. * 对话框关闭事件
  143. */
  144. handleCloseDialogClick() {
  145. // this.$emit("close", false);
  146. this.currentVisible = false;
  147. },
  148. /**
  149. * 页面回到顶部
  150. */
  151. toTop() {
  152. this.$refs.rocVuePdfContainerRef && (this.$refs.rocVuePdfContainerRef.scrollTop = 0);
  153. }
  154. }
  155. }
  156. </script>
  157. <style lang="scss" scoped>
  158. .rocvuepdfdialog {
  159. &-dialog {
  160. ::v-deep {
  161. .el-dialog__header {
  162. border-bottom: 1px solid #dfe4ed;
  163. }
  164. .el-dialog__body {
  165. padding: 20px 20px 30px;
  166. text-align: center;
  167. }
  168. }
  169. &-page {
  170. margin: 10px 0;
  171. color: #409eff;
  172. }
  173. &-container {
  174. position: relative;
  175. overflow: auto;
  176. width: 100%;
  177. height: 550px;
  178. border: 1px solid #d9d9d9;
  179. }
  180. &-nodata {
  181. display: flex;
  182. justify-content: center;
  183. align-items: center;
  184. height: 200px;
  185. font-size: 22px;
  186. color: #747474;
  187. opacity: 0.8;
  188. }
  189. }
  190. }
  191. </style>