SpringBoot使用OpenOffice将word转成pdf

安装

参考这篇文章:Linux环境下安装OpenOffice 4.1.8

引用依赖

  • 这边 jodconverter 用的是 2.2.2 版本,Maven库没有,需要自己去网上下载
  • 我这边是引用本地JAR,这是因为产生了大量WARN日志(找不到JAR包之类的)。虽然不影响使用,但是看着不舒服,所以把相关JAR放到同一个目录下引用了。
        <!--jodconverter 开始 -->
        <dependency>
            <groupId>com.artofsolving
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在Spring Boot中使用OpenOfficeWord转换为PDF可以通过以下步骤实现: 1. 首先,确保已经安装了OpenOffice软件,并且OpenOffice服务已经启动。 2. 在Spring Boot项目的pom.xml文件中添加对OpenOffice JODConverter的依赖: ```xml <dependency> <groupId>org.jodconverter</groupId> <artifactId>jodconverter-spring-boot-starter</artifactId> <version>4.2.2</version> </dependency> ``` 3. 创建一个Controller类,用于接收Word文件并将其转换为PDF。可以使用`JodConverter`类来实现转换功能。以下是一个简单的示例: ```java import org.jodconverter.DocumentConverter; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; @RestController public class ConvertController { @Autowired private DocumentConverter documentConverter; @PostMapping("/convert") public String convertToPdf(@RequestParam("file") MultipartFile file) { try { // 将上传的Word文件转换为PDF File inputFile = File.createTempFile("input", ".docx"); file.transferTo(inputFile); File outputFile = File.createTempFile("output", ".pdf"); documentConverter.convert(inputFile).to(outputFile).execute(); // 返回转换后的PDF文件路径 return outputFile.getAbsolutePath(); } catch (Exception e) { e.printStackTrace(); return "转换失败"; } } } ``` 4. 运行Spring Boot应用程序,并使用POST请求将Word文件上传到`/convert`接口。接口将返回转换后的PDF文件路径。 这样,你就可以使用Spring Boot和OpenOfficeWord文件转换为PDF了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

°Fuhb

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值