Przeglądaj źródła

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

chendayu 2 lat temu
rodzic
commit
ad54f3da41

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

@@ -16,6 +16,7 @@ import cn.hutool.core.io.IoUtil;
 import io.swagger.v3.oas.annotations.Operation;
 import io.swagger.v3.oas.annotations.tags.Tag;
 import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.context.annotation.Lazy;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
@@ -32,6 +33,7 @@ import java.io.IOException;
  * @author 王岩
  * @since 2022-03-02
  */
+@Slf4j
 @Tag(name = "专利说明书")
 @RestController
 @RequestMapping(Constants.API_VERSION_V2 + "/patent/instruction")
@@ -94,12 +96,13 @@ public class PatentInstructionController {
         }
     }
 
-//    @PostMapping("/pdfFirstPage")
-//    @Operation(summary = "下载多件专利的说明书首页pdf")
-//    public String pdfFileFirstPage(@RequestBody PatentVO params) throws IOException {
-//        patentInstructionService.queryPatentPdfFirstPages(params);
-//        return Response.success();
-//    }
+    @PostMapping("/pdfFirstPage")
+    @Operation(summary = "下载多件专利的说明书首页pdf")
+    public String pdfFileFirstPage(@RequestBody PatentVO params) throws IOException {
+        log.info("开始处理【合并多个专利pdf首页】的请求,请求参数为:{}", params);
+        patentInstructionService.queryPatentPdfFirstPages(params);
+        return Response.success();
+    }
 
 }
 

+ 183 - 179
PAS/src/main/java/cn/cslg/pas/service/PatentInstructionService.java

@@ -311,184 +311,188 @@ public class PatentInstructionService extends ServiceImpl<PatentInstructionMappe
         });
     }
 
