PdfView.vue 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <template>
  2. <div class="pdfView-box">
  3. <div class="pdfView">
  4. <pdf
  5. v-for="i in curQue.data.numPages"
  6. ref="pdf"
  7. :key="i"
  8. :src="pdfSrc"
  9. :page="i"
  10. @progress="loadedRatio = $event"
  11. />
  12. </div>
  13. <div v-show="loadedRatio!==1" class="progress">
  14. <el-progress type="circle" :width="70" color="#53a7ff" :percentage="Math.floor(loadedRatio*100)" />
  15. <br>
  16. <span>{{ remindShow }}</span>
  17. </div>
  18. </div>
  19. </template>
  20. <script>
  21. import pdf from "vue-pdf";
  22. export default {
  23. // import引入的组件需要注入到对象中才能使用
  24. components: { pdf },
  25. props: ["curQue", "fn_data", "type"],
  26. data() {
  27. // 这里存放数据
  28. return {
  29. numPages: null,
  30. remindShow: "加载文件中,文件较大请耐心等待...",
  31. loadedRatio: 0,
  32. pdfSrc: ''
  33. };
  34. },
  35. created() {
  36. this.getNumPages();
  37. },
  38. methods: {
  39. getNumPages() {
  40. let str = this.curQue.data.fileRelativePath;
  41. str.replace(process.env.VUE_APP_BASE_API, '');
  42. let loadingTask = pdf.createLoadingTask(`${process.env.VUE_APP_PDF}${str}`);
  43. loadingTask.promise
  44. .then(pdff => {
  45. console.log("拿到结果");
  46. this.pdfSrc = loadingTask;
  47. this.numPages = pdff.numPages;
  48. this.curQue.data.numPages = pdff.numPages;
  49. })
  50. .catch(err => {
  51. console.error("pdf 加载失败", err);
  52. });
  53. },
  54. },
  55. };
  56. </script>
  57. <style scoped>
  58. /* @import url(); 引入css类 */
  59. .pdfView-box{
  60. width:100%;
  61. }
  62. .pdfView {
  63. width: 100%;
  64. }
  65. .progress{
  66. position: absolute;
  67. right: 50%;
  68. top: 50%;
  69. text-align: center;
  70. width: 500px;
  71. margin-right: -250px;
  72. }
  73. </style>