使用Openoffice或LibreOffice实现World、Excel、PPTX在线预览

news2024/11/25 18:52:43

使用Openoffice或LibreOffice实现World、Excel、PPTX在线预览

  • 预览方案
    • 使用第三方服务
    • 使用前端库
    • 转换格式
  • jodconverter
    • jodconverter概述
    • 主要特性
    • OpenOffice
    • LibreOffice
  • jodconverter的基本使用
    • 添加依赖
    • 配置
    • 创建DocumentConverter实例
    • 上传与转换
    • 预览
    • 启动
    • 上传与预览World
  • 与Spring Boot集成
    • 添加依赖项
    • 配置JodConverter和LibreOffice
    • 启动对比
    • 预览 Excel
    • PPTX预览

预览方案

使用第三方服务

有三方服务提供了在线预览文档的功能,可以将文件上传到这些服务,然后嵌入其提供的预览组件到你的网页中。常用的服务包括 Google Docs、Microsoft Office Online 和 Adobe Document Cloud。

1.调用微软的在线预览功能实现

<iframe src='https://view.officeapps.live.com/op/view.aspx?src='+fileurl width='100%' height='100%' frameborder='1'></iframe>

2.调用google的文档在线预览实现

<iframe :src="https://docs.google.com/viewer?url="+fileurl></iframe>

3.调用XDOC文档预览服务

XDOC文档预览服务:https://view.xdocin.com/

XDOC还可以实现文本、带参数文本、html文本、json文本、公文等在线预览,具体实现方法请看官方文档

注意:预览资源必须是公共可访问的

使用前端库

使用一些前端库来实现文档的在线预览。例如,对于Word文档和Excel文件,可以使用mammoth.js或xlsx.js库来解析和渲染文件内容。对于 PDF 文件,可以使用pdf.js库来渲染和显示PDF内容。

world:https://github.com/mwilliamson/mammoth.js

excel:https://github.com/qax-os/excelizehttps://github.com/tealeg/xlsx

pdf:https://github.com/mozilla/pdf.js

转换格式

将文档转换为特定格式,然后在网页中显示。例如:将Word、Excel、PDF转换成PDF、HTML、图片进行预览。

可以使用jodconverter,它是一个强大的文档转换工具,适用于需要将Office文档转换为其他格式的应用程序。

jodconverter

jodconverter概述

jodconverter是一个开源项目,用于将 Office 文档(如 Word、Excel、PowerPoint 等)转换为其他格式,例如 PDF、HTML、图像等。它基于 Java 平台,并使用 LibreOffice/OpenOffice 作为转换引擎。

jodconverter 提供了简单易用的 API,使开发人员能够在自己的应用程序中集成文档转换功能。它可以与 Java 应用程序一起使用,也可以通过 REST API 进行远程调用。

Github:https://github.com/jodconverter/jodconverter

主要特性

文档转换:jodconverter 可以将各种 Office 文档格式(如 DOCX、XLSX、PPTX 等)转换为其他格式,如 PDF、HTML、图像(PNG、JPEG 等)等。

批量转换:你可以使用 jodconverter 批量转换多个文档,提高转换效率。

异步转换:jodconverter 支持异步转换,可以在后台进行文档转换,不会阻塞主线程。

自定义配置:你可以根据需要配置转换过程中的参数,如输出格式、图像质量、页面大小等。

监听器支持:jodconverter 提供了监听器接口,可以在转换过程中监听转换状态和进度。

多平台支持:jodconverter 可以在多个平台上运行,包括 Windows、Linux 和 macOS。

OpenOffice

jodconverter依赖于Apache OpenOffice或LibreOffice,在使用jodconverter之前需要先安装二者其一

Apache OpenOffice是一款免费的开源办公软件套件,包含了文本编辑器、电子表格、演示文稿、图形处理和数据库管理等多种功能。它由Apache软件基金会开发和维护,支持跨平台,可以在Windows、Mac和Linux等操作系统上运行。Apache OpenOffice与Microsoft Office相似,可用于创建、编辑和共享各种文档、报告和电子邮件等内容。

openoffice下载:http://www.openoffice.org/download/index.html
在这里插入图片描述
在这里插入图片描述

LibreOffice

jodconverter依赖于Apache OpenOffice或LibreOffice,在使用jodconverter之前需要先安装二者其一

LibreOffice是一款免费的开源办公套件,它包括文本编辑器、电子表格、演示文稿、绘图和数据库管理工具等多个组件,可以用于处理各种常见的办公任务。LibreOffice由The Document Foundation开发,是OpenOffice.org的一个分支,其源代码是完全开放的,并且提供了对多种操作系统的支持,包括Windows、Mac OS X和Linux等。

