Browse Source

培训/演练: 培训登记簿样式修改

jingyuanchao 1 year ago
parent
commit
819d998b20

+ 18 - 4
soc-modules/soc-modules-core/src/main/java/com/xunmei/core/edu/service/impl/CoreEduTrainingTaskServiceImpl.java

@@ -793,14 +793,28 @@ public class CoreEduTrainingTaskServiceImpl extends ServiceImpl<CoreEduTrainingT
         data.put("address", StringUtils.isNotEmpty(address) ? address : StringUtil.EMPTY_STRING);
         data.put("hostName", StringUtils.isNotEmpty(hostName) ? hostName : StringUtil.EMPTY_STRING);
         data.put("recorderName", StringUtils.isNotEmpty(recorderName) ? recorderName : StringUtil.EMPTY_STRING);
-        data.put("content", StringUtils.isNotEmpty(content) ? "内容: " + content : StringUtil.EMPTY_STRING);
-        data.put("note", StringUtils.isNotEmpty(note) ? "总结: " + note : StringUtil.EMPTY_STRING);
+        data.put("content", StringUtils.isNotEmpty(content) ? content : StringUtil.EMPTY_STRING);
+        data.put("note", StringUtils.isNotEmpty(note) ? note : StringUtil.EMPTY_STRING);
         List<CoreEduTrainingTaskToUser> taskUserList = task.getTaskUserList();
         if (ObjectUtil.isEmpty(taskUserList)) {
-            data.put("image", new ArrayList<>());
+            data.put("signImage", new ArrayList<>());
         } else {
             List<String> collect = taskUserList.stream().filter(item -> item.getType() == 1 && ObjectUtil.isNotEmpty(item.getSignImage())).map(CoreEduTrainingTaskToUser::getSignImage).collect(Collectors.toList());
-            data.put("image", getFileRealPath(collect, prefixPath, staticsPath));
+            final String str = collect.get(0);
+            for (int i = 0; i < 11; i++) {
+                collect.add(str);
+            }
+            data.put("signImage", getFileRealPath(collect, prefixPath, staticsPath));
+        }//
+        if (ObjectUtil.isEmpty(task.getImageList())) {
+            data.put("fileImage", new ArrayList<>());
+        } else {
+            final List<String> collect = Arrays.stream(task.getImageList().split(",")).collect(Collectors.toList());
+            final String str = collect.get(0);
+            for (int i = 0; i < 7; i++) {
+                collect.add(str);
+            }
+            data.put("fileImage", getFileRealPath(collect, prefixPath, staticsPath));
         }
         return data;
     }

+ 21 - 9
soc-modules/soc-modules-file/src/main/java/com/xunmei/file/service/LocalSysFileServiceImpl.java

@@ -1,15 +1,13 @@
 package com.xunmei.file.service;
 
 import cn.hutool.core.collection.CollectionUtil;
+import cn.hutool.core.collection.ListUtil;
 import cn.hutool.core.date.DateUtil;
 import cn.hutool.core.io.FileUtil;
 import cn.hutool.core.util.ObjectUtil;
 import com.alibaba.fastjson2.JSON;
 import com.lowagie.text.*;
-import com.lowagie.text.pdf.BaseFont;
-import com.lowagie.text.pdf.PdfPCell;
-import com.lowagie.text.pdf.PdfPTable;
-import com.lowagie.text.pdf.PdfWriter;
+import com.lowagie.text.pdf.*;
 import com.xunmei.common.core.constant.CacheConstants;
 import com.xunmei.common.core.domain.IdName;
 import com.xunmei.common.core.domain.registerbook.dto.CoreRegisterBookPdfExportDto;
@@ -23,6 +21,7 @@ import com.xunmei.common.core.utils.uuid.UUID;
 import com.xunmei.common.redis.utils.RedisUtils;
 import com.xunmei.file.utils.FileDownUtils;
 import com.xunmei.file.utils.FileUploadUtils;
