SpringBoot使用Libreoffice Online进行文件预览

Libreoffice Online实现文件在线预览

1.Docker中安装配置Libreoffice Online

1.	拉取docker镜像: 
    a)	docker search libreoffice/online
    b)	docker pull libreoffice/online:master
2.	创建并启动LibreOffice Online 服务
    a)	docker run -e TZ="Asia/Shanghai" --restart always --name libreoffice -d -p 9980:9980 -e "username=root" -e "password=123456" -e "extra_params=--o:ssl.enable=false --o:net.post_allow.host[0]=.\{1,99\}" libreoffice/online:master修改容器中loolwsd配置文件
    b)	进去docker :    docker exec -it --user root id /bin/bash
    c)	禁用SSL 加密传输:(其默认是True,开启)
    修改配置文件 /etc/loolwsd/loolwsd.xml
    
3.	宿主机防火墙放行对应端口
    a)	firewall-cmd --add-port=9980/tcp –permanent
    b)	systemctl restart firewalld
	c)	查看放行的端口: firewall-cmd --zone=public --list-ports
4.	验证Libre Office Online 安装:
	a)	访问: http://IP:(docker映射出的端口) ,返回OK,即为安装成功;
	b)	直接访问:http://IP: (docker映射出的端口) /loleaflet/dist/admin/admin.html, 出现xml内容,证明软件安装成功
5.	Libreoffice容器内使用curl  http://IP: (docker映射出的端口) 连接成功即可

配置修改

2.Maven中添加

        <properties>
            <libreoffice.version>5.4.2</libreoffice.version>
        </properties>
        <dependency>
            <groupId>org.libreoffice</groupId>
            <artifactId>juh</artifactId>
            <version>${libreoffice.version}</version>
        </dependency>
        <dependency>
            <groupId>org.libreoffice</groupId>
            <artifactId>jurt</artifactId>
            <version>${libreoffice.version}</version>
        </dependency>
        <dependency>
            <groupId>org.libreoffice</groupId>
            <artifactId>ridl</artifactId>
            <version>${libreoffice.version}</version>
        </dependency>
        <dependency>
            <groupId>org.libreoffice</groupId>
            <artifactId>unoil</artifactId>
            <version>${libreoffice.version}</version>
        </dependency>
        <dependency>
            <groupId>org.jodconverter</groupId>
            <artifactId>jodconverter-online</artifactId>
            <version>4.2.4</version>
        </dependency>

3.业务处理

FileAttribute fileAttribute = new FileAttribute();
        File inputFile = new File(url);
        // 判断获取目标文件路径是否为空
        if (!inputFile.exists()){
            throw new BaseException("预览文件不存在");
        }
		//处理文件路径
        String url2OutFilePath = StringUtils.replaceOnce(url, "upload-dir", "temp-file");
        String outFilePathStr = StringUtils.substringBeforeLast(url2OutFilePath,".")+".pdf";
        fileAttribute.setUrl(outFilePathStr);
        File outputFilePath = new File(outFilePathStr);
		// 存在已经转换的文件则直接返回文件路径
        if (outputFilePath.exists()) {
            result.setResult(fileAttribute);
            return result;
        } else {
            //转换文件
            if (!outputFilePath.getParentFile().exists() && !outputFilePath.getParentFile().mkdirs()) {
                log.error("创建目录【{}】失败,请检查目录权限!",url2OutFilePath);
            }
            final OnlineOfficeManager onlineOfficeManager = OnlineOfficeManager.make(libreofficeServer);
            try {
                onlineOfficeManager.start();
                OnlineConverter.builder().officeManager(onlineOfficeManager).build()
                        .convert(inputFile).to(outputFilePath).execute();
                onlineOfficeManager.stop();
            }catch (Exception e){
                log.error("文件转换异常信息:"+e);
                throw new BaseException("文件转换失败,请下载后查看");
            }finally {
                if (onlineOfficeManager!=null){
                    try {
                        onlineOfficeManager.stop();
                    } catch (OfficeException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        result.setResult(fileAttribute);
        return result;
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个示例代码,演示如何在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格式。最后,我们关闭了输入流和输出流,并返回一个成功转换的提示消息。 需要注意的是,上述代码仅供参考,具体实现可能因环境和需求而异。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值