pdfPreview.vue 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <template>
  2. <view>
  3. <uni-popup ref="popupRef" background-color="#fff" :animation="true" :is-mask-click="false" :mask-click="false"
  4. class="ht-preview-page">
  5. <view class="icon-title-navBar-box border-navBar-box">
  6. <view @click="goUpPage" class="nav-bar-icon"></view>
  7. <text class="nav-bar-title">合同预览</text>
  8. </view>
  9. <view class="pdf-box">
  10. <img v-for="item in imgList" mode="aspectFit" :src="`data:image/png;base64,${item}`"
  11. @click="previewBase64Image(`data:image/png;base64,${item}`)" class="pdf-img" />
  12. </view>
  13. </uni-popup>
  14. </view>
  15. </template>
  16. <script setup>
  17. import {
  18. base64ToPath
  19. } from "image-tools";
  20. import {
  21. ref
  22. } from "vue";
  23. const imgList = ref([])
  24. const popupRef = ref(null);
  25. function showPDF(list) {
  26. popupRef.value.open('bottom');
  27. imgList.value = list;
  28. }
  29. function goUpPage() {
  30. popupRef.value.close()
  31. }
  32. async function previewBase64Image(base64Data) {
  33. try {
  34. // 关键步骤:将 Base64 字符串转换为本地临时路径
  35. // 此处以使用 image-tools 的 base64ToPath 为例
  36. const localPath = await base64ToPath(base64Data);
  37. // 调用 UniApp 的图片预览 API
  38. uni.previewImage({
  39. urls: [localPath], // 注意:urls 参数需要是数组,即使只预览一张图
  40. current: 0, // 当前显示图片在 urls 数组中的索引
  41. });
  42. } catch (error) {
  43. // 如果出现错误(如转换失败),隐藏加载提示并告知用户
  44. console.error('预览失败:', error);
  45. uni.showToast({
  46. title: '预览失败',
  47. icon: 'none'
  48. });
  49. }
  50. }
  51. defineExpose({
  52. showPDF
  53. })
  54. </script>
  55. <style>
  56. </style>