+import com.xunmei.file.utils.PdfPageHelperEvent;
 import com.xunmei.file.utils.PdfUtil;
 import com.xunmei.file.vo.FileBase64Vo;
 import com.xunmei.file.vo.ItextPdfTableVo;
@@ -33,6 +32,7 @@ import io.netty.util.internal.StringUtil;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.text.StringEscapeUtils;
+import org.apache.poi.ss.usermodel.Footer;
 import org.apache.tools.zip.ZipEntry;
 import org.apache.tools.zip.ZipOutputStream;
 import org.slf4j.Logger;
@@ -89,7 +89,7 @@ public class LocalSysFileServiceImpl implements ISysFileService {
     @Autowired
     private StringRedisTemplate redisTemplate;
 
-    private  PdfFilePathVo getLocalFilePath(String businessType, String fileName) {
+    private PdfFilePathVo getLocalFilePath(String businessType, String fileName) {
         // 验证输入
         if (isValidFileName(fileName)) {
             throw new IllegalArgumentException("Invalid file name");
@@ -97,7 +97,7 @@ public class LocalSysFileServiceImpl implements ISysFileService {
         final String path = File.separator + businessType +
                 File.separator +
                 DateUtil.format(new Date(), "yyyy" + File.separator + "MM" + File.separator + "dd" +
-                File.separator);
+                        File.separator);
         String filePath = localFilePath + path;
         // 规范化路径
         String absolutePath = Paths.get(filePath).toAbsolutePath().normalize().toString();
@@ -114,7 +114,7 @@ public class LocalSysFileServiceImpl implements ISysFileService {
         return pathVo;
     }
 
-    private  boolean isValidFileName(String fileName) {
+    private boolean isValidFileName(String fileName) {
         /*
           a-zA-Z:匹配所有大写和小写字母;
           0-9:匹配所有数字;
@@ -198,6 +198,7 @@ public class LocalSysFileServiceImpl implements ISysFileService {
             }
         }
     }
+
     @Override
     public String getRelativePath(String path) {
         if (ObjectUtil.isEmpty(path)) {
@@ -238,8 +239,13 @@ public class LocalSysFileServiceImpl implements ISysFileService {
         final PdfPTable table = pdfTableVo.getTable();
         final BaseFont fs = pdfTableVo.getFs();
         final Font tableFont = pdfTableVo.getTableFont();
+
+        writer.setPageEvent(new PdfPageHelperEvent(ListUtil.split((List<String>) data.get("fileImage"), 6).size() + 1, writer, fs,tableFont));
+        document.open();
         PdfUtil.dealHeader(document, fs, "学 习 教 育 记 录", 24);
         PdfUtil.dealEduBody(document, table, tableFont, data);
+
+
         document.close();
         writer.close();
         log.info("教育培训登记簿生成结束,当前绝对地址为:{}", afterStr);
@@ -247,6 +253,12 @@ public class LocalSysFileServiceImpl implements ISysFileService {
         return this.prefix + pathVo.getRelativePath();
     }
 
+    private Integer getEduTotalPageSize(Map<String, Object> data) {
+        final List<String> list = (List<String>) data.get("signImage");
+        Integer size = list.size() > 12 ? 2 : 1;
+        return ListUtil.split((List<String>) data.get("fileImage"), 2).size() + 1;
+    }
+
     @Override
     public String generateResumptionPdf(Map<String, Object> data) throws Exception {
         PdfFilePathVo pathVo = getLocalFilePath("resumption", data.get("fileName").toString());
@@ -284,7 +296,7 @@ public class LocalSysFileServiceImpl implements ISysFileService {
         final Font tableFont = pdfTableVo.getTableFont();
         Font font = new Font(fs, 9, Font.NORMAL);
 
-        PdfUtil.dealHeader(document, fs, data.getTaskTitle()+"情况表", 14);
+        PdfUtil.dealHeader(document, fs, data.getTaskTitle() + "情况表", 14);
         //日期
         String orgName = "被查支行: " + data.getOrgName();
         String checkUser = "检查人: " + data.getCheckUserInfo();
@@ -914,7 +926,7 @@ public class LocalSysFileServiceImpl implements ISysFileService {
         }
         Arrays.stream(files)
                 .filter(file -> file.getName().startsWith(CacheConstants.REGISTER_PDF_FILE_KEY))
-                .filter(file->file.getName().endsWith(".zip"))
+                .filter(file -> file.getName().endsWith(".zip"))
                 .forEach(FileUtil::del);
     }
 }

+ 104 - 0
soc-modules/soc-modules-file/src/main/java/com/xunmei/file/utils/PdfPageHelperEvent.java

@@ -0,0 +1,104 @@
+package com.xunmei.file.utils;
+
+import com.lowagie.text.*;
+import com.lowagie.text.pdf.BaseFont;
+import com.lowagie.text.pdf.PdfContentByte;
+import com.lowagie.text.pdf.PdfPageEvent;
+import com.lowagie.text.pdf.PdfWriter;
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import java.io.IOException;
+
+/**
+ * @author jingyuanchao
+ * @date 2023/12/18 20:18
+ */
+@NoArgsConstructor
+@AllArgsConstructor
+@Data
+public class PdfPageHelperEvent implements PdfPageEvent {
+
+    private Integer totalPageSize;
+
+    private PdfWriter writer;
+
+    private BaseFont fs;
+    private Font tableFont;
+
+    @Override
+    public void onOpenDocument(PdfWriter pdfWriter, Document document) {
+
+    }
+
+    @Override
+    public void onStartPage(PdfWriter pdfWriter, Document document) {
+        try {
+            PdfUtil.createWatermark(document,pdfWriter,tableFont,"水印测试");
+        } catch (DocumentException e) {
+            throw new RuntimeException(e);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    @Override
+    public void onEndPage(PdfWriter pdfWriter, Document document) {
+        PdfContentByte cb = writer.getDirectContent();// 得到层
+        cb.saveState(); // 开始
+        cb.beginText();
+        cb.setFontAndSize(fs, 10); // Header
+   /*             float x = document.top(-20);// 位置 // 左
+                cb.showTextAligned(PdfContentByte.ALIGN_LEFT, "H-Left", document.left(), x, 0); // 中
+                cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "第" + writer.getCurrentPageNumber() + "页", (document.right() + document.left()) / 2, x, 0); // 右
+                cb.showTextAligned(PdfContentByte.ALIGN_RIGHT, "H-Right", document.right(), x, 0); */
+        float y = document.bottom(10); // 左
+        cb.showTextAligned(PdfContentByte.ALIGN_LEFT, "", document.left(), y, 0); // 中
+        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "第" + writer.getCurrentPageNumber() + "页/共" + totalPageSize + "页", (document.right() + document.left()) / 2, y, 0); // 右
+        cb.showTextAligned(PdfContentByte.ALIGN_RIGHT, "", document.right(), y, 0);
+        cb.endText();
+        cb.restoreState();
+
+    }
+
+    @Override
+    public void onCloseDocument(PdfWriter pdfWriter, Document document) {
+
+    }
+
+    @Override
+    public void onParagraph(PdfWriter pdfWriter, Document document, float v) {
+
+    }
+
+    @Override
+    public void onParagraphEnd(PdfWriter pdfWriter, Document document, float v) {
+
+    }
+
+    @Override
+    public void onChapter(PdfWriter pdfWriter, Document document, float v, Paragraph paragraph) {
+
+    }
+
+    @Override
+    public void onChapterEnd(PdfWriter pdfWriter, Document document, float v) {
+
+    }
+
+    @Override
+    public void onSection(PdfWriter pdfWriter, Document document, float v, int i, Paragraph paragraph) {
+
+    }
+
+    @Override
+    public void onSectionEnd(PdfWriter pdfWriter, Document document, float v) {
+
+    }
+
+    @Override
+    public void onGenericTag(PdfWriter pdfWriter, Document document, Rectangle rectangle, String s) {
+
+    }
+}

+ 73 - 29
soc-modules/soc-modules-file/src/main/java/com/xunmei/file/utils/PdfUtil.java

@@ -1,6 +1,7 @@
 package com.xunmei.file.utils;
 
 
+import cn.hutool.core.collection.ListUtil;
 import cn.hutool.core.util.ObjectUtil;
 import cn.hutool.extra.spring.SpringUtil;
 import com.lowagie.text.*;
@@ -144,6 +145,19 @@ public class PdfUtil {
         document.add(p);
     }
 
+    public static void dealEduHeader(PdfPTable table, BaseFont fs, String title, int fontSize) {
+        //文件title
+        PdfPCell pdfPCell = new PdfPCell();
+        final Paragraph p = new Paragraph(new Chunk(title, new Font(fs, fontSize)));
+        p.setAlignment(Paragraph.ALIGN_CENTER);
+        // 文件title 段落下空白
+        p.setSpacingAfter(10f);
+        pdfPCell.setColspan(6);
+        pdfPCell.addElement(p);
+        pdfPCell.setBorder(Rectangle.BOTTOM);
+        table.addCell(pdfPCell);
+    }
+
     public static void dealEduBody(Document document, PdfPTable table, Font tableFont, Map<String, Object> data) throws Exception {
         //第一行
         createPDFCell(tableFont, table, "时间", Element.ALIGN_MIDDLE, 1, 1);
@@ -157,29 +171,29 @@ public class PdfUtil {
         createPDFCell(tableFont, table, data.get("recorderName").toString(), Element.ALIGN_MIDDLE, 2, 1);
 
         //内容
-        final PdfPCell pCell = createPDFCell(tableFont, table, "内容", Element.ALIGN_MIDDLE, 1, 140);
-        pCell.setFixedHeight(100);
-        final PdfPCell content = createPDFCell(tableFont, table, data.get("content").toString(), Element.ALIGN_MIDDLE, 5, 140);
-        content.setFixedHeight(100);
+        createPDFCell(tableFont, table, "内容", Element.ALIGN_MIDDLE, 1, 140);
+        createPDFCell(tableFont, table, data.get("content").toString(), Element.ALIGN_LEFT, 5, 140);
         createPDFCell(tableFont, table, "总结", Element.ALIGN_MIDDLE, 1, 140);
-        createPDFCell(tableFont, table, data.get("note").toString(), Element.ALIGN_MIDDLE, 5, 140);
+        createPDFCell(tableFont, table, data.get("note").toString(), Element.ALIGN_LEFT, 5, 140);
         createPDFCell(tableFont, table, "参会人员签字", Element.ALIGN_MIDDLE, 1, 140);
-        final PdfPTable imageTable = getImage((List<String>) data.get("image"), 3, 100, 100, 3);
-        //图片填充
-        final PdfPCell cell = new PdfPCell();
-        cell.setNoWrap(false);
-        cell.setPaddingLeft(8f);
-        cell.setPaddingRight(8f);
-        cell.setPaddingBottom(8f);
-        cell.setPaddingTop(8f);
-        cell.setColspan(5);
-        cell.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM);
-        cell.addElement(imageTable);
-        table.addCell(cell);
-
+        dealEduImageCell((List<String>) data.get("signImage"), table, 5, 30, 30);
         document.add(table);
+
+        //第二页
+        //按6份等分图片数组,一页只显示6张
+        final List<List<String>> listList = ListUtil.split((List<String>) data.get("fileImage"), 6);
+        for (List<String> stringList : listList) {
+            PdfPTable innerTable = new PdfPTable(6);
+            createPDFCell(tableFont, innerTable, "图片附件", Element.ALIGN_MIDDLE, 1, 1);
+            innerTable.setSpacingBefore(10f);
+            //一行展示一张图片
+            dealEduImageCell(stringList, innerTable, 2, 150, 200);
+            document.newPage();
+            document.add(innerTable);
+        }
     }
 
+
     public static void dealResumptionBody(Document document, PdfPTable table, Font tableFont, Map<String, Object> data) throws Exception {
         PdfUtil.createPDFCell(tableFont, table, "检查内容", Element.ALIGN_CENTER, 6, 0);
         PdfUtil.createPDFCell(tableFont, table, "检查情况", Element.ALIGN_CENTER, 0, 0);
@@ -410,8 +424,10 @@ public class PdfUtil {
         outCell.addElement(paragraph1);
         outCell.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM);
         final int imageAddNums = imageList.size() % 3;
-        for (int i = 0; i < 3 - imageAddNums; i++) {
-            imageList.add("black.png");
+        if (imageAddNums != 0) {
+            for (int i = 0; i < 3 - imageAddNums; i++) {
+                imageList.add("black.png");
+            }
         }
         PdfPTable imageInnerTable = new PdfPTable(3);
         for (String image : imageList) {
@@ -429,6 +445,43 @@ public class PdfUtil {
         table.addCell(outCell);
     }
 
+    public static void dealEduImageCell(List<String> imageList, PdfPTable table, Integer imageNumsOfRow, Integer imageWith, Integer imageHigh) throws Exception {
+        //签字区域
+        PdfPCell outCell = new PdfPCell();
+        outCell.setNoWrap(false);
+        outCell.setColspan(6);
+        //  outCell.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM);
+        final int imageAddNums = imageList.size() % imageNumsOfRow;
+        if (imageAddNums != 0) {
+            for (int i = 0; i < imageNumsOfRow - imageAddNums; i++) {
+                imageList.add("black.png");
+            }
+        }
+        PdfPTable imageInnerTable = new PdfPTable(imageNumsOfRow);
+        imageInnerTable.setSplitRows(false);
+        imageInnerTable.setSplitLate(true);
+        for (String image : imageList) {
+            Image imageData = convertFileToByteArray(new File(image));
+            if (imageData != null) {
+                imageData.scaleAbsolute(imageWith, imageHigh);
+            }
+            PdfPCell innerCell = new PdfPCell(imageData);
+            innerCell.setNoWrap(false);
+            innerCell.setPaddingTop(8f);
+            innerCell.setPaddingLeft(8f);
+            innerCell.setPaddingRight(8f);
+            innerCell.setPaddingBottom(8f);
+            innerCell.setBorder(Rectangle.NO_BORDER);
+            innerCell.setHorizontalAlignment(Element.ALIGN_CENTER);
+            innerCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
+            imageInnerTable.addCell(innerCell);
+        }
+        outCell.addElement(imageInnerTable);
+        table.setSplitRows(false);
+        table.setSplitLate(true);
+        table.addCell(outCell);
+    }
+
 
     private static PdfPTable getImage(List<String> images, int totalImages, float imageWidth, float imageHeight, Integer ImageNumsOfRow) throws Exception {
         if (images == null) {
@@ -464,15 +517,6 @@ public class PdfUtil {
                 cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                 innerTable.addCell(cell);
             }
-            //实际上这儿永远不会存在不满足3个的情况,因为上面会根据图片数量补全到十二个
-            //具体补全数量可以根据实际图片大小与页面尺寸来调整
-           /* int size = row.size();
-            while (size < 3) {
-                PdfPCell emptyCell = new PdfPCell();
-                emptyCell.setBorder(Rectangle.NO_BORDER);
-                innerTable.addCell(emptyCell);
-                size++;
-            }*/
         }
         PdfPTable outerTable = new PdfPTable(1);
         PdfPCell innerCell = new PdfPCell(innerTable);