使用OpenOffice4镜像容器中运行,将文档转为pdf,并解决中文代码加扰问题。
快速启动运行容器
docker pull xiaojun207/openoffice4-daemon:latest
docker run -d -u 123456 --name soffice -p 8100:8100 -v /data/:/data/ xiaojun207/openoffice4-daemon:latest
当您运行此映像时,默认情况下,容器将以daemon模式启动Openoffice守护进程,侦听TCP端口8100。若要更改此端口,请传递环境变量-e SOFFICE_DAEMON_PORT=8100
下面是java调用示例:
import java.io.File;
import java.io.FileNotFoundException;
import lombok.extern.slf4j.Slf4j;
import org.artofsolving.jodconverter.OfficeDocumentConverter;
import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
import org.artofsolving.jodconverter.office.OfficeManager;
...
public static void main(String[] args){
DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration();
configuration.setOfficeHome("/opt/openoffice4");
configuration.setPortNumbers(8100);
configuration.setTaskExecutionTimeout(1000 * 60 * 5L);// 设置任务执行超时为5分钟
configuration.setTaskQueueTimeout(1000 * 60 * 60 * 24L);// 设置任务队列超时为24小时
OfficeManager officeManager = configuration.buildOfficeManager();
String inputFile = "/data/a.docx";
String pdfFile = "/data/a.pdf"; // 注意输出目录的docker写入权限,否则可能会失败
System.out.println("进行文档转换转换:" + inputFile + " --> " + pdfFile + ",isRunning:" + officeManager.isRunning());
OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
converter.convert(new File(inputFile), new File(pdfFile));
}
注意:输出目录需要有写入权限,否则可能会失败
更多信息,请参考
https://hub.docker.com/repository/docker/xiaojun207/openoffice4-daemon
GitHub - xiaojun207/openoffice4-daemon: Image running the OpenOffice 4 soffice daemon service