|
|
|
@ -50,9 +50,9 @@ public class BatchExportServiceImpl implements BatchExportService {
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public String batchExportPdf(int taskId) throws Exception {
|
|
|
|
public String batchExportPdf(int taskId) throws Exception {
|
|
|
|
//是否存在目录不存在创建
|
|
|
|
//是否存在目录不存在创建
|
|
|
|
File file = new File (exportPdfPath);
|
|
|
|
File file = new File(exportPdfPath);
|
|
|
|
if(!file.isDirectory ()){
|
|
|
|
if (!file.isDirectory()) {
|
|
|
|
file.mkdirs ();
|
|
|
|
file.mkdirs();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//导出文件名
|
|
|
|
//导出文件名
|
|
|
|
String fileName = null;
|
|
|
|
String fileName = null;
|
|
|
|
@ -74,7 +74,7 @@ public class BatchExportServiceImpl implements BatchExportService {
|
|
|
|
} catch (ParseException e) {
|
|
|
|
} catch (ParseException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Boolean aBoolean = imageToPdf(filePathList, fileName,exportPdfPath);
|
|
|
|
Boolean aBoolean = imageToPdf(filePathList, fileName, exportPdfPath);
|
|
|
|
//为true时修改任务状态1完成2失败
|
|
|
|
//为true时修改任务状态1完成2失败
|
|
|
|
if (aBoolean) {
|
|
|
|
if (aBoolean) {
|
|
|
|
exportTaskDetailsMapper.upStatc(1, list.getId());
|
|
|
|
exportTaskDetailsMapper.upStatc(1, list.getId());
|
|
|
|
@ -88,83 +88,96 @@ public class BatchExportServiceImpl implements BatchExportService {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static Boolean imageToPdf(List<ExportDetailsVo> scanPathVos, String fileName, String exportPdfPath) {
|
|
|
|
public static Boolean imageToPdf(List<ExportDetailsVo> scanPathVos, String fileName,String exportPdfPath) {
|
|
|
|
ByteArrayOutputStream bos = null;
|
|
|
|
Document document = new Document(PageSize.A4, 0, 0, 0, 0); //创建文档容器
|
|
|
|
FileOutputStream fileOutputStream = null;
|
|
|
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
|
|
PdfWriter writer = null;
|
|
|
|
PdfWriter writer;
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
FileOutputStream fileOutputStream = new FileOutputStream(exportPdfPath+fileName+".pdf");
|
|
|
|
bos = new ByteArrayOutputStream();
|
|
|
|
writer = PdfWriter.getInstance(document,fileOutputStream);//创建编写器(PDF类型)
|
|
|
|
Document document = new Document(PageSize.A4, 0, 0, 0, 0); //创建文档容器
|
|
|
|
imgToPdf(document, writer, scanPathVos);
|
|
|
|
fileOutputStream = new FileOutputStream(exportPdfPath + fileName + ".pdf");
|
|
|
|
|
|
|
|
writer = PdfWriter.getInstance(document, fileOutputStream);//创建编写器(PDF类型)
|
|
|
|
|
|
|
|
//上一个目录名称
|
|
|
|
|
|
|
|
String lastOutline = null;
|
|
|
|
|
|
|
|
//是否增加标签
|
|
|
|
|
|
|
|
boolean outFlag = true;
|
|
|
|
|
|
|
|
//标签顺序
|
|
|
|
|
|
|
|
Integer outNum = 1;
|
|
|
|
|
|
|
|
// 添加目录
|
|
|
|
|
|
|
|
document.open(); //打开容器
|
|
|
|
|
|
|
|
float w, h;
|
|
|
|
|
|
|
|
PdfContentByte cb = writer.getDirectContent();
|
|
|
|
|
|
|
|
cb.setFontAndSize(BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 12);
|
|
|
|
|
|
|
|
cb.beginText();
|
|
|
|
|
|
|
|
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "目录", 300, 780, 0);
|
|
|
|
|
|
|
|
cb.endText();
|
|
|
|
|
|
|
|
// 创建目录
|
|
|
|
|
|
|
|
PdfOutline root = cb.getRootOutline();
|
|
|
|
|
|
|
|
// 添加图片到PDF
|
|
|
|
|
|
|
|
for (int i = 0; i < scanPathVos.size(); i++) {
|
|
|
|
|
|
|
|
String assortName = scanPathVos.get(i).getAssortName();
|
|
|
|
|
|
|
|
if (StringUtils.isNotBlank(lastOutline) && lastOutline.equals(assortName)) {
|
|
|
|
|
|
|
|
outFlag = false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (StringUtils.isBlank(lastOutline)) {
|
|
|
|
|
|
|
|
lastOutline = assortName;
|
|
|
|
|
|
|
|
outFlag = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!lastOutline.equals(assortName)) {
|
|
|
|
|
|
|
|
lastOutline = assortName;
|
|
|
|
|
|
|
|
outFlag = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
String imagePath = scanPathVos.get(i).getFilePath();
|
|
|
|
|
|
|
|
Image image = Image.getInstance(imagePath);
|
|
|
|
|
|
|
|
/*处理图片缩放比例*/
|
|
|
|
|
|
|
|
w = image.getWidth();
|
|
|
|
|
|
|
|
h = image.getHeight();
|
|
|
|
|
|
|
|
image.setAlignment(com.lowagie.text.Image.ALIGN_CENTER);
|
|
|
|
|
|
|
|
image.scaleAbsolute(PageSize.A4.getWidth(), PageSize.A4.getWidth() * (h / w));
|
|
|
|
|
|
|
|
// 创建新页面并添加图片
|
|
|
|
|
|
|
|
if (i != 0) {
|
|
|
|
|
|
|
|
document.newPage();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
document.add(image);
|
|
|
|
|
|
|
|
if (outFlag) {
|
|
|
|
|
|
|
|
//目录跳转页面内容设置。
|
|
|
|
|
|
|
|
PdfAction action = PdfAction.gotoLocalPage(i + 1, new PdfDestination(PdfDestination.FIT), writer);
|
|
|
|
|
|
|
|
//标题目录
|
|
|
|
|
|
|
|
String title = outNum + "." + scanPathVos.get(i).getAssortName();
|
|
|
|
|
|
|
|
new PdfOutline(root, action, title, false);
|
|
|
|
|
|
|
|
outNum++;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
document.close(); //关闭容器
|
|
|
|
} catch (Exception e) {
|
|
|
|
} catch (Exception e) {
|
|
|
|
ExceptionPrintUtil.printException(e);
|
|
|
|
ExceptionPrintUtil.printException(e);
|
|
|
|
e.printStackTrace();
|
|
|
|
e.printStackTrace();
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
} finally {
|
|
|
|
} finally {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
bos.flush();
|
|
|
|
if (writer != null) {
|
|
|
|
bos.close();
|
|
|
|
writer.close();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (fileOutputStream != null) {
|
|
|
|
|
|
|
|
fileOutputStream.close();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (bos != null) {
|
|
|
|
|
|
|
|
bos.flush();
|
|
|
|
|
|
|
|
bos.close();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
document.close();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void imgToPdf(Document document, PdfWriter writer, List<ExportDetailsVo> scanPathVos) throws Exception {
|
|
|
|
public static void imgToPdf(Document document, PdfWriter writer, List<ExportDetailsVo> scanPathVos) throws Exception {
|
|
|
|
//上一个目录名称
|
|
|
|
|
|
|
|
String lastOutline = null;
|
|
|
|
|
|
|
|
//是否增加标签
|
|
|
|
|
|
|
|
boolean outFlag = true;
|
|
|
|
|
|
|
|
//标签顺序
|
|
|
|
|
|
|
|
Integer outNum = 1;
|
|
|
|
|
|
|
|
// 添加目录
|
|
|
|
|
|
|
|
document.open(); //打开容器
|
|
|
|
|
|
|
|
PdfContentByte cb = writer.getDirectContent();
|
|
|
|
|
|
|
|
cb.setFontAndSize(BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 12);
|
|
|
|
|
|
|
|
cb.beginText();
|
|
|
|
|
|
|
|
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "目录", 300, 780, 0);
|
|
|
|
|
|
|
|
cb.endText();
|
|
|
|
|
|
|
|
// 创建目录
|
|
|
|
|
|
|
|
PdfOutline root = cb.getRootOutline();
|
|
|
|
|
|
|
|
// 添加图片到PDF
|
|
|
|
|
|
|
|
for (int i = 0; i < scanPathVos.size(); i++) {
|
|
|
|
|
|
|
|
String assortName = scanPathVos.get(i).getAssortName();
|
|
|
|
|
|
|
|
if (StringUtils.isNotBlank(lastOutline) && lastOutline.equals(assortName)) {
|
|
|
|
|
|
|
|
outFlag = false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (StringUtils.isBlank(lastOutline)) {
|
|
|
|
|
|
|
|
lastOutline = assortName;
|
|
|
|
|
|
|
|
outFlag=true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!lastOutline.equals(assortName)){
|
|
|
|
|
|
|
|
lastOutline = assortName;
|
|
|
|
|
|
|
|
outFlag=true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
String imagePath = scanPathVos.get(i).getFilePath();
|
|
|
|
|
|
|
|
Image image = Image.getInstance(imagePath);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
image.setAlignment(Image.MIDDLE);
|
|
|
|
|
|
|
|
image.scaleToFit(PageSize.A4.getWidth(), PageSize.A4.getHeight());
|
|
|
|
|
|
|
|
// 等比例缩放图片以填满PDF页面
|
|
|
|
|
|
|
|
image.scaleAbsolute(document.getPageSize());
|
|
|
|
|
|
|
|
// 创建新页面并添加图片
|
|
|
|
|
|
|
|
//document.newPage();
|
|
|
|
|
|
|
|
document.add(image);
|
|
|
|
|
|
|
|
if (outFlag) {
|
|
|
|
|
|
|
|
//目录跳转页面内容设置。
|
|
|
|
|
|
|
|
PdfAction action = PdfAction.gotoLocalPage(i+1, new PdfDestination(PdfDestination.FIT), writer);
|
|
|
|
|
|
|
|
//标题目录
|
|
|
|
|
|
|
|
String title = outNum + "." + scanPathVos.get(i).getAssortName();
|
|
|
|
|
|
|
|
new PdfOutline(root, action, title, false);
|
|
|
|
|
|
|
|
outNum++;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
document.close(); //关闭容器
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|