springboot高级功能(九)jodconverter实现在线预览

简介: springboot高级功能(九)jodconverter实现在线预览


思路:其他文档转成配pdf然后通过流发送到前台,前台支持pdf

1.jodconverter-spring-boot-starter方法

1.pom文件

        <!--jodconverter 核心包 -->
        <dependency>
            <groupId>org.jodconverter</groupId>
            <artifactId>jodconverter-core</artifactId>
            <version>4.2.2</version>
        </dependency>
        <!--springboot支持包,里面包括了自动配置类 -->
        <dependency>
            <groupId>org.jodconverter</groupId>
            <artifactId>jodconverter-spring-boot-starter</artifactId>
            <version>4.2.2</version>
        </dependency>
        <!--jodconverter 本地支持包 -->
        <dependency>
            <groupId>org.jodconverter</groupId>
            <artifactId>jodconverter-local</artifactId>
            <version>4.2.2</version>
        </dependency>

2.application.xml

jodconverter:
  local:
    enabled: true
    max-tasks-per-process: 10
    port-numbers: 8100

3.调用

@RestController
public class OnlinePreviewController {
    // 第一步:转换器直接注入
    @Autowired
    DocumentConverter documentConverter;
    @GetMapping("/toPdfFile")
    public String toPdfFile(FileMessage fileMessage) {
        // 获取HttpServletResponse
        HttpServletResponse response =
            ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getResponse();
        // 需要转换的文件
        File file = new File(fileMessage.getFileDownloadUri());
        try {
            // 转换之后文件生成的地址
            File newFile = new File("D:/common_files/pdf");
            if (!newFile.exists()) {
                newFile.mkdirs();
            }
            String converterPdf = "D:/common_files/pdf" + "/" + fileMessage.getFileName() + "-pdf.pdf";
            // 文件转化
            documentConverter.convert(file).to(new File(converterPdf)).execute();
            // 使用response,将pdf文件以流的方式发送的前段
            ServletOutputStream outputStream = response.getOutputStream();
            // 读取文件
            InputStream in = new FileInputStream(new File(converterPdf));
            // copy文件
            int i = IOUtils.copy(in, outputStream);
            System.out.println(i);
            in.close();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "This is to pdf";
    }
}

但是这种会发生excle过长穿行的情况;

2.使用com.artofsolving

1.pom文件

<dependency>
            <groupId>com.artofsolving</groupId>
            <artifactId>jodconverter</artifactId>
            <version>2.2.2</version>
        </dependency>
        <dependency>
            <groupId>org.openoffice</groupId>
            <artifactId>jurt</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.openoffice</groupId>
            <artifactId>ridl</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.openoffice</groupId>
            <artifactId>juh</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.openoffice</groupId>
            <artifactId>unoil</artifactId>
            <version>3.0.1</version>
        </dependency>

注意2.2.2是在maven服务器上没有 所以需要单独下载处理 直接上网盘地址

https://pan.baidu.com/s/1dwfLHhUUg9nXf1aRAYfJ8Q

提取码:57nm

2.Controller

import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import org.apache.commons.io.IOUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
@RestController
public class OnlinePreviewController {
    @GetMapping("/toPdfFile")
    public void toPdfFile(FileMessage fileMessage) throws IOException {
        // 获取HttpServletResponse
        HttpServletResponse response =
            ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getResponse();
        // 源文件目录
        File inputFile = new File(fileMessage.getFileDownloadUri());
        if (!inputFile.exists()) {
            System.out.println("源文件不存在!");
            return;
        }
        // 转换之后文件生成的地址
        File newFile = new File("D:/common_files/pdf");
        if (!newFile.exists()) {
            newFile.mkdirs();
        }
        String converterPdf = "D:/common_files/pdf" + "/" + fileMessage.getFileName() + "-pdf.pdf";
        // 输出文件目录
        File outputFile = new File(converterPdf);
        if (!outputFile.getParentFile().exists()) {
            outputFile.getParentFile().exists();
        }
        // 调用openoffice服务线程
        String command =
            "C:/Program Files (x86)/OpenOffice 4/program/soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\"";
        Process p = Runtime.getRuntime().exec(command);
        // 连接openoffice服务
        OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);
        connection.connect();
        // 转换
        DocumentConverter converter = new ConverterDocument(connection);
        converter.convert(inputFile, outputFile);
        ServletOutputStream outputStream = response.getOutputStream();
        // 读取文件
        InputStream in = new FileInputStream(new File(converterPdf));
        // copy文件
        int i = IOUtils.copy(in, outputStream);
        in.close();
        outputStream.close();
        // 关闭连接
        connection.disconnect();
        // 关闭进程
        p.destroy();
        System.out.println("转换完成!");
    }
}

