package com.ots.project.tool;
|
import lombok.extern.slf4j.Slf4j;
|
import java.io.*;
|
|
@Slf4j
|
public class ShellTool {
|
|
public static String execLibreofficeCommand(String type, String sourcePath, String targetPath) {
|
String os = System.getProperty("os.name");
|
StringBuilder builder = new StringBuilder();
|
if (os.toLowerCase().startsWith("mac")) {
|
builder.append("/Applications/LibreOffice.app/Contents/MacOS/soffice --headless --convert-to ");
|
} else if(os.contains("Windows")){
|
builder.append("cmd /c start soffice --headless --convert-to ");
|
} else {
|
builder.append("libreoffice6.4 --headless --convert-to ");
|
}
|
builder.append(type).append(":impress_pdf_Export ");
|
builder.append("--outdir " + targetPath).append(" ");
|
builder.append(sourcePath).append(" ");
|
|
try {
|
File file = new File(sourcePath.replaceAll(".docx", ".pdf"));
|
if (file.exists()) {
|
file.delete();
|
}
|
} catch (Exception e) {
|
}
|
return exec(builder.toString());
|
}
|
|
private static String exec(String command) {
|
String returnString = "";
|
Runtime runTime = Runtime.getRuntime();
|
if (runTime == null) {
|
System.err.println("Create runtime false!");
|
}
|
try {
|
Process pro = runTime.exec(command);
|
BufferedReader input = new BufferedReader(new InputStreamReader(pro.getInputStream()));
|
PrintWriter output = new PrintWriter(new OutputStreamWriter(pro.getOutputStream()));
|
String line;
|
while ((line = input.readLine()) != null) {
|
returnString = returnString + line + "\n";
|
}
|
input.close();
|
output.close();
|
pro.destroy();
|
} catch (IOException ex) {
|
log.error("执行PDF转换Shell失败:{}", ex.getMessage(), ex);
|
}
|
return returnString;
|
}
|
public static void main(String[] args) {
|
//System.out.println(exec("soffice.exe --headless --invisible --convert-to D:/ots/uploadPath/upload/2021/02/28/01e01cf7e372ba8b1c5d24b8d69a46f8.docx 01e01cf7e372ba8b1c5d24b8d69a46f8.pdf "));
|
}
|
}
|