当前位置: 首页 > news >正文

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

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

request请求为:

//使用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界面

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界面,不会自动下载

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页面,不会自动下载,不会跳出打印窗口

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博客

相关文章:

  • CouchBase稀疏索引和全量索引的对比
  • Leetcode 3504. Longest Palindrome After Substring Concatenation II
  • react+antd封装一个可回车自定义option的select并且与某些内容相互禁用
  • ffmpeg中格式转换需要注意点总结
  • 设计模式基础入门
  • Android 使用CameraX实现预览、拍照、录制视频(Java版)
  • AI产品的基础设施:算法、数据与大语言模型
  • 解决安卓手机WebView无法直接预览PDF的问题(使用PDF.js方案)
  • 基于SpringBoot的仓库管理系统-023
  • 递归详解:从原理到实战
  • 9.常用模板引擎
  • Elasticsearch索引的字段映射
  • 串口中断接收与环形缓冲实例(apollo3 blue plus)
  • Perl 哈希
  • Springboot实战:如何用Docker和Kubernetes部署微服务
  • Unity中MonoBehaviour组件禁用时协程的行为
  • Qt基本框架(1)
  • 正则表达式和通配符
  • Python 中的 `partial`:函数参数预设的艺术
  • unity UI管理器
  • 建设银行南昌分行引金融“活水”,精准灌溉乡村沃土
  • 中国武术协会原主席张耀庭逝世,曾促成电影《少林寺》拍摄
  • 软硬件企业集中发布未成年人模式使用手册
  • 证据公布!菲律宾6人非法登上铁线礁活动
  • 对话|贝聿铭设计的不只是建筑,更是生活空间
  • 中介在网上非法贩婴“一个孩子8.5万元”?丹阳警方介入