libreoffice:https://www.libreoffice.org/download/download-libreoffice/

在这里插入图片描述
在这里插入图片描述

jodconverter的基本使用

添加依赖

        <!-- 核心包 -->
        <dependency>
            <groupId>org.jodconverter</groupId>
            <artifactId>jodconverter-core</artifactId>
            <version>4.4.6</version>
        </dependency>
        <!-- 本地支持包 -->
        <dependency>
            <groupId>org.jodconverter</groupId>
            <artifactId>jodconverter-local</artifactId>
            <version>4.4.6</version>
        </dependency>

配置

jodconverter:
  local:
    # libreOffice根目录
#    office-home: D:\LibreOffice
    # OpenOffice安装地址
    office-home: D:\OpenOffice 4
    # 同时执行任务的个数,最大进程数
    max-tasks-per-process: 2
    # 开启多个进程,每个端口对应一个进程;设置端口号(任意设置)
    port-numbers: 3000,3001
    # 一个进程的超时时间
    process-timeout: 120000

创建DocumentConverter实例

import lombok.extern.slf4j.Slf4j;
import org.jodconverter.core.DocumentConverter;
import org.jodconverter.core.office.InstalledOfficeManagerHolder;
import org.jodconverter.core.office.OfficeException;
import org.jodconverter.core.office.OfficeManager;
import org.jodconverter.core.office.OfficeUtils;
import org.jodconverter.local.LocalConverter;
import org.jodconverter.local.office.LocalOfficeManager;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.Arrays;

@Slf4j
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class ConverterConfiguration {

    private LocalOfficeManager officeManager;
    @Value("${jodconverter.local.office-home}")
    private String officeHome;

    @Value("${jodconverter.local.port-numbers}")
    private String portNumber;

    @Value("${jodconverter.local.max-tasks-per-process}")
    private Integer maxTaskPerProcess;

    @Value("${jodconverter.local.process-timeout}")
    private Long processTimeout;

    /**
     * 启动Office组件进程
     *
     * @return
     */
    @PostConstruct
    public OfficeManager officeManager() {
        // 多个端口处理
        String[] portsString = portNumber.split(",");
        int[] ports = Arrays.stream(portsString).mapToInt(Integer::parseInt).toArray();
        // 系统判断
        String os = System.getProperty("os.name").toLowerCase();

        officeManager = LocalOfficeManager.builder()
                .officeHome(os.contains("windows") ? officeHome : "linuxHome")
                .portNumbers(ports)
                .processTimeout(processTimeout)
                .maxTasksPerProcess(maxTaskPerProcess)
                .install()
                .build();
        try {
            officeManager.start();
            InstalledOfficeManagerHolder.setInstance(officeManager);
            log.info("office进程启动成功");
        } catch (OfficeException e) {
            log.error("启动office组件失败");
            throw new RuntimeException(e);
        }
        return officeManager;
    }

    /**
     * 创建DocumentConverter实例
     *
     * @return
     */
    @Bean
    public DocumentConverter documentConverter() {
        log.info("创建DocumentConverter实例");
        LocalConverter converter = LocalConverter.builder()
                .officeManager(officeManager)
                .build();
        return converter;
    }

    @PreDestroy
    public void destroyOfficeManager() {
        if (null != officeManager && officeManager.isRunning()) {
            log.info("终止office进程");
            OfficeUtils.stopQuietly(officeManager);
        }
    }

}

上传与转换


import org.jodconverter.DocumentConverter;
import org.jodconverter.document.DefaultDocumentFormatRegistry;
import org.jodconverter.office.OfficeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
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;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;

@RestController
public class FileController {

    /**
     * 上传与预览的测试目录
     */
    public static String PATH = "D://test//";

	/**
     *  转换器
     */
    @Autowired
    private DocumentConverter documentConverter;


    @GetMapping("/test")
    public String test() {
        return "OK";
    }

