pdf.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <script setup lang="ts">
  2. import { ref } from "vue";
  3. import { useI18n } from "vue-i18n";
  4. import VuePdfEmbed from "vue-pdf-embed";
  5. defineOptions({
  6. name: "Pdf"
  7. });
  8. const { t } = useI18n();
  9. const pdfRef = ref<any>();
  10. const pageCount = ref(1);
  11. const loading = ref(true);
  12. const currentPage = ref(1);
  13. const currentRotation = ref(0);
  14. const showAllPages = ref(false);
  15. const rotations = [0, 90, 180, 270];
  16. const source =
  17. "https://xiaoxian521.github.io/hyperlink/pdf/Cookie%E5%92%8CSession%E5%8C%BA%E5%88%AB%E7%94%A8%E6%B3%95.pdf";
  18. const handleDocumentRender = () => {
  19. loading.value = false;
  20. pageCount.value = pdfRef.value.doc.numPages;
  21. };
  22. const showAllPagesChange = () => {
  23. currentPage.value = showAllPages.value ? null : 1;
  24. };
  25. const onPrint = () => {
  26. // 如果在打印时,打印页面是本身的两倍,在打印配置 页面 设置 仅限页码为奇数的页面 即可
  27. pdfRef.value.print();
  28. };
  29. </script>
  30. <template>
  31. <el-card shadow="never">
  32. <template #header>
  33. <div class="font-medium">
  34. <el-link
  35. href="https://github.com/hrynko/vue-pdf-embed"
  36. target="_blank"
  37. style="margin: 0 5px 4px 0; font-size: 16px"
  38. >
  39. PDF预览
  40. </el-link>
  41. </div>
  42. <el-link
  43. class="mt-2"
  44. href="https://github.com/pure-admin/vue-pure-admin/blob/main/src/views/able/pdf.vue"
  45. target="_blank"
  46. >
  47. 代码位置 src/views/able/pdf.vue
  48. </el-link>
  49. </template>
  50. <div
  51. v-loading="loading"
  52. class="h-[calc(100vh-295px)]"
  53. :element-loading-text="t('status.pureLoad')"
  54. >
  55. <div class="flex justify-between items-center h-9">
  56. <div v-if="showAllPages" class="font-medium ml-1.25 text-xl">
  57. 共{{ pageCount }}页
  58. </div>
  59. <div v-else>
  60. <el-pagination
  61. v-model:current-page="currentPage"
  62. background
  63. layout="prev, slot, next"
  64. :page-size="1"
  65. :total="pageCount"
  66. >
  67. {{ currentPage }} / {{ pageCount }}
  68. </el-pagination>
  69. </div>
  70. <div class="w-[170px] flex-bc">
  71. <el-checkbox v-model="showAllPages" @change="showAllPagesChange">
  72. 显示所有页面
  73. </el-checkbox>
  74. <IconifyIconOnline
  75. v-tippy="{
  76. maxWidth: 'none',
  77. content: `翻转(当前角度${rotations[currentRotation]}度)`
  78. }"
  79. icon="ic:baseline-rotate-90-degrees-ccw"
  80. class="cursor-pointer outline-transparent"
  81. @click="
  82. currentRotation === 3
  83. ? (currentRotation = 0)
  84. : (currentRotation += 1)
  85. "
  86. />
  87. <IconifyIconOnline
  88. v-tippy="{
  89. maxWidth: 'none',
  90. content: '打印'
  91. }"
  92. icon="ri:printer-line"
  93. class="cursor-pointer outline-transparent"
  94. @click="onPrint"
  95. />
  96. </div>
  97. </div>
  98. <el-scrollbar>
  99. <vue-pdf-embed
  100. ref="pdfRef"
  101. class="h-full container overflow-auto"
  102. :rotation="rotations[currentRotation]"
  103. :page="currentPage"
  104. :source="source"
  105. @rendered="handleDocumentRender"
  106. />
  107. </el-scrollbar>
  108. </div>
  109. </el-card>
  110. </template>