SpringBoot集成OpenOffice将文档转换为PDF

1. 官网下载OpenOffice

下载地址

2. 引入依赖包:
        <!-- jodconverter -->
        <dependency>
            <groupId>org.jodconverter</groupId>
            <artifactId>jodconverter-core</artifactId>
            <version>4.2.2</version>
        </dependency>
        <dependency>
            <groupId>org.jodconverter</groupId>
            <artifactId>jodconverter-local</artifactId>
            <version>4.2.2</version>
        </dependency>
        <dependency>
            <groupId>org.jodconverter</groupId>
            <artifactId>jodconverter-spring-boot-starter</artifactId>
            <version>4.2.2</version>
        </dependency>
3. 在Spring Boot项目的配置文件中添加如下配置:
#jodconverter
jodconverter:
  local:
    enabled: true
    office-home: C:\Program Files (x86)\OpenOffice 4
    max-tasks-per-process: 10
    port-numbers: 8100

参数说明:

参数描述
office-home设置LibreOffice主目录,即OpenOffice本地安装路径
max-tasks-per-process进程重启前的最大进程数
port-numbers开启多个LibreOffice进程,每个端口对应一个进程
4. 格式转换代码:
    //上传课件,不是pdf转pdf
    public UploadFileResult uploadCourseWare(MultipartFile multipartFile) {
        try {
            if (multipartFile == null){
                ExceptionCast.cast(FileSystemCode.FS_UPLOADFILE_FILEISNULL);
            }
            //判断上传文件是否为PDF文件
            String originalFilename = multipartFile.getOriginalFilename();
            //拿到文件后缀
            String suffix = originalFilename.substring(originalFilename.lastIndexOf(".")+1);
            //拿到文件前缀
            String presuffix = originalFilename.substring(0,originalFilename.lastIndexOf("."));
            String fileId =null;
            //生成随机名称
            String saveFileName = UUID.randomUUID().toString().replace("-", "")+".pdf";
            FileInputStream inputStream = (FileInputStream) multipartFile.getInputStream();
            if (suffix.equals("doc") || suffix.equals("pptx") ||suffix.equals("docx")){
                File file = null;
                InputStream ins = null;
                ins = multipartFile.getInputStream();
                file = new File(multipartFile.getOriginalFilename());
                inputStreamToFile(ins,file);
                ins.close();
                //文件转化
                String newName = presuffix+".pdf";
                String pathName = "F:\\dev\\test2\\"+newName;
                converter.convert(file).to(new File(pathName)).execute();
                FileInputStream fileInputStream = new FileInputStream(new File(pathName));
                //将转换后的文件上传到七牛云存储服务器
                fileId = QiNiuUtil.upload2Qiniu(fileInputStream,saveFileName);
            }else if (suffix.equals("pdf")){
                fileId = QiNiuUtil.upload2Qiniu(inputStream, saveFileName);
            }else {
                return new UploadFileResult(FileSystemCode.FS_UPLOADFILE_NOSUPPORT,null);
            }
    }

示例代码中将 浏览器上传的.doc,.pptx,.docx格式的文件转换为pdf文件,转换成功后会保存在本地F盘的test2文件下。将保存后的pdf文件上传到七牛云存储服务器的代码在我的这篇文章下有详细说明:

Spring Boot上传文件到七牛云存储

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值