Pdf.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <template>
  2. <div>
  3. <canvas class="pdf pdf-canvas" id="pdf-canvas" :class="classList"></canvas>
  4. <div v-if="loading" class="loading">加载中...</div>
  5. </div>
  6. </template>
  7. <script>
  8. import pdfjs from '@/utils/pdf'
  9. import downloadFile from '@/utils/download-base64'
  10. import { mapState } from 'vuex'
  11. import { SET_TOTAL_PAGES, USE_SCREEN_SHOT } from '@/store/types/pdf-types'
  12. export default {
  13. data () {
  14. return {
  15. loading: true
  16. }
  17. },
  18. mounted () {
  19. this.loading = true
  20. this._canvas = document.getElementById('pdf-canvas')
  21. this._context = this._canvas.getContext('2d')
  22. // Default size
  23. this._canvas.height = 1080
  24. this._canvas.width = 1440
  25. if (this.src) {
  26. this.load(this.src)
  27. } else {
  28. // ! 1秒后重试
  29. setTimeout(() => {
  30. this.load(this.src)
  31. }, 1000)
  32. }
  33. },
  34. computed: {
  35. ...mapState({
  36. src: state => state.pdf.src,
  37. showDiscussion: state => state.pdf.showDiscussion,
  38. currentPage: state => state.pdf.currentPage,
  39. screenShot: state => state.pdf.screenShot
  40. }),
  41. classList () {
  42. return {
  43. 'full-size': !this.showDiscussion
  44. }
  45. }
  46. },
  47. watch: {
  48. currentPage () {
  49. this.showPage()
  50. },
  51. screenShot () {
  52. if (this.screenShot) {
  53. this.getScreenShot()
  54. this.$store.commit(USE_SCREEN_SHOT)
  55. }
  56. }
  57. },
  58. methods: {
  59. load (src) {
  60. const loadingTask = pdfjs.getDocument(src)
  61. loadingTask.promise.then(pdf => {
  62. this.loading = false
  63. this._pdf = pdf
  64. this.$store.commit(SET_TOTAL_PAGES, { totalPages: pdf.numPages })
  65. this.showPage()
  66. })
  67. },
  68. showPage () {
  69. this._pdf && this._pdf.getPage(this.currentPage || 1).then(page => {
  70. const scale = 2.5
  71. const viewport = page.getViewport({ scale: scale })
  72. this._canvas.height = viewport.height
  73. this._canvas.width = viewport.width
  74. const renderContext = {
  75. canvasContext: this._context,
  76. viewport: viewport
  77. }
  78. page.render(renderContext)
  79. }).catch(() => {
  80. this.$setInfoMessage('PDF 当前页解析错误')
  81. })
  82. },
  83. getScreenShot () {
  84. const img = this._canvas.toDataURL('image/png')
  85. downloadFile(img, '课程课件截图.png')
  86. }
  87. }
  88. }
  89. </script>
  90. <style lang="scss" scoped>
  91. @import '~@/style/theme.scss';
  92. div {
  93. position: relative;
  94. }
  95. .loading {
  96. position: absolute;
  97. display: flex;
  98. align-items: center;
  99. justify-content: center;
  100. height: 100%;
  101. width: 100%;
  102. top: 0;
  103. left: 0;
  104. color: #7a8a9a;
  105. font-size: 40px;
  106. }
  107. canvas {
  108. outline: none;
  109. display: block;
  110. direction: ltr;
  111. margin: 0 auto;
  112. background-color: white;
  113. max-width: 100vw;
  114. max-height: 100vh;
  115. }
  116. </style>