Python+onlyoffice 实现在线word编辑

onlyoffice部署

yml 复制代码
version: "3"
services:
	onlyoffice:
	    image: onlyoffice/documentserver:7.5.1
	    container_name: onlyoffice
	    restart: always
	    environment:
	      - JWT_ENABLED=false
	        #- USE_UNAUTHORIZED_STORAGE=true
	        #- ONLYOFFICE_HTTPS_HSTS_ENABLED=false
	
	    ports:
	      - "8080:80"
	        # - "8443:443"
	
	    volumes:
	      - ./data/onlyoffice/log:/var/log/onlyoffice
	      - ./data/onlyoffice/data:/var/www/onlyoffice/Data
	      - ./data/onlyoffice/lib:/var/lib/onlyoffice
	      - ./data/onlyoffice/postgresql:/var/lib/postgresql
	      # - ./default.json:/etc/onlyoffice/documentserver/default.json

修改default.json

docker cp onlyoffice:/etc/onlyoffice/documentserver/default.json ./

修改以下值为:

json 复制代码
...
"request-filtering-agent" : {
				"allowPrivateIPAddress": true,
				"allowMetaIPAddress": true
			},
...

在docker-compose中添加挂载:

- ./default.json:/etc/onlyoffice/documentserver/default.json

前端代码

保存为 test.html

html 复制代码
<!DOCTYPE html>
<html>

<head>

    <script type='text/javascript' src='http://IP:PORT/web-apps/apps/api/documents/api.js'></script>
    <style>
        html {
            height: 100%;
            width: 100%;
            padding: 0;
            margin: 0;
        }

        body {
            height: 100%;
            width: 100%;
            padding: 0;
            margin: 0;
        }
    </style>


</head>


<body>
    <div id="placeholder"></div>
    <script language="javascript" type="text/javascript">
        var docEditor = new DocsAPI.DocEditor("placeholder", {
            "document": {
                // 文件类型
                "fileType": "docx",
                // 每次需要重新生成随机字符串
                "key": "adfgadfgadfgad",
                // 标题
                "title": "test.docx",
                // 文档地址,GET
                "url": "http://IP:PORT/api/file?name=test.docx"
            },
            "documentType": "word",
            "editorConfig": {
                // 保存文档的回调地址, POST
                "callbackUrl": "http://IP:PORT/api/file?name=test.docx",
                // 查看模式
                // "mode": "view",
                // 编辑模式
                "mode": "edit",
                // 中文 
                "lang": "zh-CN",
                "customization": {
                    // 插件
                    "plugins":false,
                    // 关于
                    "about": false,
                    // 自动保存
                    "autosave":true,
                    // 聊天
                    "chat":false,
                    // 批注
                    "comments":false,
                    // 帮助
                    "help":false,
                }
            }
        });
    </script>
</body>

</html>

python回调接口

python 复制代码
import requests
from flask import Flask, request

app = Flask(__name__)

@app.get("/api/file")
def get_file():
	file_path = "/data/test.docx"
    return make_response(send_file(file_path, as_attachment=True))


@app.post("/api/file")
def save_file():
	file_path = "/data/test.docx"
    callback_obj = request.get_json()
    print(callback_obj)
    status = callback_obj.get("status", -1)
	
    if status == 2 or status == 3 or status == 6:
        try:
            r = requests.get(callback_obj["url"])
            with open(file_path, "wb") as f:
                f.write(r.content)
        except Exception as e:
            print("保存错误:", e)
            return {"error": 1}

    return {"error": 0}
相关推荐
老友@14 小时前
OnlyOffice:前端编辑器与后端API实现高效办公
前端·后端·websocket·编辑器·onlyoffice
老友@9 天前
OnlyOffice编辑器下载失败排查与解决方案
运维·服务器·编辑器·onlyoffice·下载失败
୧⍢⃝୨ LonelyCoder1 个月前
CentOS9 安装Docker+Dpanel+onlyoffice(https、更改字体、字号、去除限制)的避坑笔记
docker·centos·onlyoffice·dpanel
VincentYoung2 个月前
ONLYOFFICE 协作空间 3.0 新功能详解
运维·服务器·onlyoffice·协同
万能的僵小鱼ヾ(≧▽≦*)o2 个月前
onlyoffice 容器配置修改后制作镜像导出以及上传到 dockerhub
onlyoffice
三天不学习2 个月前
快速本地化部署 OnlyOffice服务 ( Linux+Docker)
linux·运维·docker·onlyoffice·在线office
万能的僵小鱼ヾ(≧▽≦*)o2 个月前
onlyoffice8.2 关闭JWT后提示 下载失败
onlyoffice
acycwf3 个月前
宝塔 docker 部署onlyoffice 服务
onlyoffice
知孤云出岫4 个月前
ONLYOFFICE 8.2版本产品评测——遥遥领先,助力自动化办公
onlyoffice·产品评测·文档编辑软件