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

java springboot word文档转pdf

java springboot word文档转pdf

  • 1、环境
  • 2、依赖
  • 3、代码

1、环境

1、java、springboot
2、maven或者gradle
3、办公软件(自己电脑上的wps或者office等,如果部署到服务器上也要安装,linux、Mac 都有,自己安装)
可能会遇到的问题:
java springboot maven项目,当然gradle 也可以,特别注意的是这个需要依赖外部软件,就是我们熟知的wps或者office 都可以,应该说是有办公软件且办公软件有word转pdf的功能,这个它应该是获取到我们电脑本地的环境来转的,如果有时候不行的话可以考虑一下是不是自己的办公软件的问题,我之前就遇到这个问题,我本地已经安装wps的,也可以正常使用,但是代码里面就是不行,一直报流已经关闭,意思就是获取不到我本地相关的流。最后我重新安装一下我的办公软件(wps)就好了,还有一种就可能版本的问题,也一定要注意

2、依赖

<dependency><groupId>com.documents4j</groupId><artifactId>documents4j-local</artifactId><version>1.0.3</version></dependency><dependency><groupId>com.documents4j</groupId><artifactId>documents4j-transformer-msoffice-word</artifactId><version>1.0.3</version></dependency>

这是maven的依赖,其他的自己找

3、代码

package org.jeecg.modules.xunshu.utils;import com.documents4j.api.DocumentType;
import com.documents4j.api.IConverter;
import com.documents4j.job.LocalConverter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;import java.io.*;/*** @author weisn*/
@Slf4j
@Component
public class WordConvertPdfUtil {private  static final IConverter CONVERTER = LocalConverter.builder().build();/*** 通过documents4j 实现word转pdf** @param sourcePath 源文件地址 如 /root/example.doc* @param targetPath 目标文件地址 如 /root/example.pdf*/public static void documents4jWordToPdf(String sourcePath, String targetPath) throws IOException {InputStream docxInputStream = null;OutputStream outputStream = null;try {// 原word地址docxInputStream = new FileInputStream(sourcePath);// 转换后pdf生成地址outputStream = new FileOutputStream(targetPath);IConverter converter = LocalConverter.builder().build();converter.convert(docxInputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute();// 关闭converter.shutDown();// 关闭outputStream.close();// 关闭docxInputStream.close();} catch (Exception e) {System.out.println("[documents4J] word转pdf失败:" + e.toString());} finally {if (outputStream != null) {outputStream.close();}if (docxInputStream != null) {docxInputStream.close();}}}}
http://www.lryc.cn/news/124769.html

相关文章:

  • 【Leetcode Sheet】Weekly Practice 2
  • 【BERTopic应用 03/3】:微调参数
  • 2023年上半年数学建模竞赛题目汇总与难度分析
  • Linux下搭建java环境
  • String、StringBuffer、StringBuilder三者的异同?
  • htmlCSS-----弹性布局案例展示
  • Fiddler模拟请求发送和修改响应数据
  • RH850从0搭建Autosar开发环境【23】- Davinci Configurator之DCM实操实现DID的读取写入
  • ChatGPT收录
  • Nginx随笔
  • 61. 旋转链表
  • Python实现动态调用Matlab自定义函数
  • redis集群和分片-Redis Cluster:分布式环境中的数据分片、主从复制和 Sentinel 哨兵
  • 【数据库基础】Mysql下载安装及配置
  • iptables安全与防火墙
  • Linux 内核线程启动以及内核调用应用层程序
  • React+Typescript清理项目环境
  • 【linux学习】linux的模块机制
  • 用 oneAPI 实现 AI 欺诈检测:一款智能图像识别工具
  • 云计算的发展前景怎么样
  • opencv实战项目 手势识别-手势音量控制(opencv)
  • Mac下编译32位Qt
  • 企业数据库遭到360后缀勒索病毒攻击,360勒索病毒解密
  • C++11时间日期库chrono的使用
  • 每天一道leetcode:1466. 重新规划路线(图论中等广度优先遍历)
  • Mysql—修改用户密码(重置密码)
  • ECE585 Tomasulo算法:C++ Tomasulo算法模拟器
  • Qt中在QLabel上画点,重写QLabel类
  • ssm+vue小型企业办公自动化系统源码和论文PPT
  • C++ STL priority_queue