
Java调用代码:
try {
String shpath = "/tmp/topdf.sh";//脚本路径,脚本中需要将源doc路径配置成变量,由下行程序调用时入参
String cmd = "param";//xshell脚本入参
Process ps = Runtime.getRuntime().exec(shpath,cmd);//
ps.waitFor();//等待转换结果
BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
StringBuffer sb = new StringBuffer();
String line;
while ((line = br.readLine()) != null) {
sb.append(line).append("\n");
}
String result = sb.toString();
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
#!/bin/sh source=$1 echo ${source} soffice --headless --invisible --convert-to pdf ${source} --outdir /tmp/
但发现运行一段时间命令不再响应,没有文档结果。经检查服务器,libreoffice运行良好,但soffice --convert-to 命令就是无响应无输出。查看活跃线程如下:
有两个相关的活跃实例在运行。
经翻阅资料,这是LO在2011年产生的一个bug,相关bug list 参考:
https://bugs.documentfoundation.org/show_bug.cgi?id=37531
https://bugs.documentfoundation.org/show_bug.cgi?id=45026
大体意思是LO GUI实例一旦运行过一个,再运行一个实例的话,就会出现无响应的问题。解决思路有两个:
1、杀掉所有的libreoffice实例,即将上面的两个实例杀掉
2、执行命令时增加一行参数,经测试下面两条命令均可以执行【必须保证命令的执行要有权限】:
soffice --headless --convert-to pdf ${source-file} --outdir ${target-path} "-env:UserInstallation=file:///tmp/LibreOffice_Conversion_${USER}"
soffice --headless --convert-to pdf ${source-file} --outdir ${target-path} -env:UserInstallation=file:///home/user/.libreoffice-alt
程序员,除了编码,生活还应该有沉淀!