<!-- <template>
  <div class="pdf-preview">
        <div class="pdf-wrap">
          <vue-pdf-embed :source="state.source" :style="scale" class="vue-pdf-embed" :page="state.pageNum" />
        </div>
        <div class="page-tool">
          <div class="page-tool-item" @click="prePage">上一页</div>
          <div class="page-tool-item" @click="nextPage">下一页</div>
          <div class="page-tool-item">{{ state.pageNum }}/{{ state.numPages }}</div>
          <div class="page-tool-item" @click="pageZoomOut">放大</div>
          <div class="page-tool-item" @click="pageZoomIn">缩小</div>
          <el-input type="number" v-model="pdf_page"></el-input>
          <el-button type="primary" @click="SetPdfPage" plain>页面跳转</el-button>
        </div>
      </div>
</template>

<script lang="ts" setup>
import { reactive, ref, toRefs, computed } from 'vue'
import { onMounted } from "vue";
import VuePdfEmbed from "vue-pdf-embed";
import { createLoadingTask } from "vue3-pdfjs/esm"; // 获得总页数

let loadingTask: any;

const pdf_page = ref(1);

const state = reactive({
  source: "", //预览pdf文件地址
  pageNum: 1, //当前页面
  scale: 1, // 缩放比例
  numPages: 0, // 总页数
});

const scale = computed(() => `transform:scale(${state.scale})`)

function getPdfImage(index: number) {
  // console.log(index, state.pageNum)
  loadingTask.promise.then((pdf: any) => {
    state.numPages = pdf.numPages;
    pdf.getPage(index).then((page: any) => {
      const viewport = page.getViewport({ scale: 1 })
      canvas.value.height = viewport.height;
      canvas.value.width = viewport.width;
      // 画布的dom大小, 设置移动端,宽度设置铺满整个屏幕
      // const clientWidth = document.body.clientWidth;
      const destWidth = 398;
      canvas.value.style.width = destWidth + 'px';
      // 根据pdf每页的宽高比例设置canvas的高度
      canvas.value.style.height = destWidth * (viewport.height / viewport.width) + 'px';
      const ctx = canvas.value.getContext('2d');
      page.render({
        canvasContext: ctx,
        viewport,
      });
      canvas.value.toBlob(function (blob) {
        pdf_img.value = dataURLtoBlob(canvas.value.toDataURL())
        // console.log(pdf_img.value);
        console.log(canvas.value.toDataURL(), state.pageNum)
      });
    })
  });
}

function SetPdfPage() {
  if (pdf_page.value >= 1 && pdf_page.value <= state.numPages) {
    console.log(pdf_page.value)
    state.pageNum = Number(pdf_page.value);
    getPdfImage(state.pageNum);
  } else {
    console.log(pdf_page.value)
    alert('输入的pdf页面无效！')
  }
}

function prePage() {
  if (state.pageNum > 1) {
    state.pageNum -= 1;
    getPdfImage(state.pageNum);
  }
}

function nextPage() {
  if (state.pageNum < state.numPages) {
    state.pageNum += 1;
    getPdfImage(state.pageNum);
  }
}
function pageZoomOut() {
  if (state.scale < 2) {
    state.scale += 0.1;
  }
}
function pageZoomIn() {
  if (state.scale > 1) {
    state.scale -= 0.1;
  }
}

function dataURLtoBlob(dataURL: any) {
  var arr = dataURL.split(','),
      mime = arr[0].match(/:(.*?);/)[1],
      bstr = atob(arr[1]),
      n = bstr.length,
      u8arr = new Uint8Array(n);
  while (n--) {
    u8arr[n] = bstr.charCodeAt(n);
  }
  return new Blob([u8arr], {type:mime});
}

async function convertCanvasToFile(canvas: HTMLCanvasElement, fileName: any) {
  // 将 Canvas 转为 Blob 对象
  const blob = await new Promise(resolve => canvas.toBlob(blob => {
            resolve(blob);
        }, pdf_img.value.type, 1.0));
  // 手动构造 File 对象
  let file = null;
  try {
    file = new File([pdf_img.value], fileName, { type: pdf_img.value.type });
  } catch (e) {
    // Safari 浏览器不支持直接通过 new File() 创建文件对象，需要手动构造
    const rawFile = blobToFile(blob, fileName);
    file = Object.assign(rawFile, { lastModifiedDate: new Date(), name: fileName });
  }
  return file;
}

</script>

<style scoped>
.pdf-preview {
  position: relative;
  height: 100vh;
  padding: 20px 0;
  box-sizing: border-box;
  background: rgb(66, 66, 66);
}

.vue-pdf-embed {
  text-align: center;
  width: 515px;
  border: 1px solid #e5e5e5;
  margin: 0 auto;
  box-sizing: border-box;
}

.pdf-preview {
  position: relative;
  height: 100vh;
  padding: 20px 0;
  box-sizing: border-box;
  background-color: e9e9e9;
}

.pdf-wrap {
  overflow-y: auto;
}

.vue-pdf-embed {
  text-align: center;
  width: 515px;
  border: 1px solid #e5e5e5;
  margin: 0 auto;
  box-sizing: border-box;
}

.page-tool {
  position: absolute;
  /* bottom: 35px; */
  /* padding-left: 15px; */
  /* padding-right: 15px; */
  display: flex;
  align-items: center;
  background: rgb(66, 66, 66);
  color: white;
  border-radius: 19px;
  z-index: 100;
  cursor: pointer;
  margin-left: 50%;
  transform: translateX(-50%);
}

.page-tool-item {
  padding: 8px 15px;
  padding-left: 10px;
  cursor: pointer;
}
</style> -->