PdfPreview.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <template>
  2. <div class="pdf-viewer">
  3. <div class="pdf-content">
  4. <canvas ref="pdfCanvas"></canvas>
  5. </div>
  6. <div class="pagination">
  7. <button @click="prevPage">上一页</button>
  8. <span>{{ pageNum }} / {{ pageCount }}</span>
  9. <button @click="nextPage">下一页</button>
  10. <!-- 添加返回按钮 -->
  11. <button @click="goBack">返回</button>
  12. </div>
  13. </div>
  14. </template>
  15. <script>
  16. window.pdfjsLib = pdfjsLib
  17. pdfjsLib.GlobalWorkerOptions.workerSrc =
  18. 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.10.377/pdf.worker.min.js'
  19. export default {
  20. data() {
  21. return {
  22. dialogVisible: false,
  23. pdfDoc: null,
  24. pageNum: 1,
  25. pageCount: 0,
  26. pdfUrl: ''
  27. }
  28. },
  29. watch: {
  30. // 观察 pdfUrl 的变化
  31. pdfUrl(newVal) {
  32. console.log(newVal,'newVal');
  33. if (newVal !== this.pdfUrl) {
  34. this.dialogVisible = false
  35. this.openPdf();
  36. }
  37. }
  38. },
  39. mounted() {
  40. this.pdfUrl = this.$route.query.url || ''; // 假设pdfUrl是通过查询参数传递的
  41. console.log( this.pdfUrl,' this.pdfUrl1111111');
  42. this.openPdf()
  43. },
  44. methods: {
  45. goBack(){
  46. this.dialogVisible = false
  47. this.$router.go(-1)
  48. },
  49. openPdf() {
  50. this.dialogVisible = true
  51. this.loadPdf()
  52. },
  53. closeDialog() {
  54. this.pdfDoc = null
  55. this.pageNum = 1
  56. },
  57. async loadPdf() {
  58. try {
  59. // 加载 PDF 文档
  60. const loadingTask = pdfjsLib.getDocument(this.pdfUrl)
  61. this.pdfDoc = await loadingTask.promise
  62. this.pageCount = this.pdfDoc.numPages
  63. this.renderPage(this.pageNum)
  64. } catch (error) {
  65. this.$message.error('加载 PDF 失败: ' + error.message)
  66. }
  67. },
  68. async renderPage(num) {
  69. try {
  70. const page = await this.pdfDoc.getPage(num)
  71. const canvas = this.$refs.pdfCanvas
  72. const ctx = canvas.getContext('2d')
  73. const viewport = page.getViewport({ scale:1 })
  74. canvas.height = viewport.height
  75. canvas.width = viewport.width
  76. await page.render({
  77. canvasContext: ctx,
  78. viewport: viewport
  79. }).promise
  80. } catch (error) {
  81. this.$message.error('渲染 PDF 页面失败: ' + error.message)
  82. }
  83. },
  84. prevPage() {
  85. if (this.pageNum <= 1) return
  86. this.pageNum--
  87. this.renderPage(this.pageNum)
  88. },
  89. nextPage() {
  90. if (this.pageNum >= this.pageCount) return
  91. this.pageNum++
  92. this.renderPage(this.pageNum)
  93. }
  94. }
  95. }
  96. </script>
  97. <style scoped>
  98. .pdf-viewer {
  99. display: flex;
  100. flex-direction: column;
  101. height: 100vh; /* 或者固定高度如 800px */
  102. text-align: center;
  103. }
  104. .pdf-content {
  105. flex: 1;
  106. overflow: auto; /* 内容超出时显示滚动条 */
  107. border: 1px solid #ddd;
  108. margin-bottom: 10px;
  109. }
  110. .pagination {
  111. display: flex;
  112. justify-content: center;
  113. align-items: center;
  114. gap: 20px;
  115. padding: 10px;
  116. background: #f5f5f5;
  117. position: sticky;
  118. bottom: 0;
  119. }
  120. </style>