pdfViewer.vue 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <template>
  2. <div class="pdf-wrap">
  3. <div class="actions">
  4. <button class="btn btn-sm btn-secondary" @click="handleScale(+0.1)">
  5. <i class="mdi mdi-magnify-plus-outline"/>
  6. 放大
  7. </button>
  8. <button class="btn btn-sm btn-secondary" @click="handleScale(-0.1)">
  9. <i class="mdi mdi-magnify-minus-outline">
  10. 缩小
  11. </i></button>
  12. </div>
  13. <div class="pdf-viewer-wrapper" v-dragscroll="true" :class="{zoomActive: scale > 1 }">
  14. <pdf
  15. v-if="render"
  16. ref="pdf"
  17. src="/static/工区/07延吉西运行工区-简洁版-模型.pdf"
  18. />
  19. <!-- <vue-pdf-embed ref="pdf" :scale="scale" source="/static/工区/07延吉西运行工区-简洁版-模型.pdf" />-->
  20. <!-- <vue-pdf-app ref="pdf" pdf="/static/7.pdf"></vue-pdf-app>-->
  21. </div>
  22. </div>
  23. </template>
  24. <script>
  25. import pdf from 'vue-pdf'
  26. import { dragscroll } from 'vue-dragscroll'
  27. import VuePdfEmbed from 'vue-pdf-embed/dist/vue2-pdf-embed.js'
  28. import VuePdfApp from 'vue-pdf-app'
  29. import 'vue-pdf-app/dist/icons/main.css'
  30. export default {
  31. name: 'PdfViewer',
  32. directives: { dragscroll },
  33. components: { pdf, VuePdfEmbed, VuePdfApp },
  34. data() {
  35. return {
  36. scale: 1,
  37. render: true,
  38. basicStyle: {
  39. width: 0,
  40. height: 0
  41. },
  42. toStyle: {
  43. width: 0,
  44. height: 0
  45. }
  46. }
  47. },
  48. created() {
  49. window['pdfIs'] = this
  50. },
  51. mounted() {
  52. this.basicStyle = {
  53. width: getComputedStyle(this.$refs.pdf.$el).width || 0,
  54. height: getComputedStyle(this.$refs.pdf.$el).height || 0
  55. }
  56. },
  57. methods: {
  58. handleScale(num) {
  59. this.scale += num
  60. if (this.scale < 0) {
  61. this.scale = 0
  62. }
  63. const style = this.basicStyle
  64. const w = parseFloat(style.width?.replace('px', ''))
  65. const h = parseFloat(style.height?.replace('px', ''))
  66. this.toStyle = {
  67. width: `${w * this.scale}px`,
  68. height: `${h * this.scale}px`
  69. }
  70. this.$refs.pdf.$el.style.width = this.toStyle.width
  71. this.$refs.pdf.$el.style.height = this.toStyle.height
  72. }
  73. }
  74. }
  75. </script>
  76. <style lang="scss">
  77. .pdf-wrap {
  78. width: 100%;
  79. height: 100%;
  80. overflow: hidden;
  81. .pdf-viewer-wrapper {
  82. width: 100%;
  83. height: 100%;
  84. flex-grow: 1;
  85. overflow: auto;
  86. &.zoomActive {
  87. cursor: grab;
  88. }
  89. }
  90. }
  91. </style>