فهرست منبع

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

chendayu 2 سال پیش
والد
کامیت
2a1d2970ce
1فایلهای تغییر یافته به همراه48 افزوده شده و 28 حذف شده
  1. 48 28
      PAS/src/main/java/cn/cslg/pas/service/PatentInstructionService.java

+ 48 - 28
PAS/src/main/java/cn/cslg/pas/service/PatentInstructionService.java

@@ -26,9 +26,6 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
-import org.apache.commons.compress.utils.IOUtils;
-import org.apache.commons.fileupload.FileItem;
-import org.apache.commons.fileupload.disk.DiskFileItemFactory;
 import org.apache.pdfbox.multipdf.PDFMergerUtility;
 import org.apache.pdfbox.multipdf.Splitter;
 import org.apache.pdfbox.pdmodel.PDDocument;
@@ -40,17 +37,18 @@ import org.apache.pdfbox.pdmodel.font.PDType1Font;
 import org.apache.pdfbox.rendering.ImageType;
 import org.apache.pdfbox.rendering.PDFRenderer;
 import org.springframework.context.annotation.Lazy;
-import org.springframework.http.MediaType;
 import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.transaction.interceptor.TransactionAspectSupport;
 import org.springframework.web.multipart.MultipartFile;
-import org.springframework.web.multipart.commons.CommonsMultipartFile;
 
 import javax.imageio.ImageIO;
+import java.awt.*;
 import java.awt.image.RenderedImage;
-import java.io.*;
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
 import java.util.*;
 import java.util.stream.Collectors;
 
@@ -317,7 +315,7 @@ public class PatentInstructionService extends ServiceImpl<PatentInstructionMappe
         //根据筛选条件分页查询专利清单(这一次查询目的只是获得专利总数和总页数)
         IPage<PatentDTO> pageList0 = patentService.getPageList(params);
         if (pageList0 == null || pageList0.getRecords() == null || pageList0.getRecords().size() == 0) {
-            ThrowException.throwXiaoShiException("导出专利pdf首页文档失败,无专利信息!");
+            ThrowException.throwXiaoShiException("合并专利pdf首页文档失败,无专利信息!");
         }
 
         int total = (int) pageList0.getTotal();  //专利总数(作为任务进度总数)
@@ -354,20 +352,51 @@ public class PatentInstructionService extends ServiceImpl<PatentInstructionMappe
             //遍历当前专利号清单
             for (String patentNo : patentNos) {
                 List<PatentInstruction> patentInstructionList = patentInstructions.stream().filter(item -> item.getPatentNo().equals(patentNo)).collect(Collectors.toList());
-                //若没有说明书pdf
-                if (patentInstructionList.size() == 0) {
+                //若有说明书pdf数据
+                PatentInstruction patentInstruction = new PatentInstruction();
+                if (patentInstructionList.size() > 1) {  //若有多个,则过滤取出授权文档
+                    patentInstruction = patentInstructionList.stream().filter(item -> item.getType() == 2).collect(Collectors.toList()).get(0);
+                } else if (patentInstructionList.size() == 1) {  //若只有一个pdf,则直接取出
+                    patentInstruction = patentInstructionList.get(0);
+                }
+                String filePath = fileUtils.getSystemPath() + patentInstruction.getUrl();
+                File pdfFile = new File(filePath);
+
+                //若没有说明书pdf数据,则手写空白页显示该专利没有说明书pdf
+                if (patentInstructionList.size() == 0 || !pdfFile.exists()) {
                     //创建文件,设置页码
                     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);
+                    PDPageContentStream contents = new PDPageContentStream(doc, pageOne, true, true, true);
+                    //设置要使用的字体类型
+                    //PDFont font = PDType1Font.COURIER_BOLD_OBLIQUE;
+                    PDFont font = PDType1Font.HELVETICA;
+                    //要写入的文本内容
+                    String message = "no pdf of " + patentNo;
+                    float fontSize = 36.0f;
+                    PDRectangle pageSize = pageOne.getMediaBox();
+                    float stringWidth = font.getStringWidth(message) * fontSize / 1000f;
+                    //计算页中心位置
+                    int rotation = pageOne.getRotation();
+                    boolean rotate = rotation == 90 || rotation == 270;
+                    float pageWidth = rotate ? pageSize.getHeight() : pageSize.getWidth();
+                    float pageHeight = rotate ? pageSize.getWidth() : pageSize.getHeight();
+                    double centeredXPosition = rotate ? pageHeight / 2f : (pageWidth - stringWidth) / 2f;
+                    double centeredYPosition = rotate ? (pageWidth - stringWidth) / 2f : pageHeight / 2f;
                     contents.beginText();
-                    //直接写入内容即可
-                    contents.showText("patentNo " + patentNo + " mission");
+                    //设置字体和大小
+                    contents.setFont(font, fontSize);
+                    //设置文本颜色
+                    contents.setNonStrokingColor(Color.BLACK);
+                    if (rotate) {
+                        // rotate the text according to the page rotation
+                        contents.setTextRotation(Math.PI / 2, centeredXPosition, centeredYPosition);
+                    } else {
+                        contents.setTextTranslation(centeredXPosition, centeredYPosition);
+                    }
+                    contents.showText(message);
                     contents.endText();
                     //记得关闭流对象要不然是无法成功保存pdf文档的
                     contents.close();
@@ -382,19 +411,7 @@ public class PatentInstructionService extends ServiceImpl<PatentInstructionMappe
                     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));
+                PDDocument doc = PDDocument.load(pdfFile);
                 Splitter splitter = new Splitter();
                 splitter.setStartPage(1);
                 splitter.setEndPage(1);
@@ -418,6 +435,7 @@ public class PatentInstructionService extends ServiceImpl<PatentInstructionMappe
         // 合并PDF
         pdfMerger.mergeDocuments();
 
+        //若最终合并成的pdf文件不存在
         if (!new File(mergedFilePath).exists()) {
             ThrowException.throwXiaoShiException("当前批次所有专利的说明书文档不存在或已丢失");
         }
@@ -444,6 +462,8 @@ public class PatentInstructionService extends ServiceImpl<PatentInstructionMappe
         for (String filePath : filePaths) {  //删除每个pdf首页文件
             new File(filePath).delete();
         }
+        //删除最终合并的pdf首页文件
+        new File(mergedFilePath).delete();
 
     }