Python+onlyoffice 实现在线word编辑

onlyoffice部署

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 ./

修改以下值为:

...
"request-filtering-agent" : {
				"allowPrivateIPAddress": true,
				"allowMetaIPAddress": true
			},
...

在docker-compose中添加挂载:

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

前端代码

保存为 test.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回调接口

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}

作者:llc的足迹

物联沃分享整理
物联沃-IOTWORD物联网 » Python+onlyoffice 实现在线word编辑

发表回复