.docx
、.xlsx
、.pptx
等格式编辑。类别 | 要求 |
---|---|
技术能力 | 熟悉 Web 开发、REST API、Docker 基础操作 |
环境配置 | 可使用 Docker 的服务器(推荐 Linux 环境) |
可用端口 | 开放 9980(LibreOffice Online) 和 8080(WOPI 服务)端口 |
我们使用由 Collabora 发布的官方 Docker 镜像:
docker-compose.yaml
version: "3.6"
services:
libreoffice-app:
image: collabora/code:6.4.8.4
container_name: libreoffice-app
ports:
- "9980:9980"
environment:
- username=admin
- password=adminadmin
restart: always
docker-compose up -d
浏览器访问以下地址确认运行状态:
端点 | 描述 |
---|---|
https://localhost:9980/hosting/discovery | 发现端点(返回 XML 文件,描述支持的 MIME 类型) |
https://localhost:9980/hosting/capabilities | 能力端点(返回 JSON) |
https://localhost:9980/loleaflet/dist/admin/admin.html | 管理界面(使用 admin 密码登录) |
WOPI Host 是您的业务服务,负责:
接口 | 方法 | 功能 |
---|---|---|
/wopi/files/<filename> | GET | 返回文件基本信息 |
/wopi/files/<filename>/contents | GET | 读取文件内容 |
/wopi/files/<filename>/contents | POST / PUT | 保存文件内容 |
from flask import Flask, jsonify, request, send_file
import os
app = Flask(__name__)
file_dir = '/tmp/' # 替换为实际存储路径
@app.route('/wopi/files/<filename>', methods=['GET'])
def check_file_info(filename):
path = os.path.join(file_dir, filename)
if not os.path.exists(path):
return jsonify({"error": "File not found"}), 404
return jsonify({
"BaseFileName": filename,
"Size": os.path.getsize(path),
"OwnerId": "user1",
"UserId": "user1",
"UserCanWrite": True,
"ReadOnly": False
})
@app.route('/wopi/files/<filename>/contents', methods=['GET'])
def get_file(filename):
return send_file(os.path.join(file_dir, filename), as_attachment=True)
@app.route('/wopi/files/<filename>/contents', methods=['POST', 'PUT'])
def put_file(filename):
with open(os.path.join(file_dir, filename), 'wb') as f:
f.write(request.data)
return jsonify({"status": "saved"})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)
docker-compose.yaml
:version: "3.6"
services:
wopi-app:
image: mywopi-service # 请替换为您构建的 WOPI 镜像
ports:
- "8080:8080"
libreoffice-app:
image: collabora/code:6.4.8.4
container_name: libreoffice-app
ports:
- "9980:9980"
environment:
- username=admin
- password=adminadmin
restart: always
✅ 建议通过 Dockerfile 构建
mywopi-service
镜像,并将 WOPI 服务打包进去。
<iframe
src="https://localhost:9980/loleaflet/1430151/loleaflet.html?WOPISrc=http://wopi-app:8080/wopi/files/test.odt&access_token=test"
width="100%" height="800px">
</iframe>
<form action="https://localhost:9980/loleaflet/1430151/loleaflet.html" method="post">
<input name="WOPISrc" value="http://wopi-app:8080/wopi/files/test.odt" />
<input name="access_token" value="test" />
<input type="submit" value="打开文档" />
</form>
<iframe
src="https://localhost:9980/loleaflet/1430151/loleaflet.html?WOPISrc=http://wopi-app:8080/wopi/files/test.odt&access_token=your_jwt_token"
width="100%" height="800px">
</iframe>
您可以进入容器内部修改配置:
docker exec -it libreoffice-app bash
nano /etc/loolwsd/loolwsd.xml
<logging>
<languages>
<host>
/ <allow>
节点postMessage
与编辑器交互LibreOffice Online 支持基于 window.postMessage
的交互 API。
function post(message) {
window.parent.postMessage(JSON.stringify(message), '*');
}
post({
MessageId: "Action_Save",
Values: {
DontTerminateEdit: true,
DontSaveIfUnmodified: false,
Notify: true
}
});
App Store 截图生成器、应用图标生成器 、在线图片压缩和 Chrome插件-强制开启复制-护眼模式-网页乱码设置编码 乖猫记账,AI智能分类的聊天记账。