经过多次测试稳定版本为:LibreOffice_7.3.7.2

7.4版本开始没办法优化内存会导致有些word转PDF时卡死

程序调用转换后日志输出后卡死不动:jodconverter executing local conversion task [docx -> pdf]

taskExecutionTimeout: 1200000 #默认为2分钟  将该值设置30分钟,有些格式有问题的会经过很久等待后转化成果

Libreoffice 转 PDF部分文件存在空白页的情况:需要将word模板文件使用libreoffice打开,将空白页格式调整即可(转出的pdf样式和libreoffice开打word样式肯定是一致的,wps、msword编辑的word有些格式libreoffice不适配)

1. 下载指定版本 LibreOffice

# 进入工作目录
cd /opt

# 下载并解压 LibreOffice 7.3.7.2(64位 Deb 包)
wget https://downloadarchive.documentfoundation.org/libreoffice/old/7.3.7.2/deb/x86_64/LibreOffice_7.3.7.2_Linux_x86-64_deb.tar.gz

tar -xzf LibreOffice_7.3.7.2_Linux_x86-64_deb.tar.gz

解压后会得到一个名为 LibreOffice_7.3.7.2_Linux_x86-64_deb 的文件夹,安装包位于其下的 DEBS/ 目录中。


 2. 创建并进入 Docker 容器环境

我们使用阿里云镜像加速的 Ubuntu 最新版作为基础环境。

# 拉取镜像(建议使用国内镜像源加速)
docker pull docker.xuanyuan.run/library/ubuntu:latest

# 启动容器,开启特权模式并以 root 用户运行
docker run --name office -itd --privileged=true --user root ubuntu:latest /bin/bash

--privileged=true:允许容器拥有更多权限(如挂载、系统调用),适用于复杂应用运行。


 3. 配置 APT 源并更新系统

进入容器后,更换为阿里云镜像源以提升下载速度(适用于 Ubuntu 24.04 Noble)。

# 进入容器
docker exec -it office /bin/bash

# 替换软件源为阿里云
cat << EOF > /etc/apt/sources.list
deb http://mirrors.aliyun.com/ubuntu/ noble main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ noble-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ noble-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ noble-backports main restricted universe multiverse

EOF

# 更新包列表
apt update

 4. 安装 LibreOffice

进入解压后的 DEBS 目录,安装所有 .deb 包:

# 安装所有 LibreOffice 组件
dpkg -i LibreOffice_7.3.7.2_Linux_x86-64_deb/DEBS/*.deb

⚠️ 若提示依赖错误,可执行:

apt install -f -y

🔧 5. 安装必要运行依赖

LibreOffice 图形和打印功能需要一些底层库支持:

apt install -y \
    libxinerama1 \
    libnss3 \
    libdbus-1-3 \
    libglib2.0-0 \
    libcups2 \
    libcairo2 \
    libsm6

 6. 安装中文字体(解决乱码问题)

文档中若含中文,需安装常用字体以避免显示异常或转 PDF 出现方框。

步骤一:将本地字体传入容器

# 将windows字体压缩 Fonts.zip 拷贝到容器中
docker cp ./Fonts.zip office:/opt/

步骤二:容器内操作

# 进入容器
docker exec -it office /bin/bash

cd /opt
unzip Fonts.zip -d Fonts

# 安装字体工具
apt install -y fontconfig

# 移动字体到系统目录
mv Fonts/* /usr/share/fonts/

# 重建字体缓存
fc-cache -fv


 7. 配置全局环境变量

为了让 soffice 命令全局可用,并支持 Python 宏、Java 扩展等,需设置环境变量。

cat << 'EOF' > /etc/profile

export JAVA_HOME=/opt/jdk1.8.0_451
export PATH=$JAVA_HOME/bin:$PATH

export PYTHONHOME=/opt/libreoffice7.3/program
export PATH=$PYTHONHOME:$PATH

export OOO_BASE_DIR=/opt/libreoffice7.3
export SOFFICE=$OOO_BASE_DIR/program/soffice
export PATH=$OOO_BASE_DIR/program:$PATH

EOF

📝 注意:'EOF' 使用单引号包裹,防止 $ 被 shell 提前解析。

加载配置:

source /etc/profile

 8. 验证 LibreOffice 是否正常运行

# 查看版本
soffice --version

# 启动无头模式(测试)
soffice --headless --accept="socket,port=2002;urp;" --norestore --nologo --nodefault &

如果无报错,说明安装成功!


 9. 提交为自定义镜像(可复用)

退出容器后,将其保存为新镜像

# 提交容器为镜像
docker commit office my-libreoffice:7.3.7.2

# 可选:推送到私有仓库
# docker tag my-libreoffice:7.3.7.2 your-registry/my-libreoffice:7.3.7.2
# docker push your-registry/my-libreoffice:7.3.7.2

后续可通过:

docker run -d --name office-server my-libreoffice:7.3.7.2 /bin/bash -c "source /etc/profile && soffice --headless ..."

快速部署。

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