pdfview.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <template>
  2. <div v-loading="loading"
  3. element-loading-text="加载中,请稍后..."
  4. element-loading-spinner="el-icon-loading"
  5. element-loading-background="rgba(0, 0, 0, 0.8)">
  6. <pdf ref="pdf"
  7. :src="src"
  8. v-for="i in pageCount"
  9. :key="i"
  10. :page="i"
  11. >
  12. </pdf>
  13. <!-- <div class="toolBar" style="display:flex;" justify="center">
  14. <van-col span="8" style="text-align: center;"><a @click="changePdfPage(0)" :disabled="currentPage==1">上一页</a></van-col>
  15. <van-col span="8" style="text-align: center;"><span>{{currentPage}} / {{pageCount}}</span></van-col>
  16. <van-col span="8" style="text-align: center;"><a @click="changePdfPage(1)" :disabled="currentPage==pageCount">下一页</a></van-col>
  17. </div> -->
  18. </div>
  19. </template>
  20. <script>
  21. import pdf from 'vue-pdf'
  22. var headers = {
  23.     'Authorization': 'Bearer SOME_TOKEN',
  24.     'x-ipp-device-uuid': 'SOME_UUID',
  25.     'x-ipp-client': 'SOME_ID',
  26.     'x-ipp-client-version': 'SOME_VERSION'
  27. };
  28. // var loadingTask = pdf.createLoadingTask({
  29. //     url:'/static/upload/222.pdf',
  30. //     httpHeaders:headers
  31. // });
  32. var loadingTask;
  33. export default {
  34. components: {pdf},
  35. data () {
  36. return {
  37. loading:true,
  38. serverFname:null,
  39. fileName:null,
  40. file:null,
  41. currentPage: 1, // pdf文件页码
  42. pageCount: 1, // pdf文件总页数
  43. fileType: 'pdf', // 文件类型     
  44. src: null, //'/upload/1d43c8c07f5944a9a1dd9e5e0197aa00.pdf', // pdf文件地址,
  45. }
  46. },  
  47. created(){
  48.   },
  49. methods: {
  50. handleError(e) {
  51. this.$toast.clear();
  52. this.$toast.fail('加载失败:'+e);
  53. },
  54. // 改变PDF页码,val传过来区分上一页下一页的值,0上一页,1下一页
  55. changePdfPage (val) {
  56. // console.log(val)
  57. if (val === 0 && this.currentPage > 1) {
  58. this.currentPage--
  59. // console.log(this.currentPage)
  60. }
  61. if (val === 1 && this.currentPage < this.pageCount) {
  62. this.currentPage++
  63. // console.log(this.currentPage)
  64. }
  65. },
  66. // pdf加载时
  67. loadPdfHandler (e) {
  68. this.currentPage = 1 // 加载的时候先加载第一页
  69. },
  70. back() {
  71. history.back();
  72. },
  73. },
  74. mounted(){
  75. console.log(sessionStorage.getItem("serverFname"));
  76. loadingTask = pdf.createLoadingTask({
  77.     url:sessionStorage.getItem("serverFname"),
  78.     httpHeaders:headers
  79. });
  80. this.src = loadingTask;
  81. this.serverFname = sessionStorage.getItem("serverFname");
  82. this.fileName = sessionStorage.getItem("fileName");
  83. loadingTask.promise.then(pdf => {
  84. this.pageCount = pdf.numPages;
  85. console.log('pageCount======'+this.pageCount);
  86. this.loading = false;
  87. }).catch(err => {
  88. console.error('pdf 加载失败', err);
  89. this.loading = false;
  90. this.$message({
  91. message: "加载失败",
  92. type: 'error'
  93. });
  94. })
  95. }
  96. }
  97. </script>
  98. <style>
  99. .toolBar {
  100. position:fixed; bottom:0;left:0; width:100%;
  101. padding:10px;
  102. background:#ddd;
  103. }
  104. .control_bar {
  105. float:right;
  106. position:fixed;
  107. bottom:80px;
  108. right:0px;
  109. margin-right:3px;
  110. background:#ddd;
  111. opacity:0.4;
  112. filter:alpha(opacity=40);
  113. border-radius:3px;
  114. padding:3px;
  115. }
  116. </style>