preview.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <template>
  2. <div class="head">
  3. <span class="head-text">学习资料提取</span>
  4. <div class="head-button">
  5. <a :href="pdfUrl" class="head-button-download">
  6. <img
  7. class=""
  8. style="width: 12px; height: 13px; margin-right: 5px"
  9. src="@/assets/img/downIcon.png"
  10. />
  11. 下载到本地
  12. </a>
  13. <div class="head-button-send">
  14. <img
  15. style="width: 12px; height: 13px; margin-right: 5px"
  16. src="@/assets/img/sendIcon.png"
  17. />发送到邮箱
  18. </div>
  19. </div>
  20. </div>
  21. <div id="pdf-canvas">
  22. </div>
  23. </template>
  24. <script lang="ts">
  25. import { defineComponent } from 'vue';
  26. import * as PDFJS from 'pdfjs-dist/legacy/build/pdf'; // 引入PDFJS
  27. import pdfjsWorker from 'pdfjs-dist/legacy/build/pdf.worker.entry.js'; // 引入workerSrc的地址
  28. import 'pdfjs-dist/web/pdf_viewer.css';
  29. import { PDFDocumentProxy } from 'pdfjs-dist/types/web/pdf_find_controller';
  30. PDFJS.GlobalWorkerOptions.workerSrc = pdfjsWorker; //设置PDFJS.GlobalWorkerOptions.workerSrc的地址
  31. export default defineComponent({
  32. setup() {},
  33. data() {
  34. return {
  35. pdfPagesNum: 1,
  36. readerpdfDoc: null,
  37. pdfUrl:""
  38. };
  39. },
  40. methods: {
  41. getPdf(url: string) {
  42. let that = this;
  43. document.getElementById('pdf-canvas').innerHTML=""
  44. PDFJS.getDocument(url).promise.then((pdfDoc) => {
  45. this.pdfPagesNum = pdfDoc.numPages; // pdf的总页数
  46. //获取第pageNum页的数据
  47. for (let index = 1; index <= pdfDoc.numPages; index++) {
  48. that.showPdfs(pdfDoc, index);
  49. }
  50. });
  51. },
  52. showPdfs(pdfDoc: PDFDocumentProxy, pageNum: number) {
  53. pdfDoc.getPage(pageNum).then((page) => {
  54. // 设置canvas相关的属性
  55. // console.log(canvasDomArr.value)
  56. const canvas:HTMLCanvasElement = document.createElement('canvas')
  57. canvas.classList.add('pdf-canvas');
  58. const ctx = canvas.getContext('2d');
  59. const dpr = window.devicePixelRatio || 1;
  60. const bsr =
  61. ctx?.webkitBackingStorePixelRatio ||
  62. ctx?.mozBackingStorePixelRatio ||
  63. ctx?.msBackingStorePixelRatio ||
  64. ctx?.oBackingStorePixelRatio ||
  65. ctx?.backingStorePixelRatio ||
  66. 1;
  67. const ratio = dpr / bsr;
  68. const viewport = page.getViewport({ scale: 1 });
  69. canvas.width = viewport.width * ratio;
  70. canvas.height = viewport.height * ratio;
  71. // canvas.style.width = viewport.width + "px";
  72. // canvas.style.height = viewport.height + "px";
  73. ctx.setTransform(ratio, 0, 0, ratio, 0, 0);
  74. const context = {
  75. canvasContext: ctx,
  76. viewport: viewport,
  77. };
  78. // 数据渲染到canvas画布上
  79. page.render(context);
  80. //插入canvas
  81. document.getElementById('pdf-canvas')?.appendChild(canvas)
  82. });
  83. },
  84. },
  85. mounted () {
  86. this.getPdf('./web/科目一标识.pdf');
  87. },
  88. });
  89. // PDFJS.GlobalWorkerOptions.workerSrc = pdfjsWorker; //设置PDFJS.GlobalWorkerOptions.workerSrc的地址
  90. // let container = ref(null);
  91. // let canvasDomArr = ref([]);
  92. // let renderContext = (el) => {
  93. // canvasDomArr.value.push(el);
  94. // };
  95. // let pdfUrl = './web/科目一标识.pdf';
  96. // let pdfPagesNum = ref(0);
  97. // let readerpdfDoc = null;
  98. // function pdfChange(event) {
  99. // if (window.createObjectURL != undefined) {
  100. // // basic
  101. // pdfUrl = window.createObjectURL(event.target.files[0]);
  102. // } else if (window.URL != undefined) {
  103. // // mozilla(firefox)
  104. // pdfUrl = window.URL.createObjectURL(event.target.files[0]);
  105. // } else if (window.webkitURL != undefined) {
  106. // // webkit or chrome
  107. // pdfUrl = window.webkitURL.createObjectURL(event.target.files[0]);
  108. // }
  109. // getPdf(pdfUrl, 1);
  110. // }
  111. // function getPdf(url, pageNum) {
  112. // PDFJS.getDocument(url).promise.then((pdfDoc) => {
  113. // pdfPagesNum.value = pdfDoc.numPages; // pdf的总页数
  114. // //获取第pageNum页的数据
  115. // for (let index = 1; index <= pdfDoc.numPages; index++) {
  116. // readerpdfDoc = pdfDoc;
  117. // showPdfs(pdfDoc, index);
  118. // }
  119. // });
  120. // }
  121. // function currentChange(num) {
  122. // showPdfs(readerpdfDoc, num);
  123. // }
  124. // function showPdfs(pdfDoc, pageNum) {
  125. // pdfDoc.getPage(pageNum).then((page) => {
  126. // // 设置canvas相关的属性
  127. // // console.log(canvasDomArr.value)
  128. // const canvas = canvasDomArr.value[pageNum - 1];
  129. // console.log(canvas)
  130. // const ctx = canvas.getContext('2d');
  131. // const dpr = window.devicePixelRatio || 1;
  132. // const bsr =
  133. // ctx.webkitBackingStorePixelRatio ||
  134. // ctx.mozBackingStorePixelRatio ||
  135. // ctx.msBackingStorePixelRatio ||
  136. // ctx.oBackingStorePixelRatio ||
  137. // ctx.backingStorePixelRatio ||
  138. // 1;
  139. // const ratio = dpr / bsr;
  140. // const viewport = page.getViewport({ scale: 1 });
  141. // canvas.width = viewport.width * ratio;
  142. // canvas.height = viewport.height * ratio;
  143. // // canvas.style.width = viewport.width + "px";
  144. // // canvas.style.height = viewport.height + "px";
  145. // ctx.setTransform(ratio, 0, 0, ratio, 0, 0);
  146. // const context = {
  147. // canvasContext: ctx,
  148. // viewport: viewport,
  149. // };
  150. // // 数据渲染到canvas画布上
  151. // page.render(context);
  152. // });
  153. // }
  154. // onMounted(() => {
  155. // getPdf('./web/科目一标识.pdf', 1);
  156. // });
  157. </script>
  158. <style lang="scss">
  159. .head {
  160. width: 100%;
  161. height: 118px;
  162. padding-left: 20px;
  163. padding-right: 40px;
  164. display: flex;
  165. align-content: center;
  166. align-items: center;
  167. justify-content: space-between;
  168. .head-text {
  169. color: #498ef5;
  170. font-size: 34px;
  171. }
  172. .head-button {
  173. display: flex;
  174. justify-content: space-between;
  175. .head-button-download {
  176. background: #01c18d;
  177. width: 204px;
  178. height: 48px;
  179. color: #fff;
  180. border-radius: 60px;
  181. margin-right: 15px;
  182. line-height: 48px;
  183. font-size: 26px;
  184. display: flex;
  185. align-content: center;
  186. align-items: center;
  187. justify-content: center;
  188. }
  189. .head-button-send {
  190. background: #498ef5;
  191. width: 204px;
  192. height: 48px;
  193. color: #fff;
  194. border-radius: 60px;
  195. line-height: 48px;
  196. font-size: 26px;
  197. display: flex;
  198. align-content: center;
  199. align-items: center;
  200. justify-content: center;
  201. }
  202. }
  203. }
  204. .pdf-canvas {
  205. width: 100%;
  206. }
  207. </style>