SpringBoot使用LibreOffice--office转pdf

软件下载地址:https://www.libreoffice.org/download/download/ 

pom.xml依赖:

        <!-- liboffice -->
        <dependency>
            <groupId>org.jodconverter</groupId>
            <artifactId>jodconverter-core</artifactId>
            <version>4.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.jodconverter</groupId>
            <artifactId>jodconverter-local</artifactId>
            <version>4.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.jodconverter</groupId>
            <artifactId>jodconverter-online</artifactId>
            <version>4.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.jodconverter</groupId>
            <artifactId>jodconverter-spring-boot-starter</artifactId>
            <version>4.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.libreoffice</groupId>
            <artifactId>ridl</artifactId>
            <version>5.4.2</version>
        </dependency>

yml配置文件:

jodconverter:
  #在线方式(docker部署libreoffice)
  online:
    enabled: true
    #服务地址
    url:  http://localhost:9980/lool/convert-to/pdf
    pool-size:  10
  #本地方式:linux和windows均可,注意配置路径
  local:
    enabled: true
    #安装路径
    office-home: C:\Program Files\LibreOffice
    #多实例
    portNumbers: 8100,8101,8102
    #最大处理数量
    maxTasksPerProcess: 100

转换代码:

@Component
public class LibOfficeUtils {

    private static final Logger logger = LoggerFactory.getLogger(LibOfficeUtils.class);

    //DocumentConvert是个接口,以下两个bean是其实现,分别对应在线和本地转换
    @Autowired
    private OnlineConverter onlineDocumentConverter;
    @Autowired
    private LocalConverter localConverter;

    /**
     * @Author xd
     * @Description office转pdf
     * @Date 2021/1/31 15:54
     * @Param [fromPath--源文件, toPath--目标文件]
     * @return void
     **/
    public void convert(String fromPath,String toPath){
        File from = new File(fromPath);
        File to = new File(toPath);
        try {
            //如果在线转换失败
            onlineDocumentConverter.convert(from).to(to).execute();
        }catch (Exception e){
            e.printStackTrace();
            try {
                //使用本地方式转换
                localConverter.convert(from).to(to).execute();
            } catch (OfficeException officeException) {
                logger.info("文件转换失败,请检查");
                officeException.printStackTrace();
            }
        }
    }
}

针对评论区:调用/lool/convert-to/pdf接口提示failed to respond错误,没记错的话应该是源文件路径中有中文存在导致的。我们可以采用以下方式转换:

在jar包中有个json文件是关于文件后缀和媒体类型的对应关系:

//根据文件类型获取对应的DocumentFormat,括号里的参数是源文件的mediaType
DocumentFormat docx = onlineDocumentConverter.getFormatRegistry().getFormatByMediaType("application/pdf");
//这是另一种转换方法,这种转换方式为了解决在线转换源文件有中文路径的问题
 onlineDocumentConverter.convert(new FileInputStream(from)).as(docx).to(to).execute();

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 15
    评论
下面是一个示例代码,演示如何在Spring Boot中使用LibreOffice来实现文档换: 首先,需要在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>com.artofsolving</groupId> <artifactId>jodconverter-core</artifactId> <version>3.0-beta-4</version> </dependency> ``` 然后,在Java类中注入LibreOffice相关的配置: ```java import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class LibreOfficeConfig { @Value("${libreoffice.home:}") private String officeHome; @Bean public LibreOfficeConverter libreOfficeConverter() { return new LibreOfficeConverter(officeHome); } } ``` 在上述代码中,我们使用了@Value注解来获取配置文件中的"libreoffice.home"属性值,并将其注入到officeHome字段中。然后,我们创建了一个名为"libreOfficeConverter"的Bean,用于将文档换为PDF格式。 最后,我们可以在Controller中使用此Bean来实现文档换: ```java 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 java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; @RestController public class DocumentController { @Autowired private LibreOfficeConverter libreOfficeConverter; @PostMapping("/convert") public String convertDocument(@RequestParam("src") String srcPath, @RequestParam("dest") String destPath) throws IOException { File inputFile = new File(srcPath); File outputFile = new File(destPath); FileInputStream inputStream = new FileInputStream(inputFile); FileOutputStream outputStream = new FileOutputStream(outputFile); libreOfficeConverter.convert(inputStream, outputFile); inputStream.close(); outputStream.close(); return "Document converted successfully!"; } } ``` 在上述代码中,我们注入了之前创建的"libreOfficeConverter" Bean,并在convertDocument方法中使用它来将文档从输入流换为PDF格式。最后,我们关闭了输入流和输出流,并返回一个成功换的提示消息。 需要注意的是,上述代码仅供参考,具体实现可能因环境和需求而异。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

dong__xue

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

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

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

打赏作者

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

抵扣说明:

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

余额充值