public String getLinuxOfficeHome() {
String osName = System.getProperty("os.name");
if (Pattern.matches("Linux.*", osName)) {
return "/opt/libreoffice5.3/program/soffice";
} else if (Pattern.matches("Windows.*", osName)) {
return "C:\LibreOffice\program\soffice.exe";
}
return null;
}
/**
* libreOffice调用命令启动及转换
* @param sourceFile
* @return
*/
public int libreOffice2PDF(String sourceFile) {
File inputFile = new File(sourceFile);
if (!inputFile.exists()) {
return -1;//文件不存在
}
String OpenOffice_HOME = getLinuxOfficeHome();
String path = sourceFile.substring(0,sourceFile.lastIndexOf(File.separator));
// 启动OpenOffice的服务
String command = OpenOffice_HOME + " --convert-to pdf:writer_pdf_Export --outdir "+path+" "+sourceFile;
Process pro = null;
log.error(command);
try {
pro = Runtime.getRuntime().exec(command);
InputStream in = pro.getErrorStream();
while (in.read() != -1) {
System.out.println(in.read());
}
in.close();
} catch (IOException e) {
e.printStackTrace();
return -2;
}
pro.destroy();
log.debug(sourceFile+":转换成功");
return 0;
}
http://blog.csdn.net/xinxin19881112/article/details/48681637