Docker OpenOffice excel 转 pdf

Eric 2022年08月07日 1,524次浏览

Docker 安装 OpenOffice

OpenOffice 下载地址:https://www.openoffice.org/download/index.html

选择对应环境的安装包文件,下载 Download full installation

Dockerfile 如下:

FROM centos:7
MAINTAINER Eric

# 添加 openoffice 安装包
ADD Apache_OpenOffice_4.1.13_Linux_x86-64_install-rpm_zh-CN.tar.gz /opt/
# 添加字体,拷贝自 windows 环境下 C:/Windows/Fonts 目录文件
COPY ./fonts /usr/share/fonts

RUN cd /opt && \
    yum install -y zh-CN/RPMS/*.rpm && \
    yum install -y java-1.8.0-openjdk && \
    yum clean all && \
    chmod -R 755 /usr/share/fonts && \
    yum install mkfontscale fontconfig -y && \
    yum groupinstall "X Window System" -y && \
    mkfontscale && \
    mkfontdir && \
    fc-cache -f && \
    rm -rf zh-CN

EXPOSE 8100

CMD /opt/openoffice4/program/soffice -headless -nofirststartwizard -accept="socket,host=0.0.0.0,port=8100;urp;"

安装完成启动容器,映射8100端口即可;

该方式可与使用的服务单独分开部署,缺点是只支持office2003格式,网络有延时

Excel 转 Pdf

java code example:

        // host,port 为 OpenOffice 服务的地址
        OpenOfficeConnection connection = new SocketOpenOfficeConnection(host, port);
        connection.connect();
        // 转换word到pdf, OpenOffice网络服务注意使用 StreamOpenOfficeDocumentConverter
        DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);
        File in = new File("E:\\tmp\\report.xls");
        File out = new File("E:\\tmp\\report.pdf");
        converter.convert(in, out);
        // 关闭连接
        connection.disconnect()

错误信息 URL seems to be an unsupported one

com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException: conversion failed: could not load input document

	at com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter.loadAndExport(OpenOfficeDocumentConverter.java:131)
	at com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter.convertInternal(OpenOfficeDocumentConverter.java:120)
	at com.artofsolving.jodconverter.openoffice.converter.AbstractOpenOfficeDocumentConverter.convert(AbstractOpenOfficeDocumentConverter.java:104)
	at com.artofsolving.jodconverter.openoffice.converter.AbstractOpenOfficeDocumentConverter.convert(AbstractOpenOfficeDocumentConverter.java:74)
	at com.artofsolving.jodconverter.openoffice.converter.AbstractOpenOfficeDocumentConverter.convert(AbstractOpenOfficeDocumentConverter.java:70)
	at com.ht.zuozhu.Excel2PdfTest.openOfficeTest(Excel2PdfTest.java:32)
    ...
Caused by: com.sun.star.lang.IllegalArgumentException: URL seems to be an unsupported one.
	at com.sun.star.lib.uno.environments.remote.Job.remoteUnoRequestRaisedException(Job.java:182)
	at com.sun.star.lib.uno.environments.remote.Job.execute(Job.java:148)
	at com.sun.star.lib.uno.environments.remote.JobQueue.enter(JobQueue.java:344)
  • 问题原因:
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);

OpenOfficeDocumentConverter 仅支持本地部署,即openoffice和使用的程序在同一个机器

  • 文档注释:
File-based conversions are faster than stream-based ones (provided by StreamOpenOfficeDocumentConverter) but they require the OpenOffice.org service to be running locally and have the correct permissions to the files.
  • 解决方案:

修改为以下代码:

DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);

StreamOpenOfficeDocumentConverter 支持远程主机部署

  • 文档注释:
Stream-based conversions are slower than the default file-based ones (provided by OpenOfficeDocumentConverter) but they allow to run the OpenOffice.org service on a different machine, or under a different system user on the same machine without file permission problems.