    @PostMapping("/upload")
    public String handleFileUpload(@RequestParam("file") MultipartFile file) throws OfficeException {
        if (file.isEmpty()) {
            return "请选择上传文件";
        }

        // 保存上传文件
        File localFile = new File(PATH + file.getOriginalFilename());
        try (OutputStream os = new FileOutputStream(localFile)) {
            os.write(file.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 转换成pdf的名称
        String pdfName = UUID.randomUUID().toString().replace("-", "");
        // 转换成pdf存放路径
        File pdfFile = new File(PATH + pdfName + ".pdf");
        // 开始转换
        documentConverter.convert(localFile)
                .as("doc".equals(DefaultDocumentFormatRegistry.DOC.getExtension()) ? DefaultDocumentFormatRegistry.DOC : DefaultDocumentFormatRegistry.DOCX)
                .to(pdfFile).as(DefaultDocumentFormatRegistry.PDF)
                .execute();

        // 返回转换后的pdf文件的URL
        String previewUrl = "http://localhost:8888/preview/" + pdfName;
        return "<a href='" + previewUrl + "' target='_blank'>Preview</a>";
    }
}

预览

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

@RestController
public class PreviewController {

    @GetMapping("/preview/{fileName}")
    public void showPreview(@PathVariable String fileName, HttpServletResponse response) throws IOException {
        File file = new File(FileController.PATH + fileName + ".pdf");
        response.setContentType("application/pdf");
        response.setHeader("Content-Disposition", "inline; filename=" + fileName);
        response.setHeader("Content-Length", String.valueOf(file.length()));
        Files.copy(file.toPath(), response.getOutputStream());
    }
}

启动

OpenOffice启动日志:

INFO 25076 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8888 (http)
INFO 25076 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
INFO 25076 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.41]
INFO 25076 --- [  restartedMain] o.a.catalina.core.AprLifecycleListener   : Loaded Apache Tomcat Native library [1.2.31] using APR version [1.7.0].
INFO 25076 --- [  restartedMain] o.a.catalina.core.AprLifecycleListener   : APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
INFO 25076 --- [  restartedMain] o.a.catalina.core.AprLifecycleListener   : APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
INFO 25076 --- [  restartedMain] o.a.catalina.core.AprLifecycleListener   : OpenSSL successfully initialized [OpenSSL 1.1.1l  24 Aug 2021]
INFO 25076 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
INFO 25076 --- [  restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2235 ms
INFO 25076 --- [  restartedMain] c.y.demo.config.ConverterConfiguration   : office进程启动成功
INFO 25076 --- [er-offprocmng-0] o.j.local.office.OfficeDescriptor        : soffice info (from exec path): Product: OpenOffice - Version: ??? - useLongOptionNameGnuStyle: false
INFO 25076 --- [er-offprocmng-1] o.j.local.office.OfficeDescriptor        : soffice info (from exec path): Product: OpenOffice - Version: ??? - useLongOptionNameGnuStyle: false
INFO 25076 --- [  restartedMain] c.y.demo.config.ConverterConfiguration   : 创建DocumentConverter实例
INFO 25076 --- [  restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
INFO 25076 --- [  restartedMain] o.s.b.a.w.s.WelcomePageHandlerMapping    : Adding welcome page template: index
INFO 25076 --- [er-offprocmng-0] o.j.l.office.LocalOfficeProcessManager   : Starting process with --accept 'socket,host=127.0.0.1,port=3000,tcpNoDelay=1;urp;StarOffice.ServiceManager' and profileDir 'C:\Users\Admin\AppData\Local\Temp\.jodconverter_socket_host-127.0.0.1_port-3000_tcpNoDelay-1'
INFO 25076 --- [er-offprocmng-1] o.j.l.office.LocalOfficeProcessManager   : Starting process with --accept 'socket,host=127.0.0.1,port=3001,tcpNoDelay=1;urp;StarOffice.ServiceManager' and profileDir 'C:\Users\Admin\AppData\Local\Temp\.jodconverter_socket_host-127.0.0.1_port-3001_tcpNoDelay-1'
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
INFO 25076 --- [er-offprocmng-0] o.j.local.office.OfficeConnection        : Connected: 'socket,host=127.0.0.1,port=3000,tcpNoDelay=1'
INFO 25076 --- [er-offprocmng-0] o.j.l.office.LocalOfficeProcessManager   : Started process; pid: 27528
INFO 25076 --- [er-offprocmng-1] o.j.local.office.OfficeConnection        : Connected: 'socket,host=127.0.0.1,port=3001,tcpNoDelay=1'
INFO 25076 --- [er-offprocmng-1] o.j.l.office.LocalOfficeProcessManager   : Started process; pid: 20444
INFO 25076 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
INFO 25076 --- [  restartedMain] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 2 endpoint(s) beneath base path '/actuator'
INFO 25076 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8888 (http) with context path ''
INFO 25076 --- [  restartedMain] cn.ybzy.demo.Application                 : Started Application in 6.576 seconds (JVM running for 8.362)
INFO 25076 --- [1)-192.168.56.1] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
WARN 25076 --- [1)-192.168.56.1] com.zaxxer.hikari.util.DriverDataSource  : Registered driver with driverClassName=com.mysql.jdbc.Driver was not found, trying direct instantiation.
INFO 25076 --- [1)-192.168.56.1] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
INFO 25076 --- [3)-192.168.56.1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
INFO 25076 --- [3)-192.168.56.1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
INFO 25076 --- [3)-192.168.56.1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 9 ms
INFO 25076 --- [ter-poolentry-2] o.j.local.task.LocalConversionTask       : Executing local conversion task [doc -> pdf]...

