加载中...

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

1.下载依赖

cnpm install --save vue-pdf

2.代码

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