规避mingganci

pull/1/head
liuwx_gitee 3 years ago
parent 612df609db
commit 812fa972fd

@ -38,7 +38,7 @@
版权声明 版权声明
----------------------------------- -----------------------------------
开源不代表免费,科亿遵循 GPL-3.0 开源协议发布,并提供技术交流学习,但 绝不允许修改后和衍生的代码做为闭源的商业软件发布和销售 如果需要将本产品在本地进行任何附带商业化性质行为使用, 请联系项目负责人进行商业授权 ,以遵守 GPL 协议保证您的正常使用。 开源不代表免费,科亿遵循 GPL-3.0 开源协议发布,并提供技术交流学习,但 绝不允许修改后和衍生的代码做为闭源的商业软件发布和销售 如果需要将本产品在本地进行任何附带商业化性质行为使用, 请联系项目负责人进行商业授权 ,以遵守 GPL 协议保证您的正常使用。
目前在国内 GPL 协议具备合同特征,是一种民事法律行为 属于我国《合同法》调整的范围。科亿项目团队保留一切诉讼权利。 科亿开源团队拥有对本开源协议的最终解释权。 目前在国内 GPL 协议具备合同特征,属于我国《合同法》调整的范围。科亿项目团队保留一切诉讼权利。 科亿开源团队拥有对本开源协议的最终解释权。
为什么选择KY KMS? 为什么选择KY KMS?