LibreOffice启动日志:

5728 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8888 (http)
25728 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
5728 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.41]
NFO 5728 --- [  restartedMain] o.a.catalina.core.AprLifecycleListener   : Loaded Apache Tomcat Native library [1.2.31] using APR version [1.7.0].INFO 5728 --- [  restartedMain] o.a.catalina.core.AprLifecycleListener   : APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].INFO 5728 --- [  restartedMain] o.a.catalina.core.AprLifecycleListener   : APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]INFO 5728 --- [  restartedMain] o.a.catalina.core.AprLifecycleListener   : OpenSSL successfully initialized [OpenSSL 1.1.1l  24 Aug 2021]INFO 5728 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContextINFO 5728 --- [  restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2260 msINFO 5728 --- [  restartedMain] c.y.demo.config.ConverterConfiguration   : office进程启动成功INFO 5728 --- [er-offprocmng-0] o.j.local.office.OfficeDescriptor        : soffice info (from exec path): Product: LibreOffice - Version: ??? - useLongOptionNameGnuStyle: true
INFO 5728 --- [er-offprocmng-1] o.j.local.office.OfficeDescriptor        : soffice info (from exec path): Product: LibreOffice - Version: ??? - useLongOptionNameGnuStyle: true
INFO 5728 --- [  restartedMain] c.y.demo.config.ConverterConfiguration   : 创建DocumentConverter实例
INFO 5728 --- [  restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
INFO 5728 --- [  restartedMain] o.s.b.a.w.s.WelcomePageHandlerMapping    : Adding welcome page template: index
INFO 5728 --- [er-offprocmng-1] o.j.l.office.LocalOfficeProcessManager   : Starting process with --accept 'socket,host=127.0.0.1,port=3001,tcpNoDelay=1;urp;StarOffice.ServiceManager' and profileDir 'C:\Users\Admin\AppData\Local\Temp\.jodconverter_socket_host-127.0.0.1_port-3001_tcpNoDelay-1'
INFO 5728 --- [er-offprocmng-0] o.j.l.office.LocalOfficeProcessManager   : Starting process with --accept 'socket,host=127.0.0.1,port=3000,tcpNoDelay=1;urp;StarOffice.ServiceManager' and profileDir 'C:\Users\Admin\AppData\Local\Temp\.jodconverter_socket_host-127.0.0.1_port-3000_tcpNoDelay-1'
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
INFO 5728 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
INFO 5728 --- [  restartedMain] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 2 endpoint(s) beneath base path '/actuator'
INFO 5728 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8888 (http) with context path ''
INFO 5728 --- [  restartedMain] cn.ybzy.demo.Application                 : Started Application in 6.968 seconds (JVM running for 8.97)
INFO 5728 --- [1)-192.168.56.1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
INFO 5728 --- [1)-192.168.56.1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet' INFO 5728 --- [2)-192.168.56.1] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
WARN 5728 --- [2)-192.168.56.1] com.zaxxer.hikari.util.DriverDataSource  : Registered driver with driverClassName=com.mysql.jdbc.Driver was not found, trying direct instantiation.
INFO 5728 --- [1)-192.168.56.1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 10 ms
INFO 5728 --- [2)-192.168.56.1] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
INFO 5728 --- [er-offprocmng-1] o.j.local.office.OfficeConnection        : Connected: 'socket,host=127.0.0.1,port=3001,tcpNoDelay=1'
INFO 5728 --- [er-offprocmng-1] o.j.l.office.LocalOfficeProcessManager   : Started process; pid: 29060
INFO 5728 --- [er-offprocmng-0] o.j.local.office.OfficeConnection        : Connected: 'socket,host=127.0.0.1,port=3000,tcpNoDelay=1'
INFO 5728 --- [er-offprocmng-0] o.j.l.office.LocalOfficeProcessManager   : Started process; pid: 16476
INFO 5728 --- [ter-poolentry-2] o.j.local.task.LocalConversionTask       : Executing local conversion task [doc -> pdf]...

上传与预览World

