wangliang 4 gadi atpakaļ
vecāks
revīzija
38bbc05095

+ 7 - 6
distributed-print-business/src/main/java/com/qmth/distributed/print/business/entity/ExamStudent.java

@@ -45,20 +45,21 @@ public class ExamStudent extends BaseEntity implements Serializable {
     @TableField("paper_type")
     private String paperType;
 
+    @JsonSerialize(using = ToStringSerializer.class)
     @ApiModelProperty(value = "附件id")
-    @TableField(value = "attachment_ids")
-    private String attachmentIds;
+    @TableField(value = "attachment_id")
+    private Long attachmentId;
 
     public static long getSerialVersionUID() {
         return serialVersionUID;
     }
 
-    public String getAttachmentIds() {
-        return attachmentIds;
+    public Long getAttachmentId() {
+        return attachmentId;
     }
 
-    public void setAttachmentIds(String attachmentIds) {
-        this.attachmentIds = attachmentIds;
+    public void setAttachmentId(Long attachmentId) {
+        this.attachmentId = attachmentId;
     }
 
     public Long getSchoolId() {

+ 2 - 1
distributed-print-business/src/main/java/com/qmth/distributed/print/business/service/BasicAttachmentService.java

@@ -45,10 +45,11 @@ public interface BasicAttachmentService extends IService<BasicAttachment> {
      * @param fileName
      * @param htmlContent
      * @param userId
+     * @param localFileList
      * @return
      * @throws IOException
      */
-    public BasicAttachment saveAttachmentHtml(String fileName, String htmlContent, Long userId) throws IOException;
+    public BasicAttachment saveAttachmentHtml(String fileName, String htmlContent, Long userId, List<String> localFileList) throws IOException;
 
     /**
      * 删除附件

+ 21 - 0
distributed-print-business/src/main/java/com/qmth/distributed/print/business/service/CommonService.java

@@ -8,6 +8,8 @@ import com.qmth.distributed.print.business.entity.SysUser;
 import com.qmth.distributed.print.business.entity.SysUserRole;
 import com.qmth.distributed.print.business.enums.PrivilegePropertyEnum;
 
+import java.io.IOException;
+import java.io.InputStream;
 import java.security.NoSuchAlgorithmException;
 import java.util.List;
 
@@ -93,8 +95,27 @@ public interface CommonService {
 
     /**
      * 题卡html文件上传,读取文件内容
+     *
      * @param path
      * @return
      */
     public String readFileContent(String path);
+
+    /**
+     * 获取文件流
+     *
+     * @param path
+     * @param pdf
+     * @return
+     */
+    public InputStream getFileInputStream(String path, boolean pdf) throws IOException;
+
+    /**
+     * 获取文件
+     *
+     * @param path
+     * @param pdf
+     * @return
+     */
+    public String getFile(String path, boolean pdf) throws IOException;
 }

+ 25 - 5
distributed-print-business/src/main/java/com/qmth/distributed/print/business/service/impl/BasicAttachmentServiceImpl.java

@@ -8,9 +8,11 @@ import com.qmth.boot.api.exception.ApiException;
 import com.qmth.distributed.print.business.config.DictionaryConfig;
 import com.qmth.distributed.print.business.entity.BasicAttachment;
 import com.qmth.distributed.print.business.entity.SysUser;
+import com.qmth.distributed.print.business.enums.PageSizeEnum;
 import com.qmth.distributed.print.business.enums.UploadFileEnum;
 import com.qmth.distributed.print.business.mapper.BasicAttachmentMapper;
 import com.qmth.distributed.print.business.service.BasicAttachmentService;
+import com.qmth.distributed.print.business.util.HtmlToPdfUtil;
 import com.qmth.distributed.print.business.util.OssUtil;
 import com.qmth.distributed.print.business.util.ServletUtil;
 import com.qmth.distributed.print.common.contant.SystemConstant;
@@ -29,10 +31,7 @@ import org.springframework.util.FileCopyUtils;
 import org.springframework.web.multipart.MultipartFile;
 
 import javax.annotation.Resource;
-import java.io.ByteArrayInputStream;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
+import java.io.*;
 import java.math.BigDecimal;
 import java.time.LocalDateTime;
 import java.util.List;
@@ -92,12 +91,13 @@ public class BasicAttachmentServiceImpl extends ServiceImpl<BasicAttachmentMappe
      * @param fileName
      * @param htmlContent
      * @param userId
+     * @param localFileList
      * @return
      * @throws IOException
      */
     @Override
     @Transactional
-    public BasicAttachment saveAttachmentHtml(String fileName, String htmlContent, Long userId) throws IOException {
+    public BasicAttachment saveAttachmentHtml(String fileName, String htmlContent, Long userId, List<String> localFileList) throws IOException {
         BasicAttachment basicAttachment = null;
         try {
             byte[] bytes = htmlContent.getBytes();
@@ -123,6 +123,16 @@ public class BasicAttachmentServiceImpl extends ServiceImpl<BasicAttachmentMappe
                 fileMd5 = DigestUtils.md5Hex(new ByteArrayInputStream(data));
                 jsonObject.put(SystemConstant.TYPE, SystemConstant.OSS);
                 jsonObject.put(SystemConstant.PATH, dirName);
+                String url = SystemConstant.TEMP_FILES_DIR + File.separator + dirName;
+                File localHtmlFile = ossUtil.ossDownload(dirName, url);
+                String pdfDirName = dirName.replaceAll(SystemConstant.HTML_PREFIX, SystemConstant.PDF_PREFIX).replaceAll(UploadFileEnum.HTML.name().toLowerCase(), UploadFileEnum.PDF.name().toLowerCase());
+                String destUrl = SystemConstant.PDF_TEMP_FILES_DIR + File.separator + pdfDirName;
+                HtmlToPdfUtil.convert(localHtmlFile.getPath(), destUrl, PageSizeEnum.A3);
+                File pdf = new File(destUrl);
+                localFileList.add(pdf.getPath());
+                ossUtil.ossUpload(pdfDirName, pdf, BinaryUtil.toBase64String(HexUtils.decodeHex(DigestUtils.md5Hex(new FileInputStream(pdf)))));
+                localHtmlFile.delete();
+                jsonObject.put(SystemConstant.PDF_PATH, pdfDirName);
             } else {//上传至服务器
                 File finalFile = new File(stringJoiner.toString());
                 if (!finalFile.exists()) {
@@ -133,6 +143,9 @@ public class BasicAttachmentServiceImpl extends ServiceImpl<BasicAttachmentMappe
                 fileMd5 = DigestUtils.md5Hex(new FileInputStream(stringJoiner.toString()));
                 jsonObject.put(SystemConstant.TYPE, SystemConstant.LOCAL);
                 jsonObject.put(SystemConstant.PATH, stringJoiner.toString());
+                String destUrl = finalFile.getPath().replaceAll(SystemConstant.HTML_PREFIX, SystemConstant.PDF_PREFIX).replaceAll(UploadFileEnum.HTML.name().toLowerCase(), UploadFileEnum.PDF.name().toLowerCase());
+                HtmlToPdfUtil.convert(finalFile.getPath(), destUrl, PageSizeEnum.A4);
+                jsonObject.put(SystemConstant.PDF_PATH, destUrl);
             }
             jsonObject.put(SystemConstant.UPLOAD_TYPE, UploadFileEnum.HTML);
             basicAttachment = new BasicAttachment(jsonObject.toJSONString(), fileName, SystemConstant.HTML_PREFIX, new BigDecimal(size), fileMd5, userId);
@@ -184,9 +197,16 @@ public class BasicAttachmentServiceImpl extends ServiceImpl<BasicAttachmentMappe
             String type = String.valueOf(jsonObject.get(SystemConstant.TYPE));
             if (Objects.nonNull(type) && Objects.equals(type, SystemConstant.OSS)) {//删除阿里云附件
                 ossUtil.ossDelete(jsonObject.get(SystemConstant.PATH).toString());
+                if (Objects.nonNull(jsonObject.get(SystemConstant.PDF_PATH))) {
+                    ossUtil.ossDelete(jsonObject.get(SystemConstant.PDF_PATH).toString());
+                }
             } else {//删除服务器附件
                 File file = new File(jsonObject.get(SystemConstant.PATH).toString());
                 file.delete();
+                if (Objects.nonNull(jsonObject.get(SystemConstant.PDF_PATH))) {
+                    file = new File(jsonObject.get(SystemConstant.PDF_PATH).toString());
+                    file.delete();
+                }
             }
         }
     }

+ 46 - 0
distributed-print-business/src/main/java/com/qmth/distributed/print/business/service/impl/CommonServiceImpl.java

@@ -353,4 +353,50 @@ public class CommonServiceImpl implements CommonService {
         }
         return null;
     }
+
+    /**
+     * 获取文件流
+     *
+     * @param path
+     * @param pdf
+     * @return
+     */
+    @Override
+    public InputStream getFileInputStream(String path, boolean pdf) throws IOException {
+        InputStream inputStream = null;
+        JSONObject jsonObject = JSONObject.parseObject(path);
+        String attachmentType = (String) jsonObject.get(SystemConstant.TYPE);
+        String filePath = pdf ? (String) jsonObject.get(SystemConstant.PDF_PATH) : (String) jsonObject.get(SystemConstant.PATH);
+        if (Objects.equals(attachmentType, SystemConstant.OSS)) {
+            byte[] data = ossUtil.ossDownload(filePath);
+            inputStream = new ByteArrayInputStream(data);
+        } else {
+            StringJoiner localPath = new StringJoiner("").add(SystemConstant.TEMP_FILES_DIR).add(File.separator).add(filePath);
+            inputStream = new FileInputStream(new File(localPath.toString()));
+        }
+        return inputStream;
+    }
+
+    /**
+     * 获取文件
+     *
+     * @param path
+     * @param pdf
+     * @return
+     * @throws IOException
+     */
+    @Override
+    public String getFile(String path, boolean pdf) throws IOException {
+        File file = null;
+        JSONObject jsonObject = JSONObject.parseObject(path);
+        String attachmentType = (String) jsonObject.get(SystemConstant.TYPE);
+        String filePath = pdf ? (String) jsonObject.get(SystemConstant.PDF_PATH) : (String) jsonObject.get(SystemConstant.PATH);
+        StringJoiner localPath = new StringJoiner("").add(SystemConstant.TEMP_FILES_DIR).add(File.separator).add(filePath);
+        if (Objects.equals(attachmentType, SystemConstant.OSS)) {
+            file = ossUtil.ossDownload(filePath, localPath.toString());
+        } else {
+            file = new File(localPath.toString());
+        }
+        return file.getPath();
+    }
 }

+ 2 - 2
distributed-print-business/src/main/java/com/qmth/distributed/print/business/templete/execute/AsyncCreatePdfTempleteService.java

@@ -66,8 +66,8 @@ public class AsyncCreatePdfTempleteService extends AsyncCreateTaskTemplete {
                 ResultUtil.error(e.getMessage());
             }
         } finally {//生成txt文件
-            tbTask.setSummary(stringJoinerSummary.toString());
-            super.createTxt(tbTask);
+//            tbTask.setSummary(stringJoinerSummary.toString());
+//            super.createTxt(tbTask);
         }
         return ResultUtil.ok(map);
     }

+ 53 - 11
distributed-print-business/src/main/java/com/qmth/distributed/print/business/templete/service/impl/TaskLogicServiceImpl.java

@@ -2,21 +2,27 @@ package com.qmth.distributed.print.business.templete.service.impl;
 
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
+import com.aliyun.oss.common.utils.BinaryUtil;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.google.gson.Gson;
 import com.qmth.boot.api.exception.ApiException;
 import com.qmth.distributed.print.business.bean.dto.excel.UserImportDto;
 import com.qmth.distributed.print.business.entity.*;
 import com.qmth.distributed.print.business.enums.ExamStatusEnum;
+import com.qmth.distributed.print.business.enums.UploadFileEnum;
 import com.qmth.distributed.print.business.service.*;
 import com.qmth.distributed.print.business.templete.service.TaskLogicService;
 import com.qmth.distributed.print.business.util.GoogleBarCodeUtil;
+import com.qmth.distributed.print.business.util.OssUtil;
+import com.qmth.distributed.print.business.util.PdfUtil;
 import com.qmth.distributed.print.business.util.ServletUtil;
 import com.qmth.distributed.print.business.util.excel.ExcelError;
 import com.qmth.distributed.print.common.contant.SystemConstant;
 import com.qmth.distributed.print.common.enums.ExceptionResultEnum;
 import com.qmth.distributed.print.common.util.Base64Util;
+import com.qmth.distributed.print.common.util.HexUtils;
 import com.qmth.distributed.print.common.util.ResultUtil;
+import org.apache.commons.codec.digest.DigestUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Service;
@@ -24,7 +30,11 @@ import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.LinkedMultiValueMap;
 
 import javax.annotation.Resource;
+import java.io.File;
+import java.io.FileInputStream;
 import java.io.IOException;
+import java.io.InputStream;
+import java.math.BigDecimal;
 import java.util.*;
 import java.util.stream.Collectors;
 
@@ -75,6 +85,9 @@ public class TaskLogicServiceImpl implements TaskLogicService {
     @Resource
     BasicAttachmentService basicAttachmentService;
 
+    @Resource
+    OssUtil ossUtil;
+
     /**
      * 执行导入用户逻辑
      *
@@ -167,7 +180,11 @@ public class TaskLogicServiceImpl implements TaskLogicService {
             examDetailCourseQueryWrapper.lambda().eq(ExamDetailCourse::getExamDetailId, examDetail.getId());
             List<ExamDetailCourse> examDetailCourseList = detailCourseService.list(examDetailCourseQueryWrapper);
 
+            StringJoiner stringJoiner = new StringJoiner("").add(SystemConstant.PDF_TEMP_FILES_DIR).add(File.separator);
             for (ExamDetailCourse examDetailCourse : examDetailCourseList) {
+                List<String> paperPdfList = new ArrayList<>();
+                List<String> cardPdfList = new ArrayList<>();
+                List<String> studentPdfList = new ArrayList<>();
                 //查询试卷
                 QueryWrapper<ExamTask> examTaskQueryWrapper = new QueryWrapper<>();
                 examTaskQueryWrapper.lambda().eq(ExamTask::getSchoolId, sysUser.getSchoolId())
@@ -199,6 +216,16 @@ public class TaskLogicServiceImpl implements TaskLogicService {
                     List<ExamCardDetail> examCardDetailList = examCardDetailService.list(examCardDetailQueryWrapper);
 
                     String[] paperTypes = examTaskDetail.getPaperType().split("/");
+                    JSONObject jsonObjectPaper = JSONObject.parseObject(examTaskDetail.getPaperAttachmentIds());
+                    JSONArray jsonArrayPaper = jsonObjectPaper.getJSONArray("paper");
+                    for (int i = 0; i < jsonArrayPaper.size(); i++) {
+                        JSONObject object = (JSONObject) jsonArrayPaper.get(i);
+                        if (Objects.nonNull(object.get("attachmentId"))) {
+                            Long attachmentId = Long.parseLong(String.valueOf(object.get("attachmentId")));
+                            BasicAttachment basicAttachment = basicAttachmentService.getById(attachmentId);
+                            paperPdfList.add(commonService.getFile(basicAttachment.getPath(), false));
+                        }
+                    }
                     basicAttachmentList = Objects.isNull(basicAttachmentList) ? basicAttachmentList = new ArrayList<>() : basicAttachmentList;
                     for (ExamCardDetail examCardDetail : examCardDetailList) {
                         if (Objects.nonNull(examCardDetail.getAttachmentId())) {
@@ -207,7 +234,7 @@ public class TaskLogicServiceImpl implements TaskLogicService {
                             for (int i = 0; i < jsonArrayCard.size(); i++) {
                                 JSONObject object = (JSONObject) jsonArrayCard.get(i);
                                 if (Objects.nonNull(object.get("attachmentId"))) {
-                                    attachmentIds.add((Long) object.get("attachmentId"));
+                                    attachmentIds.add(Long.parseLong(String.valueOf(object.get("attachmentId"))));
                                 }
                             }
                         }
@@ -232,7 +259,7 @@ public class TaskLogicServiceImpl implements TaskLogicService {
                                 String paperCodeImg = GoogleBarCodeUtil.createBarCode(paperCode, false);
                                 String paperCodeDiv = "<div class=\"page-box page-box-0\"><div class=\"package-number\" style=\"position: absolute;width: 200px;height: 40px;top: 80px;right: 35px;transform: rotate(-90deg);transform-origin: center right;text-align: center;z-index: 99;\"><img src=\"data:image/png;base64," + paperCodeImg + "\" style=\"display: block; height: 28px; width: 100%\" /><p style=\"line-height: 1; font-size: 12px; margin: 0;\">" + paperCode + "</p></div>";
                                 cardTemp = cardTemp.replaceAll("<div class=\"page-box page-box-0\">", paperCodeDiv);
-                                BasicAttachment cardAttachment = basicAttachmentService.saveAttachmentHtml(examCard.getSchoolId() + "|" + examCard.getCourseCode(), cardTemp, sysUser.getId());
+                                BasicAttachment cardAttachment = basicAttachmentService.saveAttachmentHtml(examCard.getSchoolId() + "|" + examCard.getCourseCode(), cardTemp, sysUser.getId(), cardPdfList);
                                 JSONObject object = new JSONObject();
                                 object.put("name", paperType);
                                 object.put("examTaskDetailId", examTaskDetail.getId());
@@ -248,10 +275,8 @@ public class TaskLogicServiceImpl implements TaskLogicService {
                             List<ExamStudent> examStudentList = examStudentService.list(examStudentQueryWrapper);
 
                             for (ExamStudent t : examStudentList) {
-                                if (Objects.nonNull(t.getAttachmentIds())) {
-                                    JSONObject json = JSONObject.parseObject(t.getAttachmentIds());
-                                    attachmentIds.add((Long) json.get("htmlAttachmentId"));
-//                                    attachmentIds.add((Long) json.get("pdfAttachmentId"));
+                                if (Objects.nonNull(t.getAttachmentId())) {
+                                    attachmentIds.add(t.getAttachmentId());
                                 }
                                 String studentHtml = studentContent;
                                 if (Objects.nonNull(t.getExtendFields())) {
@@ -274,11 +299,8 @@ public class TaskLogicServiceImpl implements TaskLogicService {
                                 studentHtml = studentHtml.replaceAll("\\$\\{courseName\\}", examCard.getCourseName());
 
                                 //学生题卡
-                                BasicAttachment examStudentAttachment = basicAttachmentService.saveAttachmentHtml(examCard.getSchoolId() + "|" + examCard.getCourseCode() + "|" + t.getTicketNumber(), studentHtml, sysUser.getId());
-                                JSONObject jsonObjectExamStudent = new JSONObject();
-                                jsonObjectExamStudent.put("htmlAttachmentId", examStudentAttachment.getId());
-                                jsonObjectExamStudent.put("pdfAttachmentId", 123L);
-                                t.setAttachmentIds(jsonObjectExamStudent.toJSONString());
+                                BasicAttachment examStudentAttachment = basicAttachmentService.saveAttachmentHtml(examCard.getSchoolId() + "|" + examCard.getCourseCode() + "|" + t.getTicketNumber(), studentHtml, sysUser.getId(), studentPdfList);
+                                t.setAttachmentId(examStudentAttachment.getId());
                                 basicAttachmentList.add(examStudentAttachment);
                             }
                             examStudentService.saveOrUpdateBatch(examStudentList);
@@ -287,6 +309,26 @@ public class TaskLogicServiceImpl implements TaskLogicService {
                         examCardDetail.setAttachmentId(jsonObject.toJSONString());
                     }
                     examCardDetailService.saveOrUpdateBatch(examCardDetailList);
+                    List<String> mergePdfList = new ArrayList<>();
+                    mergePdfList.addAll(paperPdfList);
+                    mergePdfList.addAll(cardPdfList);
+                    mergePdfList.addAll(studentPdfList);
+                    String dirName = PdfUtil.mergePdf(mergePdfList.toArray(new String[mergePdfList.size()]), null);
+                    File localPdfFile = new File(stringJoiner.toString() + File.separator + dirName);
+                    JSONObject jsonPdf = new JSONObject();
+                    jsonPdf.put(SystemConstant.TYPE, SystemConstant.OSS);
+                    jsonPdf.put(SystemConstant.PATH, dirName);
+                    jsonPdf.put(SystemConstant.UPLOAD_TYPE, UploadFileEnum.PDF);
+                    BasicAttachment basicAttachment = new BasicAttachment(jsonPdf.toJSONString(), localPdfFile.getName(), SystemConstant.PDF_PREFIX, new BigDecimal(localPdfFile.length()), DigestUtils.md5Hex(new FileInputStream(localPdfFile)), sysUser.getId());
+                    basicAttachmentService.save(basicAttachment);
+                    examDetail.setAttachmentId(basicAttachment.getId());
+                    detailService.saveOrUpdate(examDetail);
+                    basicAttachmentList.add(basicAttachment);
+                    ossUtil.ossUpload(dirName, localPdfFile, BinaryUtil.toBase64String(HexUtils.decodeHex(DigestUtils.md5Hex(new FileInputStream(localPdfFile)))));
+                    localPdfFile.delete();
+                    for (String s : mergePdfList) {
+                        new File(s).delete();
+                    }
                 }
             }
             //最后一步删除附件

+ 1 - 0
distributed-print-business/src/main/java/com/qmth/distributed/print/business/util/HtmlToPdfUtil.java

@@ -42,6 +42,7 @@ public class HtmlToPdfUtil {
         cmd.append(dictionaryConfig.sysDomain().getHtmlToPdfUrl());
         cmd.append(" ");
         cmd.append(" --page-size ").append(pageSizeEnum.name()).append(" ");
+//        cmd.append(" --page-width 1200px");
         cmd.append("  --margin-top 0 ");
         cmd.append("  --margin-bottom 0 ");
         cmd.append("  --margin-left 0 ");

+ 20 - 0
distributed-print-business/src/main/java/com/qmth/distributed/print/business/util/OssUtil.java

@@ -47,6 +47,7 @@ public class OssUtil {
      *
      * @param dirName
      * @param inputStream
+     * @param md5
      */
     public void ossUpload(String dirName, InputStream inputStream, String md5) {
         log.info("ossUpload is come in");
@@ -60,6 +61,25 @@ public class OssUtil {
         log.info("dirName:{},requestid:{}", dirName, por.getRequestId());
     }
 
+    /**
+     * oss上传文件
+     *
+     * @param dirName
+     * @param file
+     * @param md5
+     */
+    public void ossUpload(String dirName, File file, String md5) {
+        log.info("ossUpload is come in");
+        ObjectMetadata meta = new ObjectMetadata();
+        if (Objects.nonNull(md5)) {
+            // 设置MD5校验。
+            meta.setContentMD5(md5);
+        }
+        PutObjectRequest putObjectRequest = new PutObjectRequest(this.aliYunOssDomain.getBucket(), dirName, file);
+        PutObjectResult por = this.ossClient.putObject(putObjectRequest);
+        log.info("dirName:{},requestid:{}", dirName, por.getRequestId());
+    }
+
     /**
      * oss上传文件
      *

+ 61 - 2
distributed-print-business/src/main/java/com/qmth/distributed/print/business/util/PdfUtil.java

@@ -4,11 +4,18 @@ import com.itextpdf.text.Document;
 import com.itextpdf.text.pdf.PdfCopy;
 import com.itextpdf.text.pdf.PdfImportedPage;
 import com.itextpdf.text.pdf.PdfReader;
+import com.qmth.distributed.print.business.enums.UploadFileEnum;
+import com.qmth.distributed.print.common.contant.SystemConstant;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.io.File;
 import java.io.FileOutputStream;
+import java.io.InputStream;
+import java.time.LocalDateTime;
+import java.util.List;
 import java.util.Objects;
+import java.util.StringJoiner;
 
 /**
  * @Description: 合并pdf util
@@ -26,12 +33,24 @@ public class PdfUtil {
      * @param files
      * @param outputPath
      */
-    public static void mergePdf(String[] files, String outputPath) {
+    public static String mergePdf(String[] files, String outputPath) {
         Document document = null;
         PdfCopy copy = null;
         PdfReader reader = null;
+        StringJoiner dirName = null;
         try {
             document = new Document(new PdfReader(files[0]).getPageSize(1));
+            if (Objects.isNull(outputPath)) {
+                LocalDateTime nowTime = LocalDateTime.now();
+                StringJoiner stringJoiner = new StringJoiner("").add(SystemConstant.PDF_TEMP_FILES_DIR).add(File.separator);
+                dirName = new StringJoiner("")
+                        .add(UploadFileEnum.PDF.getTitle()).add(File.separator)
+                        .add(String.valueOf(nowTime.getYear())).add(File.separator)
+                        .add(String.format("%02d", nowTime.getMonthValue())).add(File.separator)
+                        .add(String.format("%02d", nowTime.getDayOfMonth()))
+                        .add(File.separator).add(SystemConstant.getUuid()).add(SystemConstant.PDF_PREFIX);
+                outputPath = stringJoiner.toString() + dirName.toString();
+            }
             copy = new PdfCopy(document, new FileOutputStream(outputPath));
             document.open();
             for (int i = 0; i < files.length; i++) {
@@ -57,6 +76,46 @@ public class PdfUtil {
                 copy.close();
             }
         }
+        return Objects.nonNull(dirName) ? dirName.toString() : null;
+    }
+
+    /**
+     * 将多个PDF合并成一个PDF
+     *
+     * @param files
+     * @param outputPath
+     */
+    public static void mergePdf(List<InputStream> files, String outputPath) {
+        Document document = null;
+        PdfCopy copy = null;
+        PdfReader reader = null;
+        try {
+            document = new Document(new PdfReader(files.get(0)).getPageSize(1));
+            copy = new PdfCopy(document, new FileOutputStream(outputPath));
+            document.open();
+            for (int i = 0; i < files.size(); i++) {
+                reader = new PdfReader(files.get(i));
+                int numberOfPages = reader.getNumberOfPages();
+                for (int j = 1; j <= numberOfPages; j++) {
+                    document.newPage();
+                    PdfImportedPage page = copy.getImportedPage(reader, j);
+                    copy.addPage(page);
+                }
+            }
+        } catch (Exception e) {
+            log.error("请求出错", e);
+        } finally {
+            if (Objects.nonNull(document)) {
+                document.close();
+            }
+            if (Objects.nonNull(reader)) {
+                reader.close();
+            }
+            if (Objects.nonNull(copy)) {
+                copy.flush();
+                copy.close();
+            }
+        }
     }
 
 //    public static void main(String[] args) {
@@ -67,6 +126,6 @@ public class PdfUtil {
 ////                "/Users/king/Downloads/测试附件/101_148382537591291904_signAndSticker.pdf"
 //        };
 //        String outputPath = "/Users/king/Downloads/pdf/merge1.pdf";
-//        MergePdfUtil.mergePdf(files, outputPath);
+//        PdfUtil.mergePdf(files, outputPath);
 //    }
 }

+ 2 - 0
distributed-print-common/src/main/java/com/qmth/distributed/print/common/contant/SystemConstant.java

@@ -39,6 +39,7 @@ public class SystemConstant {
     public static final String DEFAULT_PASSWORD = "MTIzNDU2";
     public static final String UPDATE_TIME = "updateTime";
     public static final String PATH = "path";
+    public static final String PDF_PATH = "pdfPath";
     public static final String TYPE = "type";
     public static final String LOCAL = "local";
     public static final String OSS = "oss";
@@ -49,6 +50,7 @@ public class SystemConstant {
     public static final String FILE = "file";
     public static final String SIZE = "size";
     public static final String HTML_PREFIX = ".html";
+    public static final String PDF_PREFIX = ".pdf";
     public static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd HH:mm:ss";
     public static final int PAGE_NUMBER = 0;
     public static final int PAGE_SIZE = 10000000;