pdfPreview.vue 1.6 KB

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