ppc84222
2017-06-30 06:49libreOffice将文档转换pdf的java代码
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;
}
- 点赞
- 回答
- 收藏
- 复制链接分享
1条回答
为你推荐
- Gobot OpenCV失败
- opencv
- 1个回答
- PHPExcel csv文件的computeWorksheetDimension
- php
- 1个回答
- LibreOffice将PDF转换为Word作为文本框而不是普通文档
- libreoffice
- php
- 1个回答
- PHP str_getcsv不会分隔索引1和2中的元素
- arrays
- csv
- php
- 1个回答
- PHP:使用utf8_encode时在csv中错误编码的字符
- mysql
- csv
- php
- 2个回答