在这里插入图片描述

在这里插入图片描述

与Spring Boot集成

添加依赖项

        <!-- 核心包 -->
        <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>
        <!-- springboot支持,包括自动配置类 -->
        <dependency>
            <groupId>org.jodconverter</groupId>
            <artifactId>jodconverter-spring-boot-starter</artifactId>
            <version>4.2.2</version>
        </dependency>

配置JodConverter和LibreOffice

通过在应用程序的配置文件中添加以下属性来配置JodConverter和LibreOffice:

jodconverter:
  local:
    enabled: true
    # libreOffice根目录
    office-home: D:\LibreOffice
    # OpenOffice安装地址
    # office-home: D:\OpenOffice 4
    # 同时执行任务的个数,最大进程数
    max-tasks-per-process: 2
    # 开启多个进程,每个端口对应一个进程;设置端口号(任意设置)
    port-numbers: 3000
    # 任务执行的超时时间
    taskExecutionTimeout: 120000
    # 任务队列的超时时间
    taskQueueTimeout: 30000
    # 一个进程的超时时间
    process-timeout: 120000

启动对比

使用Openoffice

INFO 17640 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8888 (http)
INFO 17640 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
INFO 17640 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.41]
INFO 17640 --- [  restartedMain] o.a.catalina.core.AprLifecycleListener   : Loaded Apache Tomcat Native library [1.2.31] using APR version [1.7.0].
INFO 17640 --- [  restartedMain] o.a.catalina.core.AprLifecycleListener   : APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
INFO 17640 --- [  restartedMain] o.a.catalina.core.AprLifecycleListener   : APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
INFO 17640 --- [  restartedMain] o.a.catalina.core.AprLifecycleListener   : OpenSSL successfully initialized [OpenSSL 1.1.1l  24 Aug 2021]
INFO 17640 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
INFO 17640 --- [  restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 5223 ms
INFO 17640 --- [  restartedMain] o.j.office.OfficeProcessManager          : Submitting task 'Start' and waiting...
INFO 17640 --- [ProcessThread-0] o.jodconverter.office.OfficeDescriptor   : soffice info (from exec path): Product: OpenOffice - Version: ??? - useLongOptionNameGnuStyle: false
INFO 17640 --- [ProcessThread-0] org.jodconverter.office.OfficeProcess    : Starting process with acceptString 'socket,host=127.0.0.1,port=3000,tcpNoDelay=1;urp;StarOffice.ServiceManager' and profileDir 'C:\Users\Admin\AppData\Local\Temp\.jodconverter_socket_host-127.0.0.1_port-3000_tcpNoDelay-1'
INFO 17640 --- [ProcessThread-0] org.jodconverter.office.OfficeProcess    : Started process; pid = 19960
INFO 17640 --- [ProcessThread-0] o.jodconverter.office.OfficeConnection   : Connected: 'socket,host=127.0.0.1,port=3000,tcpNoDelay=1'
INFO 17640 --- [  restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
INFO 17640 --- [  restartedMain] o.s.b.a.w.s.WelcomePageHandlerMapping    : Adding welcome page template: index
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
INFO 17640 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
INFO 17640 --- [  restartedMain] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 2 endpoint(s) beneath base path '/actuator'
INFO 17640 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8888 (http) with context path ''
INFO 17640 --- [  restartedMain] cn.ybzy.demo.Application                 : Started Application in 12.67 seconds (JVM running for 16.446)
INFO 17640 --- [2)-192.168.56.1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
INFO 17640 --- [2)-192.168.56.1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
INFO 17640 --- [1)-192.168.56.1] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
WARN 17640 --- [1)-192.168.56.1] com.zaxxer.hikari.util.DriverDataSource  : Registered driver with driverClassName=com.mysql.jdbc.Driver was not found, trying direct instantiation.
INFO 17640 --- [2)-192.168.56.1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 16 ms
INFO 17640 --- [1)-192.168.56.1] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
INFO 17640 --- [agerPoolEntry-1] o.jodconverter.task.LocalConversionTask  : Executing local conversion task...

使用LibreOffice

