前端处理pdf文件流,展示pdf

**场景:**后端传pdf文件流,前端在页面中展示pdf

request请求为:

javascript 复制代码
//使用get请求
export async function getRequest(INSTSN, REGISTESN) {
  const url = '/api/getPdf?INSTSN=' + INSTSN + '&REGISTESN=' + REGISTESN
  const method = 'get'
  const headers = { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' }
  const responseType = 'arraybuffer'
  const response = await service.request({
    url,
    method,
    headers,
    responseType,
  })
  return response;
}

调用请求,处理返回的数据显示pdf

自动下载pdf,打开打印界面,跳转到pdf界面

javascript 复制代码
async getReport() {
        //得到需要的参数
      const INSTSN = INSTSN;
      const REGISTESN = REGISTESN;
      try {
        //通过get请求,从后端返回得到pdf的数据
        const response = await getReport(INSTSN, REGISTESN);
        const binaryData = [response];
        const blob = new Blob(binaryData, { type: 'application/pdf' });
        const pdfUrl = URL.createObjectURL(blob);
        if (pdfUrl) {
          this.handlePrint(pdfUrl);
        }
      } catch (error) {
        console.error('加载 PDF 失败:', error);
        alert('无法打开 PDF,请检查控制台!');
      }
    },
    //处理生成的url,打开pdf
    handlePrint(pdfUrl) {
       // 创建一个隐藏的 a 标签用于下载 PDF
       const a = document.createElement('a');
       a.href = pdfUrl;
       a.download = 'report.pdf';
       a.style.display = 'none';
       document.body.appendChild(a);
      // // 触发下载
       a.click();
       // 移除 a 标签
       document.body.removeChild(a);
      // 打开 PDF 并直接调用打印功能
      const newWindow = window.open(pdfUrl);
       if (newWindow) {
         newWindow.onload = function () {
           newWindow.print();
         };
       }
      // 释放之前创建的临时 URL
      URL.revokeObjectURL(pdfUrl);
    }

打开打印界面,转到pdf界面,不会自动下载

javascript 复制代码
async getReport() {
        //得到需要的参数
      const INSTSN = INSTSN;
      const REGISTESN = REGISTESN;
      try {
        //通过get请求,从后端返回得到pdf的数据
        const response = await getReport(INSTSN, REGISTESN);
        const binaryData = [response];
        const blob = new Blob(binaryData, { type: 'application/pdf' });
        const pdfUrl = URL.createObjectURL(blob);
        if (pdfUrl) {
          this.handlePrint(pdfUrl);
        }
      } catch (error) {
        console.error('加载 PDF 失败:', error);
        alert('无法打开 PDF,请检查控制台!');
      }
    },
    //处理生成的url,打开pdf
    handlePrint(pdfUrl) {
       // 创建一个隐藏的 a 标签用于下载 PDF
       //const a = document.createElement('a');
       //a.href = pdfUrl;
       //a.download = 'report.pdf';
       //a.style.display = 'none';
       //document.body.appendChild(a);
      // // 触发下载
       //a.click();
       // 移除 a 标签
       //document.body.removeChild(a);
      // 打开 PDF 并直接调用打印功能
      const newWindow = window.open(pdfUrl);
       if (newWindow) {
         newWindow.onload = function () {
           newWindow.print();
         };
       }
      // 释放之前创建的临时 URL
      URL.revokeObjectURL(pdfUrl);
    }

转到pdf页面,不会自动下载,不会跳出打印窗口

javascript 复制代码
async getReport() {
        //得到需要的参数
      const INSTSN = INSTSN;
      const REGISTESN = REGISTESN;
      try {
        //通过get请求,从后端返回得到pdf的数据
        const response = await getReport(INSTSN, REGISTESN);
        const binaryData = [response];
        const blob = new Blob(binaryData, { type: 'application/pdf' });
        const pdfUrl = URL.createObjectURL(blob);
        if (pdfUrl) {
          this.handlePrint(pdfUrl);
        }
      } catch (error) {
        console.error('加载 PDF 失败:', error);
        alert('无法打开 PDF,请检查控制台!');
      }
    },
    //处理生成的url,打开pdf
    handlePrint(pdfUrl) {
       // 创建一个隐藏的 a 标签用于下载 PDF
       //const a = document.createElement('a');
       //a.href = pdfUrl;
       //a.download = 'report.pdf';
       //a.style.display = 'none';
       //document.body.appendChild(a);
      // // 触发下载
       //a.click();
       // 移除 a 标签
       //document.body.removeChild(a);
      // 打开 PDF 并直接调用打印功能
      const newWindow = window.open(pdfUrl);
       //if (newWindow) {
         //newWindow.onload = function () {
           //newWindow.print();
         //};
      // }
      // 释放之前创建的临时 URL
      URL.revokeObjectURL(pdfUrl);
    }

原文链接:vue接收后端传来的pdf文件流,前端调用预览PDF_vue如何显示fastreport-CSDN博客

相关推荐
枫,为落叶1 分钟前
Axios使用教程(一)
前端
小章鱼学前端6 分钟前
2025 年最新 Fabric.js 实战:一个完整可上线的图片选区标注组件(含全部源码).
前端·vue.js
ohyeah8 分钟前
JavaScript 词法作用域、作用域链与闭包:从代码看机制
前端·javascript
流星稍逝10 分钟前
手搓一个简简单单进度条
前端
倚栏听风雨32 分钟前
详解 TypeScript 中,async 和 await
前端
小皮虾40 分钟前
告别服务器!小程序纯前端“图片转 PDF”工具,隐私安全又高效
前端·javascript·微信小程序
ohyeah41 分钟前
我的变量去哪了?JS 作用域入门指南
前端·javascript
灼华_43 分钟前
Vue 3 + Vite + Router + Pinia + Element Plus + Monorepo + qiankun 构建企业级中后台前端框架
前端
倚栏听风雨44 分钟前
TypeScript 中,Promise
前端
影i1 小时前
Vue 3 踩坑实录:如何优雅地把“上古”第三方插件关进 Iframe 小黑屋
前端