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

docx文档转为pdf文件响应前端

1、转换文件(docx~pdf)

1.引入pom依赖

<dependency>
  	<groupId>com.aspose</groupId>
  	<artifactId>aspose-words</artifactId>
  	<version>20.12.0</version>
</dependency>

2.读取docx文档数据-转换

// 初始化pdf文件
String pdfPath = "testConvert.pdf";
// docx文件名
String fileName = "test.docx";

// 1. 加载 Word 文件
Document doc = new Document(fileName);
doc.save(pdfPath, SaveFormat.PDF);

3.读取pdf文档数据-响应

FIle pdfFile // 转换后的文件
FileInputStream fis = new FileInputStream(pdfFile);

// 设置响应头或类型
response.setCharacterEncoding("UTF-8");
response.setContentType("application/pdf; charset=UTF-8");
// 设置文件名
String disposition = "attachment; filename=" + URLEncoder.encode("fileName", "UTF-8");
response.setHeader("Content-Disposition", disposition);
response.setContentLength((int) pdfFile.length());
// try-with-resources 读取写入流
try (FileInputStream fileInputStream = new FileInputStream(pdfFile);
    OutputStream outputStream = response.getOutputStream()) {
    byte[] buffer = new byte[4096];
    int bytesRead;
    // 将文件流写入响应输出流
    while ((bytesRead = fileInputStream.read(buffer)) != -1) {
        outputStream.write(buffer, 0, bytesRead);
    }
}
// 可将临时生成的文件删除 .delete()

相关文章:

  • 01-算法打卡-数组-二分查找-leetcode(704)-第一天
  • 两大奇妙的波-机械波-电磁波
  • 3D打印革新制造范式:CASAIM 3D打印解决方案
  • redis的基本使用
  • 大模型day1 - 什么是GPT
  • freecad内部python来源 + pip install 装包
  • 应用安全系列之四十五:日志伪造(Log_Forging)之三
  • DeepSeek实战:如何用AI工具提升销售转化率?
  • newspaper公共库获取每个 URL 对应的新闻内容,并将提取的新闻正文保存到一个文件中
  • 数字集成电路中时延不可综合与时间单位介绍
  • 用实体识别模型提取每一条事实性句子的关键词(实体),并保存到 JSON 文件中
  • JVM不同环境不同参数配置文件覆盖
  • C++中作用域(public,private,protected
  • CSS 过渡与变形:让交互更丝滑
  • STM32中Hz和时间的转换
  • context上下文(一)
  • C#里使用MaterialDesign来构造自定义窗口
  • 希哈表的学习
  • 9-函数的定义及用法
  • ASP.NET Core 性能优化:服务器端响应缓存
  • 阿里云网站主体变更怎么做/小说风云榜
  • 网站运营与管理的对策/软文写作是什么
  • 唐山建设网站制作/大数据推广公司
  • 昆明优秀网站/seo整站优化
  • 给别人做网站挣钱么/磁力宅
  • 给别人做网站赚钱吗/2020最近的新闻大事10条