INFO 13756 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8888 (http)
INFO 13756 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
INFO 13756 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.41]
INFO 13756 --- [  restartedMain] o.a.catalina.core.AprLifecycleListener   : Loaded Apache Tomcat Native library [1.2.31] using APR version [1.7.0].
INFO 13756 --- [  restartedMain] o.a.catalina.core.AprLifecycleListener   : APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
INFO 13756 --- [  restartedMain] o.a.catalina.core.AprLifecycleListener   : APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
INFO 13756 --- [  restartedMain] o.a.catalina.core.AprLifecycleListener   : OpenSSL successfully initialized [OpenSSL 1.1.1l  24 Aug 2021]
INFO 13756 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
INFO 13756 --- [  restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2312 ms
INFO 13756 --- [  restartedMain] o.j.office.OfficeProcessManager          : Submitting task 'Start' and waiting...
INFO 13756 --- [ProcessThread-0] o.jodconverter.office.OfficeDescriptor   : soffice info (from exec path): Product: LibreOffice - Version: ??? - useLongOptionNameGnuStyle: true
INFO 13756 --- [ProcessThread-0] org.jodconverter.office.OfficeProcess    : Starting process with acceptString 'socket,host=127.0.0.1,port=3000,tcpNoDelay=1;urp;StarOffice.ServiceManager' and profileDir 'C:\Users\Admin\AppData\Local\Temp\.jodconverter_socket_host-127.0.0.1_port-3000_tcpNoDelay-1'
INFO 13756 --- [ProcessThread-0] org.jodconverter.office.OfficeProcess    : Started process; pid = 3216
INFO 13756 --- [ProcessThread-0] o.jodconverter.office.OfficeConnection   : Connected: 'socket,host=127.0.0.1,port=3000,tcpNoDelay=1'
INFO 13756 --- [  restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
INFO 13756 --- [  restartedMain] o.s.b.a.w.s.WelcomePageHandlerMapping    : Adding welcome page template: index
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
INFO 13756 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
INFO 13756 --- [  restartedMain] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 2 endpoint(s) beneath base path '/actuator'
INFO 13756 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8888 (http) with context path ''
INFO 13756 --- [  restartedMain] cn.ybzy.demo.Application                 : Started Application in 21.748 seconds (JVM running for 23.568)
INFO 13756 --- [1)-192.168.56.1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
INFO 13756 --- [1)-192.168.56.1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
INFO 13756 --- [2)-192.168.56.1] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
WARN 13756 --- [2)-192.168.56.1] com.zaxxer.hikari.util.DriverDataSource  : Registered driver with driverClassName=com.mysql.jdbc.Driver was not found, trying direct instantiation.
INFO 13756 --- [1)-192.168.56.1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 10 ms
INFO 13756 --- [2)-192.168.56.1] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
INFO 13756 --- [agerPoolEntry-1] o.jodconverter.task.LocalConversionTask  : Executing local conversion task...

经测试发现: 使用Openoffice启动速度更快

预览 Excel

可以将Excel转成PDF或者HTML格式进行预览,通常来说转成HTML格式更好,这里仅作为调试转换成PDF

@RestController
public class FileController {

    /**
     * 上传与预览的测试目录
     */
    public static String PATH = "D://test//";

    @Autowired
    private DocumentConverter documentConverter;


    @GetMapping("/test")
    public String test() {
        return "OK";
    }

    @PostMapping("/upload")
    public String handleFileUpload(@RequestParam("file") MultipartFile file) throws OfficeException, IOException {
        if (file.isEmpty()) {
            return "请选择上传文件";
        }

        // 保存上传文件
        File localFile = new File(PATH + file.getOriginalFilename());
        FileUtils.writeByteArrayToFile(localFile, file.getBytes());

        // 转换成pdf的名称
        String pdfName = UUID.randomUUID().toString().replace("-", "");
        // 转换成pdf存放路径
        File pdfFile = new File(PATH + pdfName + ".pdf");

        // 开始转换
        documentConverter.convert(localFile).to(pdfFile).execute();

        // 返回转换后的pdf文件的URL
        String previewUrl = "http://localhost:8888/preview/" + pdfName;
        return "<a href='" + previewUrl + "' target='_blank'>Preview</a>";
    }
}

NFO 24048 --- [er-offprocmng-0] o.j.local.office.OfficeConnection        : Connected: 'socket,host=127.0.0.1,port=3000,tcpNoDelay=1'
INFO 24048 --- [er-offprocmng-0] o.j.l.office.LocalOfficeProcessManager   : Started process; pid: 9896
INFO 24048 --- [ter-poolentry-1] o.j.local.task.LocalConversionTask       : Executing local conversion task [xlsx -> pdf]...

在这里插入图片描述

在这里插入图片描述

