在 CentOS 上远程使用 LibreOffice 的可行路径
方案一 远程桌面方式
sudo yum install -y epel-release && sudo yum install -y xrdp tigervnc-serversudo systemctl start xrdp && sudo systemctl enable xrdpsudo firewall-cmd --add-port=3389/tcp --permanent && sudo firewall-cmd --reloadmstsc,输入服务器 IP:3389 登录;登录后在桌面中启动 LibreOffice。sudo yum install -y tigervnc-servervncpasswd(为当前用户)vncserver :1(端口为 5901)ssh -L 5901:localhost:5901 用户名@服务器IP 后再连 localhost:5901。方案二 UNO 监听与脚本远程调用
soffice -headless -accept="socket,host=0.0.0.0,port=2002;urp;" -nofirststartwizard &import uno
from com.sun.star.beans import PropertyValue
def convert_docx_to_pdf(in_path, out_path):
local_ctx = uno.getComponentContext()
resolver = local_ctx.ServiceManager.createInstanceWithContext(
"com.sun.star.bridge.UnoUrlResolver", local_ctx)
ctx = resolver.resolve("uno:socket,host=服务器IP,port=2002;urp;StarOffice.ComponentContext")
desktop = ctx.ServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", ctx)
url_in = uno.systemPathToFileUrl(in_path)
url_out = uno.systemPathToFileUrl(out_path)
doc = desktop.loadComponentFromURL(url_in, "_blank", 0, ())
try:
doc.storeToURL(url_out, (PropertyValue("FilterName", 0, "writer_pdf_Export", 0),))
finally:
doc.dispose()
if __name__ == "__main__":
convert_docx_to_pdf("/data/in.docx", "/data/out.pdf")
运行前确保已安装 LibreOffice 的 Python 环境(可用 LibreOffice 自带 Python 安装依赖),并开放 2002 端口。/opt/libreoffice7.6/program/python -m pip install unoserverunoserver --host 0.0.0.0 --port 2002unoconvert in.docx out.pdf --convert-to pdf方案三 Web 化桌面一键交付
docker run -d \
--name=webtop \
-e PUID=1000 -e PGID=1000 -e TZ=Asia/Shanghai \
-p 3000:3000 \
-v /path/to/data:/config \
--shm-size="1gb" \
--restart unless-stopped \
ghcr.io/linuxserver/webtops
docker exec -it webtop passwd abc 后启用登录页。安全与网络建议