Browse Source

根据多个专利号生成这些专利的说明书pdf首页,拼成的一个大pdf

chendayu 2 years ago
parent
commit
0b8d443f4a

+ 17 - 0
PAS/src/main/java/cn/cslg/pas/common/AAA.java

@@ -0,0 +1,17 @@
+package cn.cslg.pas.common;
+
+import lombok.Data;
+
+import java.util.List;
+
+/**
+ * @author chenyu
+ * @date 2023/9/7
+ */
+@Data
+public class AAA {
+    /**
+     * 多件专利号
+     */
+    private List<String> patentNos;
+}

+ 4 - 6
PAS/src/main/java/cn/cslg/pas/controller/PatentInstructionController.java

@@ -1,6 +1,7 @@
 package cn.cslg.pas.controller;
 
 
+import cn.cslg.pas.common.AAA;
 import cn.cslg.pas.common.core.annotation.Permission;
 import cn.cslg.pas.common.core.base.Constants;
 import cn.cslg.pas.common.model.vo.PatentInstructionVO;
@@ -17,10 +18,7 @@ import io.swagger.v3.oas.annotations.Operation;
 import io.swagger.v3.oas.annotations.tags.Tag;
 import lombok.RequiredArgsConstructor;
 import org.springframework.context.annotation.Lazy;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
 
 import javax.servlet.ServletOutputStream;
@@ -101,8 +99,8 @@ public class PatentInstructionController {
 
     @PostMapping("/pdfFirstPage")
     @Operation(summary = "获取说明书首页")
-    public void pdfFileFirstPage(List<String> patentNos) throws IOException {
-        patentPDFService.queryPatentPdfFirstPages(patentNos);
+    public void pdfFileFirstPage(@RequestBody AAA aaa) throws IOException {
+        patentPDFService.queryPatentPdfFirstPages(aaa.getPatentNos());
     }
 
 }

+ 33 - 4
PAS/src/main/java/cn/cslg/pas/service/patentPDF/PatentPDFService.java

@@ -17,7 +17,10 @@ import org.springframework.stereotype.Service;
 
 import java.awt.image.BufferedImage;
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
 import java.util.List;
 
 /**
@@ -36,18 +39,44 @@ public class PatentPDFService extends ServiceImpl<PatentInstructionMapper, Paten
         log.info("开始处理【导出多件专利pdf首页】的业务,业务参数为patentNos:{}", patentNos);
 
         //根据专利号patentNos查询出所有pdf文档数据
-        List<PatentInstruction> patentInstructions = this.list(new LambdaQueryWrapper<PatentInstruction>().in(PatentInstruction::getPatentNo, patentNos));
+        List<PatentInstruction> patentInstructions = this.list(new LambdaQueryWrapper<PatentInstruction>().in(PatentInstruction::getPatentNo, patentNos).groupBy(PatentInstruction::getPatentNo));
 
-        for (PatentInstruction patentInstruction : patentInstructions) {
+        ArrayList<String> filePaths = new ArrayList<>();  //所有专利说明书首页pdf文件路径
+        for (PatentInstruction patentInstruction : patentInstructions) {  //遍历,将所有专利说明书pdf的首页取出生成新的Pdf文件存在本地
             String filePath = fileUtils.getSystemPath() + patentInstruction.getUrl();
             PDDocument doc = PDDocument.load(new File(filePath));
             Splitter splitter = new Splitter();
             splitter.setStartPage(1);
             splitter.setEndPage(1);
             PDDocument neededDoc = splitter.split(doc).get(0);
-            File pdfFirstPageFile = new File("合并多件专利的pdf首页.pdf");
-            neededDoc.save(pdfFirstPageFile);
+            String newFilePath = "专利号" + patentInstruction.getPatentNo() + "说明书pdf首页.pdf";
+            //File file = new File(newFilePath);
+            neededDoc.save(newFilePath);
+            filePaths.add(newFilePath);
+            neededDoc.close();
+            doc.close();
+        }
+
+        //开始合并上面生成的所有专利说明书首页pdf文件
+        PDFMergerUtility mergePdf = new PDFMergerUtility();
+        List<InputStream> inputStreams = new ArrayList<>();
+        for (String filePath : filePaths) {
+            inputStreams.add(new FileInputStream(filePath));
+        }
+        mergePdf.addSources(inputStreams);
+        // 设置合并生成pdf文件名称
+        mergePdf.setDestinationFileName("多件专利说明书pdf首页合并后的pdf.pdf");
+        // 合并PDF
+        mergePdf.mergeDocuments();
+        for (InputStream in : inputStreams) {
+            if (in != null) {
+                in.close();
+            }
+        }
 
+        //最后删除所有pdf首页临时文件
+        for (String filePath : filePaths) {
+            new File(filePath).delete();
         }
 
     }

BIN
多件专利说明书pdf首页合并后的pdf.pdf