一、libreOffice构建docker镜像,并启动项目
-
安装包准备
- LibreOffice(v7.4.2): LibreOffice_7.4.2_Linux_x86-64_rpm.tar.gz
- LibreOffice-langpack:(v7.4.2): LibreOffice_7.4.2_Linux_x86-64_rpm_langpack_zh-CN.tar.gz
- jdk-8u202-linux-x64.tar.gz
- 打包windows字体包(C:\Windows\Fonts\,搜索中文):fonts.tar.gz
-
Dockerfile编写
#基于centos7版本镜像 FROM centos:7 #以下设置中文语言环境与修改时区 ENV LANG=zh_CN.UTF-8 \ LANGUAGE=zh_CN:zh \ LC_ALL=zh_CN.UTF-8 RUN yum update -y && \ yum reinstall -y glibc-common && \ yum install -y telnet net-tools && \ yum clean all && \ rm -rf /tmp/* rm -rf /var/cache/yum/* && \ localedef -c -f UTF-8 -i zh_CN zh_CN.UTF-8 && \ ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime #加入windows字体包 ADD fonts.tar.gz /usr/share/fonts/ #将下载好的包解压到相应文件下 ADD LibreOffice_7.4.2_Linux_x86-64_rpm.tar.gz /home/ ADD LibreOffice_7.4.2_Linux_x86-64_rpm_langpack_zh-CN.tar.gz /usr/ #执行安装 注意解压后的名字跟压缩包的会不一样 RUN cd /home/LibreOffice_7.4.2.3_Linux_x86-64_rpm/RPMS/ \ && yum localinstall *.rpm -y \ && cd /usr/LibreOffice_7.4.2.3_Linux_x86-64_rpm_langpack_zh-CN/RPMS/ \ && yum localinstall *.rpm -y \ #安装依赖 && yum install ibus -y \ #加入中文字体支持并赋权限 && cd /usr/share/fonts/ \ && chmod -R 755 /usr/share/fonts \ && yum install mkfontscale -y \ && mkfontscale \ && yum install fontconfig -y \ && mkfontdir \ && fc-cache -fv \ && mkdir /usr/local/java/ \ #清理缓存,减少镜像大小 && yum clean all #加入安装java环境 ADD jdk-8u202-linux-x64.tar.gz /usr/local/java/ RUN ln -s /usr/local/java/jdk1.8.0_202/usr/local/java/jdk #配置环境变量 ENV JAVA_HOME /usr/local/java/jdk ENV JRE_HOME ${JAVA_HOME}/jre ENV CLASSPATH .:${JAVA_HOME}/lib:${JRE_HOME}/lib ENV PATH ${JAVA_HOME}/bin:$PATH #暴露端口 EXPOSE 8100 #运行 RUN cd /opt/libreoffice7.4/ \ && program/soffice --headless --accept="socket,host=0.0.0.0,port=8100;urp;" --nofirststartwizard & CMD ["bash"]
-
构建镜像
docker run --name libreoffice -v /opt/workingdir:/opt/libreoffice7.4/workingdir -p 8100:8100 -itd libreoffice:0.0.1
-
启动项目
docker run --name api-test --network net \ -p 8038:80 \ -v /var/run/docker.sock:/var/run/docker.sock \ -v /usr/bin/docker:/usr/bin/docker \ -v /usr/local/docker/oa-api/ROOT.jar:/usr/ROOT.jar \ -v /usr/local/docker/oa-api/logging:/usr/logging \ -v /opt/workingdir:/opt/workingdir \ -e TZ="Asia/Shanghai" \ -d java java -jar /usr/ROOT.jar \ --jodconverter.working-dir="/opt/workingdir"
二、代码
-
使用代码进入libreoffice容器转换文件
-
引用路径
// 引用路径 @Value("${jodconverter.working-dir}") private String workingDir;
-
转换文件
File sourceFile = new File(workingDir); if(!sourceFile.exists()) { sourceFile.mkdirs(); } IOUtils.copy(inputStream, new FileOutputStream(new File(workingDir + File.separator + prefix + "." + suffix))); String command = "docker exec libreoffice /opt/libreoffice7.4/program/soffice --convert-to pdf:writer_pdf_Export /opt/libreoffice7.4/workingdir/" + prefix + "." + suffix + " --outdir /opt/libreoffice7.4/workingdir"; boolean result = executeCommand(command); if(result) { IOUtils.copy(new FileInputStream(new File(workingDir + File.separator + prefix + "." + "pdf")), outputStream); }
public static boolean executeCommand(String command) { Process process; try { process = Runtime.getRuntime().exec(command); process.waitFor(); } catch (Exception e) { e.printStackTrace(); return false; } process.destroy(); return true; }
-