PPTX预览

    @PostMapping("/upload")
    public String handleFileUpload(@RequestParam("file") MultipartFile file) throws OfficeException, IOException {
        if (file.isEmpty()) {
            return "请选择上传文件";
        }

        // 保存上传文件
        File localFile = new File(PATH + file.getOriginalFilename());
        FileUtils.writeByteArrayToFile(localFile, file.getBytes());

        // 转换成pdf的名称
        String pdfName = UUID.randomUUID().toString().replace("-", "");
        // 转换成pdf存放路径
        File pdfFile = new File(PATH + pdfName + ".pdf");

        // 开始转换
        documentConverter.convert(localFile).to(pdfFile).execute();
        
        // 返回转换后的pdf文件的URL
        String previewUrl = "http://localhost:8888/preview/" + pdfName;
        return previewUrl;
    }

在这里插入图片描述

INFO 24048 --- [er-offprocmng-0] o.j.local.office.OfficeConnection        : Connected: 'socket,host=127.0.0.1,port=3000,tcpNoDelay=1'
INFO 24048 --- [er-offprocmng-0] o.j.l.office.LocalOfficeProcessManager   : Started process; pid: 16724
INFO 24048 --- [ter-poolentry-1] o.j.local.task.LocalConversionTask       : Executing local conversion task [pptx -> pdf]...

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/848237.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

海外跨境购物商城汇率自动更新系统开发

要搭建一个海外跨境购物商城多货币汇率自动更新系统&#xff0c;您可以按照以下步骤进行&#xff1a; 1. 选择合适的电子商务平台&#xff1a;选择一款适合海外跨境购物的电子商务平台。 2. 确定支付方式&#xff1a;选择支持多种货币支付的支付网关。确保支付网关支持自动汇…

springboot(1)

精要&#xff1a; 自动配置&#xff1a;针对很多Spring应用程序常见的应用功能&#xff0c;Spring Boot能自动提供相关配置。 起步依赖&#xff1a;告诉Spring Boot需要什么功能&#xff0c;它就能引入需要的库。 命令行界面&#xff1a;这是Spring Boot的可选特性&#xff0…

基于DETR (DEtection TRansformer)开发构建MSTAR雷达影像目标检测系统

关于DETR相关的实践在之前的文章中很详细地介绍过&#xff0c;感兴趣的话可以自行移步阅读即可&#xff1a; 《DETR (DEtection TRansformer)基于自建数据集开发构建目标检测模型超详细教程》 《书接上文——DETR评估可视化》 基于MSTAR雷达影像数据开发构建目标检测系统&am…

力扣279.完全平方数(动态规划)

