加载中...

vue-pdf实现文件预览放大、缩小、上下页

1.下载依赖

cnpm install --save vue-pdf

2.代码

  1. ​
  2. <style lang="less" scoped>
  3. .main {
  4. overflow: auto;
  5. max-width: 375px;
  6. max-height: 530px;
  7. }
  8. </style>
  9. <template>
  10. <div class="pdf">
  11. <div class="main">
  12. <pdf ref="pdf"
  13. :src="pdfUrl"
  14. :page="pageNum"
  15. :rotate="pageRotate"
  16. @password="password"
  17. @progress="loadedRatio = $event"
  18. @page-loaded="pageLoaded($event)"
  19. @num-pages="pageTotalNum=$event"
  20. @error="pdfError($event)"
  21. @link-clicked="page = $event">
  22. </pdf>
  23. </div>
  24. <div class="pdf-tab">
  25. <!-- <div
  26. @click.stop="clock">顺时针</div>
  27. <div
  28. @click.stop="counterClock">逆时针</div> -->
  29. </div>
  30. <button class="btn-def btn-pre"
  31. @click.stop="prePage">上一页</button>
  32. <button class="btn-def btn-next"
  33. @click.stop="nextPage">下一页</button>
  34. <div>{{pageNum}}/{{pageTotalNum}}</div>
  35. <button :class="{select:idx==0}"
  36. @touchstart="idx=0"
  37. @touchend="idx=-1"
  38. @click="scaleD">
  39. 放大
  40. </button>
  41. <button :class="{select:idx==1}"
  42. @touchstart="idx=1"
  43. @touchend="idx=-1"
  44. @click="scaleX">
  45. 缩小
  46. </button>
  47. <button @click="fileDownload(pdfUrl,''pdf文件'')">下载</button>
  48. <!-- <div>进度:{{loadedRatio}}</div> -->
  49. <!-- <div>页面加载成功: {{curPageNum}}</div> -->
  50. </div>
  51. </template>
  52. <script>
  53. import closeLoading from ''../../mixins/closeLoading''
  54. import pdf from ''vue-pdf''
  55. export default {
  56. name: ''Pdf'',
  57. mixins: [closeLoading],
  58. components: {
  59. pdf
  60. },
  61. data() {
  62. return {
  63. pdfUrl: "http://192.168.6.135:8882/A1.pdf",
  64. pageNum: 1,
  65. pageTotalNum: 1,
  66. pageRotate: 0,
  67. // 加载进度
  68. loadedRatio: 0,
  69. curPageNum: 0,
  70. scale: 100, //放大系数
  71. idx: -1,
  72. }
  73. },
  74. mounted: function () {
  75. },
  76. methods: {
  77. //下载PDF
  78. fileDownload (data, fileName) {
  79. let blob = new Blob([data], {
  80. //type类型后端返回来的数据中会有,根据自己实际进行修改
  81. type: "application/pdf;charset-UTF-8"
  82. });
  83. let filename = fileName || "报表.xls";
  84. if (typeof window.navigator.msSaveBlob !== "undefined") {
  85. window.navigator.msSaveBlob(blob, filename);
  86. } else {
  87. var blobURL = window.URL.createObjectURL(blob);
  88. // 创建隐藏<a>标签进行下载
  89. var tempLink = document.createElement("a");
  90. tempLink.style.display = "none";
  91. tempLink.href = blobURL;
  92. tempLink.setAttribute("download", filename);
  93. if (typeof tempLink.download === "undefined") {
  94. tempLink.setAttribute("target", "_blank");
  95. }
  96. document.body.appendChild(tempLink);
  97. tempLink.click();
  98. document.body.removeChild(tempLink);
  99. window.URL.revokeObjectURL(blobURL);
  100. }
  101. },
  102. //放大
  103. scaleD() {
  104. this.scale += 5;
  105. this.$refs.pdf.$el.style.width = parseInt(this.scale) + "%";
  106. },
  107. //缩小
  108. scaleX() {
  109. if (this.scale == 100) {
  110. return;
  111. }
  112. this.scale += -5;
  113. this.$refs.pdf.$el.style.width = parseInt(this.scale) + "%";
  114. },
  115. prePage() {
  116. var p = this.pageNum
  117. p = p > 1 ? p - 1 : this.pageTotalNum
  118. this.pageNum = p
  119. },
  120. nextPage() {
  121. var p = this.pageNum
  122. p = p < this.pageTotalNum ? p + 1 : 1
  123. this.pageNum = p
  124. },
  125. clock() {
  126. this.pageRotate += 90
  127. },
  128. counterClock() {
  129. this.pageRotate -= 90
  130. },
  131. password(updatePassword, reason) {
  132. updatePassword(prompt(''password is "123456"''))
  133. console.log(''...reason...'')
  134. console.log(reason)
  135. console.log(''...reason...'')
  136. },
  137. pageLoaded(e) {
  138. this.curPageNum = e
  139. },
  140. pdfError(error) {
  141. console.error(error)
  142. },
  143. pdfPrintAll() {
  144. this.$refs.pdf.print()
  145. },
  146. pdfPrint() {
  147. this.$refs.pdf.print(100, [1, 2])
  148. },
  149. }
  150. }
  151. </script>
  152. ​

 

扫码添加商务顾问
项目合作一对一沟通
联系二维码
商务经理