-//    public void queryPatentPdfFirstPages(PatentVO params) throws IOException {
-//        log.info("开始处理【导出多件专利pdf首页】的业务,业务参数为:{}", params);
-//
-//        //根据筛选条件分页查询专利清单(这一次查询目的只是获得专利总数和总页数)
-//        IPage<PatentDTO> pageList0 = patentService.getPageList(params);
-//        int total = (int) pageList0.getTotal();  //专利总数(作为任务进度总数)
-//        long pages = pageList0.getPages();  //总页数(用来计算要检索几次)
-//
-//        ArrayList<String> filePaths = new ArrayList<>();  //存放所有临时文件(说明书pdf首页的文件)路径
-//        String mergedFilePath = StringUtils.getUUID() + ".pdf";  //合并后的pdf文件路径
-//        PDFMergerUtility pdfMerger = new PDFMergerUtility();  //pdf合并工具
-//
-//        //上传任务
-//        Task task = new Task()
-//                .setFileName(mergedFilePath)
-//                .setProjectId(params.getProjectId())
-//                .setType(6)
-//                .setCreateBy(loginUtils.getId())
-//                .setStartTime(DateUtils.getDateTime())
-//                .setTotal(total)
-//                .setStatus(6)
-//                .setDefaultNum(0)
-//                .setSuccessNum(0)
-//                .setTrueSuccessNum(0);
-//        taskService.save(task);
-//
-//        //一页一页检索
-//        for (long i = 1; i <= pages; i++) {
-//            params.setCurrent(i);
-//            IPage<PatentDTO> pageList = patentService.getPageList(params);  //根据筛选条件分页查询专利清单
-//            List<PatentDTO> patents = pageList.getRecords();  //取出专利清单
-//            List<String> patentNos = patents.stream().map(PatentDTO::getPatentNo).collect(Collectors.toList());  //过滤取出专利号
-//
-//            //根据专利号patentNos查询出所有pdf文档数据
-//            List<PatentInstruction> patentInstructions = this.list(new LambdaQueryWrapper<PatentInstruction>().in(PatentInstruction::getPatentNo, patentNos));
-//
-//            //遍历当前专利号清单
-//            for (String patentNo : patentNos) {
-//                List<PatentInstruction> patentInstructionList = patentInstructions.stream().filter(item -> item.getPatentNo().equals(patentNo)).collect(Collectors.toList());
-//                //若没有说明书pdf
-//                if (patentInstructionList.size() == 0) {
-//                    //创建文件,设置页码
-//                    PDDocument doc = new PDDocument();
-//                    PDPage pageOne = new PDPage(PDRectangle.A4);
-//                    doc.addPage(pageOne);
-//                    //创建页面内容流
-//                    PDPageContentStream contents = new PDPageContentStream(doc, pageOne);
-//                    //设置要使用的字体
-//                    PDFont font =  PDType1Font.COURIER_BOLD_OBLIQUE;
-//                    contents.setFont(font,18);
-//                    contents.beginText();
-//                    //直接写入内容即可
-//                    contents.showText("patentNo " + patentNo + " mission");
-//                    contents.endText();
-//                    //记得关闭流对象要不然是无法成功保存pdf文档的
-//                    contents.close();
-//                    String newFilePath = StringUtils.getUUID() + ".pdf";
-//                    doc.save(newFilePath);
-//                    filePaths.add(newFilePath);
-//                    pdfMerger.addSource(newFilePath);
-//                    doc.close();
-//                    //更新任务表(完成数量+1)并发送进度
-//                    task.setSuccessNum(task.getSuccessNum() + 1);
-//                    task.setTrueSuccessNum(task.getTrueSuccessNum() + 1);
-//                    sendMessage(total, task);
-//                    continue;
-//                }
-//                //若有说明书pdf
-//                PatentInstruction patentInstruction;
-//                if (patentInstructionList.size() > 1) {  //若有多个,则过滤取出授权文档
-//                    patentInstruction = patentInstructionList.stream().filter(item -> item.getType() == 2).collect(Collectors.toList()).get(0);
-//                } else {  //若只有一个pdf,则直接取出
-//                    patentInstruction = patentInstructionList.get(0);
-//                }
-//
-//                String filePath = fileUtils.getSystemPath() + patentInstruction.getUrl();
-//                if (!new File(filePath).exists()) {  //如果说明书文档不存在了,则跳过,继续下一个
-//                    continue;
-//                }
-//                PDDocument doc = PDDocument.load(new File(filePath));
-//                Splitter splitter = new Splitter();
-//                splitter.setStartPage(1);
-//                splitter.setEndPage(1);
-//                PDDocument needDoc = splitter.split(doc).get(0);
-//                String newFilePath = StringUtils.getUUID() + ".pdf";
-//                needDoc.save(newFilePath);
-//                filePaths.add(newFilePath);
-//                pdfMerger.addSource(newFilePath);
-//                needDoc.close();
-//                doc.close();
-//                //更新任务表(完成数量+1)并发送进度
-//                task.setSuccessNum(task.getSuccessNum() + 1);
-//                task.setTrueSuccessNum(task.getTrueSuccessNum() + 1);
-//                sendMessage(total, task);
-//            }
-//
-//        }
-//
-//        // 设置合并生成pdf文件名称
-//        pdfMerger.setDestinationFileName(mergedFilePath);
-//        // 合并PDF
-//        pdfMerger.mergeDocuments();
-//
-//        if (!new File(mergedFilePath).exists()) {
-//            ThrowException.throwXiaoShiException("当前批次所有专利的说明书文档丢失");
-//        }
-//
-//        //File文件转成MultipartFile
-//        MultipartFile multipartFile = FileToMultipartFileUtiles.fileToMultipartFile(mergedFilePath);
-//        ArrayList<MultipartFile> multipartFiles = new ArrayList<>();
-//        multipartFiles.add(multipartFile);
-//
-//        String res = fileManagerService.uploadFile(multipartFiles);
-//        JSONObject jsonObject = JSONObject.parseObject(res);
-//        List<Integer> fileIds = JSONArray.parseArray(jsonObject.get("data").toString(), Integer.class);
-//        res = fileManagerService.getSystemFileFromFMS(fileIds);
-//        List<SystemFile> systemFiles = JSONArray.parseArray(res, SystemFile.class);
-//        SystemFile systemFile = systemFiles.get(0);
-//        Task updateTask = new Task();
-//        updateTask.setId(task.getId());
-//        updateTask.setSystemFileId(fileIds.get(0));
-//        updateTask.setUrl(systemFile.getFilePath().substring(systemFile.getFilePath().indexOf("file") + 4));
-//        updateTask.setFileSize(Long.valueOf(systemFile.getFileLength()));
-//        taskService.updateById(updateTask);
-//
-//        //最后要记得删除所有PDF首页临时文件
-//        for (String filePath : filePaths) {  //删除每个pdf首页文件
-//            new File(filePath).delete();
-//        }
-//
-//    }
-//
-//    /**
-//     * 更新任务表(完成数量+1)、发送websocket进度
-//     *
-//     * @param total 任务专利总数量
-//     * @param task  任务对象
-//     */
-//    private void sendMessage(int total, Task task) {
-//        //更新任务表(完成数量+1),并发送进度+1
-//        Task updateTask = new Task();
-//        updateTask.setId(task.getId());
-//        updateTask.setSuccessNum(task.getSuccessNum());
-//        updateTask.setTrueSuccessNum(task.getTrueSuccessNum());
-//        taskService.updateById(updateTask);
-//        long percentage = (long) Math.floor((task.getSuccessNum() + 0D) / total * 100D);
-//        //当全部完成时
-//        if (task.getSuccessNum().equals(total)) {
-//            percentage = 100L;
-//            //任务表更新最终数据
-//            task.setStatus(2);
-//            updateTask.setStatus(task.getStatus());
-//            updateTask.setEndTime(DateUtils.getDateTime());
-//            taskService.updateById(updateTask);
-//        }
-//        //websocket发送进度
-//        messageService.sendWebsocketMessage(task, total, task.getSuccessNum(), percentage);
-//    }
-//
-//    public void generateBookIamge(File inputFile, File outputFile) {
-//
-//        try {
-//            PDDocument doc = PDDocument.load(inputFile);
-//            PDFRenderer pdfRenderer = new PDFRenderer(doc);
-//            // 提取的页码
-//            int pageNumber = 0;
-//            // 以300 dpi 读取存入 BufferedImage 对象
-//            int dpi = 300;
-//            RenderedImage buffImage = pdfRenderer.renderImageWithDPI(pageNumber, dpi, ImageType.RGB);
-//            // 将 BufferedImage 写入到 png
-//            ImageIO.write(buffImage, "png", outputFile);
-//
-//            // 关闭文档
-//            doc.close();
-//
-//        } catch (Exception e) {
-//            e.printStackTrace();
-//        }
-//    }
+    public void queryPatentPdfFirstPages(PatentVO params) throws IOException {
+        log.info("开始处理【合并多个专利pdf首页】的业务,业务参数为:{}", params);
+
+        //根据筛选条件分页查询专利清单(这一次查询目的只是获得专利总数和总页数)
+        IPage<PatentDTO> pageList0 = patentService.getPageList(params);
+        if (pageList0 == null || pageList0.getRecords() == null || pageList0.getRecords().size() == 0) {
+            ThrowException.throwXiaoShiException("导出专利pdf首页文档失败,无专利信息!");
+        }
+
+        int total = (int) pageList0.getTotal();  //专利总数(作为任务进度总数)
+        long pages = pageList0.getPages();  //总页数(用来计算要检索几次)
+
+        ArrayList<String> filePaths = new ArrayList<>();  //存放所有临时文件(说明书pdf首页的文件)路径
+        String mergedFilePath = StringUtils.getUUID() + ".pdf";  //合并后的pdf文件路径
+        PDFMergerUtility pdfMerger = new PDFMergerUtility();  //pdf合并工具
+
+        //上传任务
+        Task task = new Task()
+                .setFileName(mergedFilePath)
+                .setProjectId(params.getProjectId())
+                .setType(6)
+                .setCreateBy(loginUtils.getId())
+                .setStartTime(DateUtils.getDateTime())
+                .setTotal(total)
+                .setStatus(6)
+                .setDefaultNum(0)
+                .setSuccessNum(0)
+                .setTrueSuccessNum(0);
+        taskService.save(task);
+
+        //一页一页检索
+        for (long i = 1; i <= pages; i++) {
+            params.setCurrent(i);
+            IPage<PatentDTO> pageList = patentService.getPageList(params);  //根据筛选条件分页查询专利清单
+            List<PatentDTO> patents = pageList.getRecords();  //取出专利清单
+            List<String> patentNos = patents.stream().map(PatentDTO::getPatentNo).collect(Collectors.toList());  //过滤取出专利号
+
+            //根据专利号patentNos查询出所有pdf文档数据
+            List<PatentInstruction> patentInstructions = this.list(new LambdaQueryWrapper<PatentInstruction>().in(PatentInstruction::getPatentNo, patentNos));
+
+            //遍历当前专利号清单
+            for (String patentNo : patentNos) {
+                List<PatentInstruction> patentInstructionList = patentInstructions.stream().filter(item -> item.getPatentNo().equals(patentNo)).collect(Collectors.toList());
+                //若没有说明书pdf
+                if (patentInstructionList.size() == 0) {
+                    //创建文件,设置页码
+                    PDDocument doc = new PDDocument();
+                    PDPage pageOne = new PDPage(PDRectangle.A4);
+                    doc.addPage(pageOne);
+                    //创建页面内容流
+                    PDPageContentStream contents = new PDPageContentStream(doc, pageOne);
+                    //设置要使用的字体
+                    PDFont font = PDType1Font.COURIER_BOLD_OBLIQUE;
+                    contents.setFont(font, 18);
+                    contents.beginText();
+                    //直接写入内容即可
+                    contents.showText("patentNo " + patentNo + " mission");
+                    contents.endText();
+                    //记得关闭流对象要不然是无法成功保存pdf文档的
+                    contents.close();
+                    String newFilePath = StringUtils.getUUID() + ".pdf";
+                    doc.save(newFilePath);
+                    filePaths.add(newFilePath);
+                    pdfMerger.addSource(newFilePath);
+                    doc.close();
+                    //更新任务表(完成数量+1)并发送进度
+                    task.setSuccessNum(task.getSuccessNum() + 1);
+                    task.setTrueSuccessNum(task.getTrueSuccessNum() + 1);
+                    sendMessage(total, task);
+                    continue;
+                }
+                //若有说明书pdf
+                PatentInstruction patentInstruction;
+                if (patentInstructionList.size() > 1) {  //若有多个,则过滤取出授权文档
+                    patentInstruction = patentInstructionList.stream().filter(item -> item.getType() == 2).collect(Collectors.toList()).get(0);
+                } else {  //若只有一个pdf,则直接取出
+                    patentInstruction = patentInstructionList.get(0);
+                }
+
+                String filePath = fileUtils.getSystemPath() + patentInstruction.getUrl();
+                if (!new File(filePath).exists()) {  //如果说明书文档不存在了,则跳过,继续下一个
+                    continue;
+                }
+                PDDocument doc = PDDocument.load(new File(filePath));
+                Splitter splitter = new Splitter();
+                splitter.setStartPage(1);
+                splitter.setEndPage(1);
+                PDDocument needDoc = splitter.split(doc).get(0);
+                String newFilePath = StringUtils.getUUID() + ".pdf";
+                needDoc.save(newFilePath);
+                filePaths.add(newFilePath);
+                pdfMerger.addSource(newFilePath);
+                needDoc.close();
+                doc.close();
+                //更新任务表(完成数量+1)并发送进度
+                task.setSuccessNum(task.getSuccessNum() + 1);
+                task.setTrueSuccessNum(task.getTrueSuccessNum() + 1);
+                sendMessage(total, task);
+            }
+
+        }
+
+        // 设置合并生成pdf文件名称
+        pdfMerger.setDestinationFileName(mergedFilePath);
+        // 合并PDF
+        pdfMerger.mergeDocuments();
+
+        if (!new File(mergedFilePath).exists()) {
+            ThrowException.throwXiaoShiException("当前批次所有专利的说明书文档不存在或已丢失");
+        }
+
+        //File文件转成MultipartFile
+        MultipartFile multipartFile = FileToMultipartFileUtiles.fileToMultipartFile(mergedFilePath);
+        ArrayList<MultipartFile> multipartFiles = new ArrayList<>();
+        multipartFiles.add(multipartFile);
+
+        String res = fileManagerService.uploadFile(multipartFiles);
+        JSONObject jsonObject = JSONObject.parseObject(res);
+        List<Integer> fileIds = JSONArray.parseArray(jsonObject.get("data").toString(), Integer.class);
+        res = fileManagerService.getSystemFileFromFMS(fileIds);
+        List<SystemFile> systemFiles = JSONArray.parseArray(res, SystemFile.class);
+        SystemFile systemFile = systemFiles.get(0);
+        Task updateTask = new Task();
+        updateTask.setId(task.getId());
+        updateTask.setSystemFileId(fileIds.get(0));
+        updateTask.setUrl(systemFile.getFilePath().substring(systemFile.getFilePath().indexOf("file") + 4));
+        updateTask.setFileSize(Long.valueOf(systemFile.getFileLength()));
+        taskService.updateById(updateTask);
+
+        //最后要记得删除所有PDF首页临时文件
+        for (String filePath : filePaths) {  //删除每个pdf首页文件
+            new File(filePath).delete();
+        }
+
+    }
+
+    /**
+     * 更新任务表(完成数量+1)、发送websocket进度
+     *
+     * @param total 任务专利总数量
+     * @param task  任务对象
+     */
+    private void sendMessage(int total, Task task) {
+        //更新任务表(完成数量+1),并发送进度+1
+        Task updateTask = new Task();
+        updateTask.setId(task.getId());
+        updateTask.setSuccessNum(task.getSuccessNum());
+        updateTask.setTrueSuccessNum(task.getTrueSuccessNum());
+        taskService.updateById(updateTask);
+        long percentage = (long) Math.floor((task.getSuccessNum() + 0D) / total * 100D);
+        //当全部完成时
+        if (task.getSuccessNum().equals(total)) {
+            percentage = 100L;
+            //任务表更新最终数据
+            task.setStatus(2);
+            updateTask.setStatus(task.getStatus());
+            updateTask.setEndTime(DateUtils.getDateTime());
+            taskService.updateById(updateTask);
+        }
+        //websocket发送进度
+        messageService.sendWebsocketMessage(task, total, task.getSuccessNum(), percentage);
+    }
+
+    public void generateBookIamge(File inputFile, File outputFile) {
+
+        try {
+            PDDocument doc = PDDocument.load(inputFile);
+            PDFRenderer pdfRenderer = new PDFRenderer(doc);
+            // 提取的页码
+            int pageNumber = 0;
+            // 以300 dpi 读取存入 BufferedImage 对象
+            int dpi = 300;
+            RenderedImage buffImage = pdfRenderer.renderImageWithDPI(pageNumber, dpi, ImageType.RGB);
+            // 将 BufferedImage 写入到 png
+            ImageIO.write(buffImage, "png", outputFile);
+
+            // 关闭文档
+            doc.close();
+
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
 
 }