@ -2,90 +2,83 @@ package org.jeecg.modules.KM.common.utils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.slf4j.Logger; import org.apache.poi.hssf.usermodel.HSSFPrintSetup;
import org.slf4j.LoggerFactory; import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import java.io.File; import org.apache.poi.ss.usermodel.PrintSetup;
import java.io.IOException; import org.apache.poi.ss.usermodel.Sheet;
import java.io.InputStream; import org.apache.poi.xssf.usermodel.XSSFPrintSetup;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit;
/** /**
* *
* Title: OfficeUtils.java * Title: OfficeUtils.java
* *
* @author zxc * @author lwx
* @time 2018/6/29 5:20 * @time 2018/6/29 5:20
*/ */
@Slf4j @Slf4j
public class OfficeUtils { public class OfficeUtils {
// private static Logger log = LoggerFactory.getLogger(OfficeUtils.class); private static void formatExcel(String srcFile){
String fileExt = StringUtils.getFileSuffix(srcFile);
public static boolean convertPdf2(String sofficePath,File srcFile, File targetDir){ if ( fileExt != null ) {
///Applications/LibreOffice.app/Contents/MacOS/soffice try {
// --headless --invisible --convert-to if(fileExt.equals("xlsx")){
// html --outdir pdf-html/ *.pdf XSSFWorkbook wb = new XSSFWorkbook(Files.newInputStream(Paths.get(srcFile)));
Runtime runtime =Runtime.getRuntime(); for (int i = 0; i < wb.getNumberOfSheets(); i++) {
StringBuilder command = new StringBuilder(); XSSFSheet sheet = wb.getSheetAt(i);
command.append(sofficePath); //打印设置
String outputDir = targetDir.getAbsolutePath(); XSSFPrintSetup print = sheet.getPrintSetup();
command.append(" --headless --invisible --convert-to pdf --outdir "); print.setLandscape(true); // 打印方向true横向false纵向(默认)
command.append(outputDir); print.setFitHeight((short)0);//设置高度为自动分页
command.append(" "); print.setFitWidth((short)1);//设置宽度为一页
command.append(srcFile.getAbsolutePath()); print.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE); //纸张类型
log.info("开始转换pdf,命令行是:{}",command.toString()); // print.setScale((short)55);//自定义缩放①此处100为无缩放
try { //启用“适合页面”打印选项的标志
sheet.setFitToPage(true);
ProcessBuilder pb = new ProcessBuilder(command.toString()); }
pb.redirectErrorStream(true); File file = new File(srcFile);
Process process = pb.start(); OutputStream fos = new FileOutputStream(file);
printStream(process.getInputStream()); wb.write(fos);
//Process process = runtime.exec(command.toString()); }else if(fileExt.equals("xls")){
// printStream(process.getInputStream()); POIFSFileSystem fs = new POIFSFileSystem(Files.newInputStream(Paths.get(srcFile)));
// printStream(process.getErrorStream()); HSSFWorkbook wb = new HSSFWorkbook(fs);
int code = process.waitFor();
log.info("code:{}",code); for (int i = 0; i < wb.getNumberOfSheets(); i++) {
Sheet sheet = wb.getSheetAt(i);
//String result = IOUtils.toString(process.getInputStream(),"utf-8"); //打印设置
//String errorResult = IOUtils.toString(process.getErrorStream(),"utf-8"); PrintSetup print = sheet.getPrintSetup();
//log.info("convertPdf from {} output {}:\n result:{}\n",srcFile.getAbsolutePath(),outputDir,result); print.setLandscape(true); // 打印方向true横向false纵向(默认)
// if(StringUtil.isNotEmpty(errorResult)){ print.setFitHeight((short)0);//设置高度为自动分页
// log.error("convertPdf from {} output {}:\n errorResult:{}\n",srcFile.getAbsolutePath(),outputDir,errorResult); print.setFitWidth((short)1);//设置宽度为一页
// } print.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE); //纸张类型
File targetFile = new File(targetDir,StringUtils.getFileNameWithoutSuffix(srcFile.getName())+".pdf"); //启用“适合页面”打印选项的标志
log.info("目标文件是:{}",targetFile.getAbsolutePath()); sheet.setFitToPage(true);
if(targetFile.exists()){ }
log.info("文档转化成功"); File file = new File(srcFile);
return true; OutputStream fos = new FileOutputStream(file);
}else { wb.write(fos);
log.info("文档转化失败"); }
return false; } catch (IOException e) {
throw new RuntimeException(e);
} }
// if(code==0){
// log.info("文档转化成功");
// return true;
// }else {
// log.info("文档转化失败");
// return false;
// }
} catch (IOException e) {
log.error("convertPdf",e);
} catch (InterruptedException e) {
log.error("convertPdf",e);
} }
return false;
} }
public static boolean convertPdf(String sofficePath,File srcFile, File targetDir){ public static boolean convertPdf(String sofficePath,File srcFile, File targetDir){
///Applications/LibreOffice.app/Contents/MacOS/soffice //设置excel文件格式为不换行折断
// --headless --invisible --convert-to formatExcel(srcFile.getAbsolutePath());
// html --outdir pdf-html/ *.pdf
//Thread.sleep();
List<String>commands=new ArrayList<>(); List<String>commands=new ArrayList<>();
String outputDir = targetDir.getAbsolutePath(); String outputDir = targetDir.getAbsolutePath();
commands.add(sofficePath); commands.add(sofficePath);
@ -103,40 +96,31 @@ public class OfficeUtils {
pb.redirectErrorStream(true); pb.redirectErrorStream(true);
Process process = pb.start(); Process process = pb.start();
String result = IOUtils.toString(process.getInputStream(),"utf-8"); String result = IOUtils.toString(process.getInputStream(),"utf-8");
log.info("convertPdf from {} output {}:\n result:{}\n",srcFile.getAbsolutePath(),outputDir,result); boolean exitFlag = process.waitFor(10, TimeUnit.SECONDS);
//printStream(process.getInputStream()); if(exitFlag) {
//Process process = runtime.exec(command.toString()); int code = process.exitValue();
// printStream(process.getInputStream());
// printStream(process.getErrorStream()); log.info("convertPdf from {} output: {}\n result:{}\n exitValue:{}\n", srcFile.getAbsolutePath(), outputDir, result, code);
//int code = process.waitFor();
//log.info("code:{}",code); process.destroy();
process.destroy();
//String result = IOUtils.toString(process.getInputStream(),"utf-8"); File targetFile = new File(targetDir, StringUtils.getFileNameWithoutSuffix(srcFile.getName()) + ".pdf");
//String errorResult = IOUtils.toString(process.getErrorStream(),"utf-8"); if (targetFile.exists()) {
//log.info("convertPdf from {} output {}:\n result:{}\n",srcFile.getAbsolutePath(),outputDir,result); log.info("文档转化成功,{},目标文件是:{}", srcFile.getAbsolutePath(), targetFile.getAbsolutePath());
// if(StringUtil.isNotEmpty(errorResult)){ return true;
// log.error("convertPdf from {} output {}:\n errorResult:{}\n",srcFile.getAbsolutePath(),outputDir,errorResult); } else {
// } log.info("文档转化失败,{},目标文件是:{}", srcFile.getAbsolutePath(), targetFile.getAbsolutePath());
File targetFile = new File(targetDir,StringUtils.getFileNameWithoutSuffix(srcFile.getName())+".pdf"); return false;
log.info("目标文件是:{}",targetFile.getAbsolutePath()); }
if(targetFile.exists()){ }
log.info("文档转化成功"); else {
return true; log.info("等待命令退出失败");
}else {
log.info("文档转化失败");
return false; return false;
} }
// if(code==0){
// log.info("文档转化成功");
// return true;
// }else {
// log.info("文档转化失败");
// return false;
// }
} catch (IOException e) { } catch (IOException e) {
log.error("convertPdf",e); log.error("convertPdf IOException:",e);
}catch (InterruptedException e) {
log.error("convertPdf InterruptedException:",e);
} }
return false; return false;
} }
@ -156,6 +140,4 @@ public class OfficeUtils {
} }
}.start(); }.start();
} }
} }

Loading…
Cancel
Save