package com.sunflower.rm.web.utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
public class PDFUtil {
private static Logger logger = LoggerFactory.getLogger(PDFUtil.class);
/**
* breOfficeを使って、ExcelからPDFに変換する。
* @param inputFile
* @param pdfFile
* @return
*/
public static boolean convertOffice2PDF(String inputFile, String pdfFile) {
long start = System.currentTimeMillis();
String command;
boolean flag;
String osName = System.getProperty("os.name");
if (osName.contains("Windows")) {
command = "cmd /c start soffice --headless --invisible --convert-to pdf:writer_pdf_Export " + inputFile + " --outdir " + pdfFile;
} else {
command = "libreoffice --headless --invisible --convert-to pdf:writer_pdf_Export " + inputFile + " --outdir " + pdfFile;
}
flag = executeLibreOfficeCommand(command);
long end = System.currentTimeMillis();
logger.debug("{} msを掛かりました", end - start);
return flag;
}
/**
* コマンドを実施
* @param command
* @return
*/
public static boolean executeLibreOfficeCommand(String command) {
logger.info("変換開始.......");
Process process;
try {
logger.debug("convertOffice2PDF cmd : {}", command);
process = Runtime.getRuntime().exec(command);
} catch (IOException e) {
logger.error(" convertOffice2PDF {} error", command, e);
return false;
}
int exitStatus = 0;
try {
exitStatus = process.waitFor();
int i = process.exitValue();
logger.debug("i----" + i);
} catch (InterruptedException e) {
logger.error("InterruptedException convertOffice2PDF {}", command, e);
return false;
}
if (exitStatus != 0) {
logger.error("convertOffice2PDF cmd exitStatus {}", exitStatus);
} else {
logger.debug("convertOffice2PDF cmd exitStatus {}", exitStatus);
}
process.destroy();
logger.info("変換終了.......");
return true;
}
}
注意:要下载https://zh-cn.libreoffice.org/download/libreoffice/
二:
spire.xls第三方jar包