vue3室友iframe实现pdf弹窗预览

效果:

代码:

弹窗

    <div class="viewBox">
      <a-modal
        class="modalBox"
        v-model:visible="visible"
        :title="title"
        @ok="handleOk"
        :bodyStyle="bodyStyle"
        :width="1300"
        :maskClosable="false"
        :destroyOnClose="true"
        :footer="null"
        @cancel="handleCancel"
        :maskStyle="{}"
        :dialogStyle="{}"
      >
        <vue-office-docx :src="docSrc" v-if="docSrc" />

        <div v-if="pdfSrc" style="width: 100%; height: 100%">
          <iframe :src="pdfUrl" style="width: 100%; height: 100%"></iframe>
        </div>

        <!-- <vue-office-pdf :src="pdfSrc" v-if="pdfSrc" /> -->
      </a-modal>
    </div>
let docSrc = ref("");
let pdfSrc = ref("");
const bodyStyle = {
  height: `700px`,
  // marginTop: '-40px'
  // padding: '0 24px'
};
let pdfUrl = ref("");
const preview = (record) => {
  visible.value = true;
  title.value = "预览";
  if (isPdf(record.FilePath[0].DisPlayName)) {
    return new Promise((resolve, reject) => {
      let blob = null;
      let xhr = new XMLHttpRequest();
      xhr.open(
        "GET",
        window.defaultconfig.fileUrl +
          "/api/FileManage/Download" +
          `?Id=${record.FilePath[0].FileId}`
      );
      xhr.responseType = "blob";
      xhr.onload = () => {
        blob = xhr.response;
        const blobob = new Blob([blob], {
          type: "application/pdf;charset=utf-8",
        });
        const href = window.URL.createObjectURL(blobob);
        pdfUrl.value = href;
        pdfSrc.value = "true";
        // window.open(href);
        // resolve(file);
      };
      xhr.onerror = (e) => {
        reject(e);
      };
      xhr.send();
    });
  }
  if (isDoc(record.FilePath[0].DisPlayName)) {
    axios
      .get(
        window.defaultconfig.fileUrl +
          "/api/FileManage/Download" +
          `?Id=${record.FilePath[0].FileId}`,
        { responseType: "arraybuffer" }
      )
      .then((res) => {
        const blob = new Blob([res.data], { type: "application/vnd.ms-excel" });
        let fileReader = new FileReader();
        fileReader.readAsArrayBuffer(blob);
        fileReader.onload = () => {
          docSrc.value = fileReader.result;
        };
      });
  }
};
function isPdf(file) {
  var fileExtension = file.split(".").pop().toLowerCase();
  return fileExtension === "pdf";
}
function isDoc(file) {
  var fileExtension = file.split(".").pop().toLowerCase();
  return fileExtension === "docx";
}
const handleOk = () => {
  visible.value = false;
  docSrc.value = "";
  pdfSrc.value = "";
};
const handleCancel = () => {
  visible.value = false;
  docSrc.value = "";
  pdfSrc.value = "";
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
实现这个功能,我们需要用到以下两个库: 1. `pdfjs-dist`:用于解析 PDF 文件并将其转换为可供浏览器渲染的 HTML。 2. `vue-pdf`:一个 Vue 组件,用于在页面上呈现 PDF 文件。 下面是具体的实现步骤: 1. 安装依赖: ``` npm install pdfjs-dist vue-pdf --save ``` 2. 在 Vue 组件中引入所需的库: ```javascript import pdfjsLib from 'pdfjs-dist/webpack'; import VuePdf from 'vue-pdf'; ``` 3. 定义一个方法,用于下载 PDF 文件: ```javascript downloadPdf() { const url = 'your_pdf_url'; axios.get(url, { responseType: 'blob' }).then(response => { const blob = new Blob([response.data], { type: 'application/pdf' }); const url = window.URL.createObjectURL(blob); const link = document.createElement('a'); link.href = url; link.download = 'your_pdf_name'; link.click(); }); } ``` 4. 在组件中使用 `vue-pdf` 组件: ```html <template> <div> <button @click="downloadPdf">下载 PDF</button> <vue-pdf :src="pdfSrc" :show-toolbar="true"></vue-pdf> </div> </template> <script> import pdfjsLib from 'pdfjs-dist/webpack'; import VuePdf from 'vue-pdf'; export default { components: { VuePdf }, data() { return { pdfSrc: '' }; }, methods: { downloadPdf() { // 下载 PDF 文件的代码 } }, mounted() { const url = 'your_pdf_url'; pdfjsLib.getDocument(url).promise.then(pdf => { this.pdfSrc = pdf; }); } }; </script> ``` 这样,当用户点击下载按钮时,会下载 PDF 文件并以弹窗的形式呈现在页面上。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值