SpringBoot项目集成ONLYOFFICE

ONLYOFFICE 文档8.2版本已发布:PDF 协作编辑、改进界面、性能优化、表格中的 RTL 支持等更新

文章目录
  • 前言
  • [ONLYOFFICE 产品简介](#ONLYOFFICE 产品简介)
  • 功能与特点
  • [Spring Boot 项目中集成 OnlyOffice](#Spring Boot 项目中集成 OnlyOffice)
    • [1. 环境准备](#1. 环境准备)
    • [2. 部署OnlyOffice Document Server](#2. 部署OnlyOffice Document Server)
    • [3. 配置Spring Boot项目](#3. 配置Spring Boot项目)
    • [4. 实现文档编辑功能](#4. 实现文档编辑功能)
    • [5. 文档保存与版本控制](#5. 文档保存与版本控制)
      • [(1)配置文件 `application.properties`](#(1)配置文件 application.properties)
      • [(2)依赖管理 `pom.xml`](#(2)依赖管理 pom.xml)
      • [(3)控制器 `DocumentController.java`](#(3)控制器 DocumentController.java)
      • [(4)配置类 `OnlyOfficeConfig.java`](#(4)配置类 OnlyOfficeConfig.java)
      • [(5)文档服务类 `DocumentService.java`](#(5)文档服务类 DocumentService.java)
      • [(6)文档模型类 `Document.java`](#(6)文档模型类 Document.java)
      • [(7)前端页面 `index.html`](#(7)前端页面 index.html)
      • [(8)用户服务类 `UserService.java`(可选)](#(8)用户服务类 UserService.java(可选))
      • [(9)修改控制器以添加权限验证 `DocumentController.java`](#(9)修改控制器以添加权限验证 DocumentController.java)
    • [6. 安全性和权限管理](#6. 安全性和权限管理)
  • 体验与测评
  • [8.2 版本的其他更新:](#8.2 版本的其他更新:)
  • [结尾:ONLYOFFICE 项目介绍](#结尾:ONLYOFFICE 项目介绍)

前言

提示:这里可以添加本文要记录的大概内容:

随着互联网技术的发展,越来越多的企业和个人开始寻求高效的在线文档处理解决方案。传统的本地文档编辑软件虽然功能强大,但在多用户协同工作方面存在诸多不便。为了满足这一需求,市场上涌现出了许多优秀的在线文档编辑工具,其中OnlyOffice因其出色的性能和灵活的集成能力而受到广泛好评。本文将详细介绍如何在Spring Boot项目中集成OnlyOffice,实现文档的在线编辑功能,并分享OnlyOffice的产品特点和用户体验。

ONLYOFFICE 产品简介

OnlyOffice是一套完整的开源办公套件,旨在为企业和个人提供高效的文档处理解决方案。它包含文字处理、电子表格和演示文稿三种类型的文档编辑器,支持多种文档格式的读取和编辑。OnlyOffice不仅提供了丰富的桌面应用程序,还拥有强大的Web版编辑器------OnlyOffice Document Server,后者可以方便地集成到各类Web应用中,实现文档的在线编辑和协作。

功能与特点

  • 全面的文档支持 :支持DOCXXLSXPPTX等多种文档格式的编辑和转换。
  • 实时协作编辑:允许多个用户同时编辑同一文档,所有更改实时同步。
  • 权限管理:提供细粒度的文档访问权限控制,确保文档的安全性。
  • 版本管理:自动记录文档的历史版本,便于追踪修改记录和恢复早期版本。
  • 插件扩展:支持通过插件扩展编辑器的功能,满足更多个性化需求。
  • API接口丰富 :提供RESTful APIJavaScript API,方便开发者集成到现有系统中。

Spring Boot 项目中集成 OnlyOffice

1. 环境准备

  • Java环境 :确保已经安装了Java环境(建议JDK 1.8及以上版本)。

  • 构建工具 :安装MavenGradle作为项目的构建工具。

  • Spring Boot项目 :可以通过Spring Initializr快速创建一个Spring Boot项目基础框架。

2. 部署OnlyOffice Document Server

  • 安装OnlyOffice :可以选择在本地或云服务器上安装OnlyOffice Document Server。官方提供了详细的安装指南,可以根据自己的操作系统选择合适的安装方法。

  • 检查安装 :安装完成后,确保Document Server能够正常访问,通常可以通过浏览器访问http://<your_server_ip>:80来检查是否成功安装。

3. 配置Spring Boot项目

  • 添加配置 :在Spring Boot项目的application.properties文件中添加OnlyOffice服务器的相关配置信息,如服务器地址等。

    onlyoffice.server.url=http://<your_server_ip>

    onlyoffice.server.secret=<your_secret_key>

  • 创建控制器:创建一个控制器(Controller)用于处理前端请求,调用OnlyOffice API进行文档的加载、保存等操作。

    @RestController

    @RequestMapping("/documents")

    public class DocumentController {

    复制代码
      @Value("${onlyoffice.server.url}")
      private String serverUrl;
    
      @Value("${onlyoffice.server.secret}")
      private String secretKey;
    
      @GetMapping("/{id}")
      public ResponseEntity<?> getDocument(@PathVariable String id) {
          // 调用OnlyOffice API获取文档编辑所需参数
          // ...
      }
    
      @PostMapping("/{id}")
      public ResponseEntity<?> saveDocument(@PathVariable String id, @RequestBody Document document) {
          // 处理文档保存逻辑
          // ...
      }

    }

4. 实现文档编辑功能

  • 前端集成:利用OnlyOffice提供的JavaScript API,在前端页面中嵌入文档编辑器。

5. 文档保存与版本控制

  • 保存文档:当用户完成编辑并保存文档时,OnlyOffice会将更新后的文档发送回指定的回调URL。

    @PostMapping("/{id}")

    public ResponseEntity<?> saveDocument(@PathVariable String id, @RequestBody Document document) {

    // 将文档保存到数据库或文件系统中

    // 记录版本信息

    return ResponseEntity.ok().build();

    }

  • 版本管理:记录文档的历史版本,便于追踪修改记录和恢复早期版本。

以下是Spring Boot项目中集成OnlyOffice的完整代码部分:

(1)配置文件 application.properties
复制代码
onlyoffice.server.url=http://<your_server_ip>
onlyoffice.server.secret=<your_secret_key>
(2)依赖管理 pom.xml
复制代码
<dependencies>
    <!-- Spring Boot Web Starter -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- Spring Boot Thymeleaf Starter (可选,用于模板引擎) -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

    <!-- Spring Boot Security Starter (可选,用于安全控制) -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

    <!-- JSON处理库 -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
    </dependency>
</dependencies>
(3)控制器 DocumentController.java
复制代码
package com.example.onlyoffice.controller;

import com.example.onlyoffice.config.OnlyOfficeConfig;
import com.example.onlyoffice.model.Document;
import com.example.onlyoffice.service.DocumentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/documents")
public class DocumentController {

    @Autowired
    private OnlyOfficeConfig onlyOfficeConfig;

    @Autowired
    private DocumentService documentService;

    @GetMapping("/{id}")
    public ResponseEntity<Document> getDocument(@PathVariable String id) {
        Document document = documentService.getDocumentById(id);
        if (document == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(document);
    }

    @PostMapping("/{id}")
    public ResponseEntity<Void> saveDocument(@PathVariable String id, @RequestBody Document document) {
        documentService.saveDocument(id, document);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/config/{id}")
    public ResponseEntity<Object> getConfig(@PathVariable String id) {
        return ResponseEntity.ok(onlyOfficeConfig.getConfig(id));
    }
}
(4)配置类 OnlyOfficeConfig.java
复制代码
package com.example.onlyoffice.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class OnlyOfficeConfig {

    @Value("${onlyoffice.server.url}")
    private String serverUrl;

    @Value("${onlyoffice.server.secret}")
    private String secretKey;

    public Map<String, Object> getConfig(String documentId) {
        Map<String, Object> config = new HashMap<>();
        config.put("document", Map.of(
                "fileType", "docx",
                "key", documentId,
                "title", "Sample Document",
                "url", serverUrl + "/documents/" + documentId
        ));
        config.put("documentType", "word");
        config.put("editorConfig", Map.of(
                "callbackUrl", serverUrl + "/documents/" + documentId,
                "mode", "edit",
                "lang", "en"
        ));
        config.put("customization", Map.of(
                "actionBar", false,
                "chat", false
        ));
        return config;
    }
}
(5)文档服务类 DocumentService.java
复制代码
package com.example.onlyoffice.service;

import com.example.onlyoffice.model.Document;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.Collections;

@Service
public class DocumentService {

    private final Map<String, Document> documents = new HashMap<>();
    private final Map<String, List<Document>> history = new HashMap<>();

    public Document getDocumentById(String id) {
        return documents.get(id);
    }

    public void saveDocument(String id, Document document) {
        Document currentDocument = documents.get(id);
        if (currentDocument != null) {
            history.computeIfAbsent(id, k -> new ArrayList<>()).add(currentDocument);
        }
        documents.put(id, document);
    }

    public List<Document> getHistory(String id) {
        return history.getOrDefault(id, Collections.emptyList());
    }
}
(6)文档模型类 Document.java
复制代码
package com.example.onlyoffice.model;

import lombok.Data;

@Data
public class Document {
    private String content;
    // 其他字段...
}
(7)前端页面 index.html
复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>OnlyOffice Document Editor</title>
</head>
<body>
<div id="document-editor" style="height: 600px;"></div>
<script src="https://<your_server_ip>/web-apps/apps/api/documents/api.js"></script>
<script>
    async function loadDocumentEditor() {
        const response = await fetch('/documents/config/<document_id>');
        const config = await response.json();

        new DocsAPI.DocEditor("document-editor", config);
    }

    window.onload = loadDocumentEditor;
</script>
</body>
</html>
(8)用户服务类 UserService.java(可选)
复制代码
package com.example.onlyoffice.service;

import org.springframework.stereotype.Service;

@Service
public class UserService {

    public boolean hasAccess(String username, String documentId) {
        // 这里实现具体的权限验证逻辑
        // 示例:假设所有用户都有访问权限
        return true;
    }
}
(9)修改控制器以添加权限验证 DocumentController.java
复制代码
package com.example.onlyoffice.controller;

import com.example.onlyoffice.config.OnlyOfficeConfig;
import com.example.onlyoffice.model.Document;
import com.example.onlyoffice.service.DocumentService;
import com.example.onlyoffice.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/documents")
public class DocumentController {

    @Autowired
    private OnlyOfficeConfig onlyOfficeConfig;

    @Autowired
    private DocumentService documentService;

    @Autowired
    private UserService userService;

    @GetMapping("/{id}")
    public ResponseEntity<Document> getDocument(@PathVariable String id, @AuthenticationPrincipal UserDetails userDetails) {
        if (!userService.hasAccess(userDetails.getUsername(), id)) {
            return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
        }
        Document document = documentService.getDocumentById(id);
        if (document == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(document);
    }

    @PostMapping("/{id}")
    public ResponseEntity<Void> saveDocument(@PathVariable String id, @RequestBody Document document, @AuthenticationPrincipal UserDetails userDetails) {
        if (!userService.hasAccess(userDetails.getUsername(), id)) {
            return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
        }
        documentService.saveDocument(id, document);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/config/{id}")
    public ResponseEntity<Object> getConfig(@PathVariable String id, @AuthenticationPrincipal UserDetails userDetails) {
        if (!userService.hasAccess(userDetails.getUsername(), id)) {
            return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
        }
        return ResponseEntity.ok(onlyOfficeConfig.getConfig(id));
    }
}

以上代码涵盖了从配置文件、依赖管理、控制器、配置类、服务类、模型类到前端页面的完整实现。希望这些代码能帮助你在Spring Boot项目中成功集成OnlyOffice。如果有任何问题或需要进一步的帮助,请随时提问。

6. 安全性和权限管理

  • 权限控制:通过OnlyOffice提供的API设置文档的访问权限,例如只读、编辑等。

  • 后端验证:在Spring Boot后端实现相应的权限验证逻辑,确保只有授权用户才能访问特定的文档。

体验与测评

在实际使用中,OnlyOffice的表现令人满意。其界面设计简洁直观,用户可以轻松上手;编辑器响应速度快,即使在网络条件不佳的情况下也能保持流畅的操作体验。OnlyOffice对中文文档的支持也相当不错,无论是字体显示还是排版布局都达到了较高的水平。

  • 更快的文件加载速度:为了加快编辑器的打开速度,我们优化了加载脚本。与之前的版本相比:

打开普通文件 -- 最高提速 21%

打开演示文稿 -- 最高提速 17%

Excel:

PPT:

PDF:

当然也有一些不足之处需要注意。部分高级功能需要购买商业许可证才能使用 ,这可能会增加企业的成本负担。另外与其他一些在线文档编辑工具相比,OnlyOffice的社区活跃度稍显不足,遇到问题时可能难以获得及时的帮助和支持。

8.2 版本的其他更新:

协作编辑 PDF 文件;

文本文档中的域代码;

从第三方来源插入文本;

预设阿拉伯语数字编号;

电子表格中的迭代计算;

电子表格编辑器中的丝滑滚动;

在幻灯片上绘图;

演示文稿中的随机切换效果;

所有语言的词典更新和拼写检查改进;

新的图表类型,如直方图、瀑布图、漏斗图等。

结尾:ONLYOFFICE 项目介绍

OnlyOffice不仅仅是一款文档编辑工具,它背后是一个充满活力的开源项目。该项目始于2009年,由Ascensio System SIA公司发起并维护。多年来,OnlyOffice不断发展壮大,形成了一个包括文档编辑器、邮件客户端、项目管理等多个子项目的生态系统。目前,OnlyOffice在全球范围内拥有数百万用户,得到了广泛的认可和好评。

ONLYOFFICE 官网

相关推荐
Java天梯之路1 小时前
Spring Boot 实战:基于 JWT 优化 Spring Security 无状态登录
spring boot·后端
qq_336313931 小时前
java基础-常用的API
java·开发语言
百锦再1 小时前
第21章 构建命令行工具
android·java·图像处理·python·计算机视觉·rust·django
极光代码工作室1 小时前
基于SpringBoot的校园招聘信息管理系统的设计与实现
java·前端·spring
未若君雅裁1 小时前
斐波那契数列 - 动态规划实现 详解笔记
java·数据结构·笔记·算法·动态规划·代理模式
断剑zou天涯1 小时前
【算法笔记】从暴力递归到动态规划(三)
java·算法·动态规划
断剑zou天涯1 小时前
【算法笔记】从暴力递归到动态规划(一)
java·算法·动态规划
o***74171 小时前
SpringBoot Maven快速上手
spring boot·后端·maven
Ace_31750887761 小时前
微店平台关键字搜索接口深度解析:从 Token 动态生成到多维度数据挖掘
java·前端·javascript