class Solution { public:int numSquares(int n) {vector<int> f(n 1);for (int i 1; i < n; i) {int minn INT_MAX;for (int j 1; j * j < i; j) {minn min(minn, f[i - j * j]); //上一次的 & 当前数可以找到一个新的更大的平方}f[i] minn 1; }…

分布式应用:Zabbix自定义监控模板

目录 一、理论 1.zabbix监控模板 2.在客户端创建自定义 key 3.在 Web 页面创建自定义监控项模板 4.设置邮件报警 二、实验 1.在客户端创建自定义 key 2.在 Web 页面创建自定义监控项模板 3.设置邮件报警 三、问题 1.查看动作发送邮件失败 四、总结 一、理论 1.zab…

功能上新|全新GPU性能优化方案

GPU优化迎来了全新的里程碑&#xff01;我们深知移动游戏对高品质画面的追求日益升温&#xff0c;因此UWA一直着眼于移动设备GPU性能优化&#xff0c;以确保您的游戏体验尽善尽美。然而&#xff0c;不同GPU芯片之间的性能差异及可能导致的GPU瓶颈问题&#xff0c;让优化工作变得…

Hybrid技术的下一站是什么?

Hybrid这个词&#xff0c;在App开发领域&#xff0c;相信大家都不陌生。Hybrid App是指介于web-app、native-app这两者之间的app&#xff0c;它虽然看上去是一个Native App&#xff0c;但只有一个UI WebView&#xff0c;里面访问的是一个Web App。Hybrid在移动领域的发展&#…

低代码平台——需求和技术发展的产物

前言&#xff1a;低代码平台是需求和技术发展的必然产物&#xff0c;从开发方式、开发门槛、开发效率各层面上&#xff0c;跟传统的开发方式有根本区别&#xff0c;是业界已达成共识的新技术方向。 一、低代码平台起源 从2016年开始&#xff0c;低代码突然进入快速发展阶段&…

无涯教程-Perl - flock函数

描述 该函数支持使用系统flock(),fcntl()锁定或lockf()在指定的FILEHANDLE上锁定文件。使用的确切实现方式取决于系统支持的功能。 OPERATION是此处定义的static 值之一。 Operation Result LOCK_SH Set shared lock. LOCK_EX Set exclusive lock. LOCK_UN Unlock specifi…

用Vue实现页面访问拦截

(创作不易&#xff0c;感谢有你&#xff0c;你的支持&#xff0c;就是我前行的最大动力&#xff0c;如果看完对你有帮助&#xff0c;请留下您的足迹&#xff09; 目录 页面访问拦截 1.创建axios实例 2.添加拦截器 3.全局前置守卫 可选的第三个参数 next 总结&#xff1a…

JDBC概念与类的详解

JDBC 文章目录 JDBC一、概念及入门二、各个类详解1、DriverManager&#xff1a;驱动管理对象&#xff08;1&#xff09;注册驱动&#xff08;2&#xff09;获取数据库连接 2、Connection&#xff1a;数据库连接对象&#xff08;1&#xff09;获取执行sql的对象&#xff08;2&am…

碎片拼接恢复XenServer虚拟机中SQLServer数据库的数据恢复案例

服务器数据恢复环境&#xff1a; 某单位一台Dell服务器上使用RAID卡搭建了一组由4盘RAID10。 服务器安装的XenServer虚拟化操作系统&#xff0c;虚拟机采用的Windows Server操作系统。 共系统盘和数据盘两个虚拟机磁盘&#xff0c;上层部署的是Web服务器&#xff08;ASP SQLSe…

实施高级存储功能

实施高级存储功能 使用Stratis管理分层存储 Stratis 适用于Linux的本地存储管理解决方案。旨在提供更便利的方式执行存储的初始配置&#xff0c;对存储配置进行修改&#xff0c;并使用高级存储功能。 Stratis以管理物理存储设备池的服务形式运行&#xff0c;并透明地为新创…

基于机器视觉和倾角传感器的位姿检测系统及验证

悬臂式掘进机位姿检测是综掘工作面自动化的基础和前提。只有获取稳定可靠的掘进机实时位姿&#xff0c;才能够在此基础上进行综掘工作面自动化、智能化改造工作。 为了提高井下综掘工作面的生产效率&#xff0c;西安电子科技大学机电工程学院的研究团队提出一种基于机器视觉和…

[免费在线] 将 PDF 转换为 Excel 或 Excel 转换为 PDF | 5 工具

有了免费的在线 PDF 转换器&#xff0c;您可以轻松免费在线将 PDF 转换为 Excel 或 Excel 转换为 PDF。这篇文章为您筛选了 5 个最常用的工具。要从存储介质恢复错误删除或丢失的 PDF 文档、Excel 电子表格、Word 文件或任何其他文件&#xff0c;您可以使用免费的数据恢复程序 …

力扣62.不同路径(动态规划)

/*** 一个机器人位于一个 m x n 网格的左上角 &#xff08;起始点在下图中标记为 “Start” &#xff09;。* 机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角&#xff08;在下图中标记为 “Finish” &#xff09;。* 问总共有多少条不同的路径&#xff1f; *…

【生物医学】应激(应激反应)全身适应综合征

最近在探索疲劳、负荷、应激方面的底层发生机制&#xff0c;遂整理了一些相关内容&#xff0c;以脑图方式呈现。本文以生物医学向为主。 OK&#xff0c;开始基础介绍&#xff1a;应激 (stress)是指在收到外部或内部、心理社会刺激下的非特异性适应反应。 本文主要收集整理了相…

BLIP2

BLIP2的任务是基于已有的固定参数的图像encoder和语言大模型&#xff08;LLM&#xff09;搭建一个具有图像理解能力的图文模型&#xff0c;输入是图像和文本&#xff0c;输出是文本。 BLIP2基于Q-Former结构&#xff0c;如下图所示。Q-Former包含图像transformer和文本transfo…

Unity之ShaderGraph 节点介绍 UV节点

UV节点 Flipbook&#xff08;翻页或纹理帧动画&#xff09; Polar Coordinates&#xff08;将输入 UV 的值转换为极坐标。&#xff09; Radial Shear&#xff08;径向剪切变形&#xff09; Rotate&#xff08;将UV 的值旋转&#xff09; Spherize&#xff08;鱼眼镜头的球形变…

3天爆肝整理,自动化测试-YAML文件读写实战(超细总结)

目录&#xff1a;导读 前言一、Python编程入门到精通二、接口自动化项目实战三、Web自动化项目实战四、App自动化项目实战五、一线大厂简历六、测试开发DevOps体系七、常用自动化测试工具八、JMeter性能测试九、总结&#xff08;尾部小惊喜&#xff09; 前言 YAML 简介 YAML&…