转换方法为

DocumentConverter converter = new ConverterDocument(connection);
        converter.convert(inputFile, outputFile);

因为excel折行问题 所以我们把纸张改变 重写了ConverterDocument

3.ConverterDocument

import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter;
import com.sun.star.awt.Size;
import com.sun.star.beans.PropertyValue;
import com.sun.star.lang.XComponent;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.view.PaperFormat;
import com.sun.star.view.XPrintable;
/**
 * 应用模块名称:
 * 代码描述:
 * Copyright: Copyright (C) 2020, Inc. All rights reserved.
 * Company: 万朋测绘科技有限公司
 *
 * @author baocl
 * @since 2020/5/27 14:49
 */
public class ConverterDocument extends StreamOpenOfficeDocumentConverter {
    public ConverterDocument(OpenOfficeConnection connection) {
        super(connection);
    }
    public final static Size A5, A4, A3;
    public final static Size B4, B5, B6;
    public final static Size paperSize;
    static {
        A5 = new Size(14800, 21000);
        A4 = new Size(21000, 29700);
        A3 = new Size(29700, 42000);
        B4 = new Size(25000, 35300);
        B5 = new Size(17600, 25000);
        B6 = new Size(12500, 17600);
        // 最大限度 宽 1600000
        paperSize = new Size(29700, 27940);
    }
    @Override
    protected void refreshDocument(XComponent document) {
        super.refreshDocument(document);
        XPrintable xPrintable = (XPrintable)UnoRuntime.queryInterface(XPrintable.class, document);
        PropertyValue[] printerDesc = new PropertyValue[2];
        // 转换
        printerDesc[0] = new PropertyValue();
        printerDesc[0].Name = "PaperFormat";
        printerDesc[0].Value = PaperFormat.USER;
        // 纸张大小
        printerDesc[1] = new PropertyValue();
        printerDesc[1].Name = "PaperSize";
        printerDesc[1].Value = paperSize;
        try {
            xPrintable.setPrinter(printerDesc);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

但是这里有一个问题 多sheet页 只有第一页改变了 所以解决办法想了两个

1:将多个sheet合成一个

2:将多sheet也分开打印

(暂时没实现)

然后发现了kkFileView 请参考:https://blog.csdn.net/qq_20143059/article/details/106427297

emmmmmm真香


相关文章
|
6月前
|
XML Java 应用服务中间件
【SpringBoot(一)】Spring的认知、容器功能讲解与自动装配原理的入门,带你熟悉Springboot中基本的注解使用
SpringBoot专栏开篇第一章,讲述认识SpringBoot、Bean容器功能的讲解、自动装配原理的入门,还有其他常用的Springboot注解!如果想要了解SpringBoot,那么就进来看看吧!
683 2
|
XML 前端开发 Java
SpringBoot实现文件上传下载功能
本文介绍了如何使用SpringBoot实现文件上传与下载功能,涵盖配置和代码实现。包括Maven依赖配置(如`spring-boot-starter-web`和`spring-boot-starter-thymeleaf`)、前端HTML页面设计、WebConfig路径映射配置、YAML文件路径设置,以及核心的文件上传(通过`MultipartFile`处理)和下载(利用`ResponseEntity`返回文件流)功能的Java代码实现。文章由Colorful_WP撰写,内容详实,适合开发者学习参考。
1098 0
|
9月前
|
缓存 前端开发 Java
SpringBoot 实现动态菜单功能完整指南
本文介绍了一个动态菜单系统的实现方案,涵盖数据库设计、SpringBoot后端实现、Vue前端展示及权限控制等内容,适用于中后台系统的权限管理。
979 2
|
Java API 数据库
构建RESTful API已经成为现代Web开发的标准做法之一。Spring Boot框架因其简洁的配置、快速的启动特性及丰富的功能集而备受开发者青睐。
【10月更文挑战第11天】本文介绍如何使用Spring Boot构建在线图书管理系统的RESTful API。通过创建Spring Boot项目,定义`Book`实体类、`BookRepository`接口和`BookService`服务类,最后实现`BookController`控制器来处理HTTP请求,展示了从基础环境搭建到API测试的完整过程。
504 4
|
JavaScript 安全 Java
如何使用 Spring Boot 和 Ant Design Pro Vue 实现动态路由和菜单功能,快速搭建前后端分离的应用框架
本文介绍了如何使用 Spring Boot 和 Ant Design Pro Vue 实现动态路由和菜单功能,快速搭建前后端分离的应用框架。首先,确保开发环境已安装必要的工具,然后创建并配置 Spring Boot 项目,包括添加依赖和配置 Spring Security。接着,创建后端 API 和前端项目,配置动态路由和菜单。最后,运行项目并分享实践心得,包括版本兼容性、安全性、性能调优等方面。
824 1
|
Java 开发者 微服务
手写模拟Spring Boot自动配置功能
【11月更文挑战第19天】随着微服务架构的兴起,Spring Boot作为一种快速开发框架,因其简化了Spring应用的初始搭建和开发过程,受到了广大开发者的青睐。自动配置作为Spring Boot的核心特性之一,大大减少了手动配置的工作量,提高了开发效率。
306 0
|
11月前
|
安全 Java API
Spring Boot 功能模块全解析:构建现代Java应用的技术图谱
Spring Boot不是一个单一的工具,而是一个由众多功能模块组成的生态系统。这些模块可以根据应用需求灵活组合,构建从简单的REST API到复杂的微服务系统,再到现代的AI驱动应用。
1454 8
|
Java API 数据库
Spring Boot框架因其简洁的配置、快速的启动特性及丰富的功能集而备受开发者青睐
本文通过在线图书管理系统案例,详细介绍如何使用Spring Boot构建RESTful API。从项目基础环境搭建、实体类与数据访问层定义,到业务逻辑实现和控制器编写,逐步展示了Spring Boot的简洁配置和强大功能。最后,通过Postman测试API,并介绍了如何添加安全性和异常处理,确保API的稳定性和安全性。
270 0
|
10月前
|
监控 安全 Java
Java 开发中基于 Spring Boot 3.2 框架集成 MQTT 5.0 协议实现消息推送与订阅功能的技术方案解析
本文介绍基于Spring Boot 3.2集成MQTT 5.0的消息推送与订阅技术方案,涵盖核心技术栈选型(Spring Boot、Eclipse Paho、HiveMQ)、项目搭建与配置、消息发布与订阅服务实现,以及在智能家居控制系统中的应用实例。同时,详细探讨了安全增强(TLS/SSL)、性能优化(异步处理与背压控制)、测试监控及生产环境部署方案,为构建高可用、高性能的消息通信系统提供全面指导。附资源下载链接:[https://pan.quark.cn/s/14fcf913bae6](https://pan.quark.cn/s/14fcf913bae6)。
2165 0

热门文章

最新文章