PDFExportFirstPageService.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package cn.cslg.pas.service.business;
  2. import cn.cslg.pas.common.dto.PatentColumnDTO;
  3. import cn.cslg.pas.common.dto.PatentDTO;
  4. import cn.cslg.pas.common.model.request.QueryRequest;
  5. import cn.cslg.pas.common.utils.FormatUtil;
  6. import cn.cslg.pas.exception.XiaoShiException;
  7. import cn.cslg.pas.service.business.es.EsService;
  8. import cn.cslg.pas.service.common.FileManagerService;
  9. import lombok.extern.slf4j.Slf4j;
  10. import org.apache.pdfbox.multipdf.PDFMergerUtility;
  11. import org.apache.pdfbox.io.MemoryUsageSetting;
  12. import org.apache.pdfbox.pdmodel.PDDocument;
  13. import org.apache.pdfbox.pdmodel.PDPage;
  14. import org.apache.pdfbox.rendering.PDFRenderer;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.stereotype.Service;
  17. import java.io.ByteArrayOutputStream;
  18. import java.io.FileNotFoundException;
  19. import java.io.IOException;
  20. import java.util.ArrayList;
  21. import java.util.Arrays;
  22. import java.util.List;
  23. /**
  24. * @Author xiexiang
  25. * @Date 2024/1/4
  26. */
  27. @Slf4j
  28. @Service
  29. public class PDFExportFirstPageService {
  30. @Autowired
  31. private FileManagerService fileManagerService;
  32. @Autowired
  33. private EsService esService;
  34. /**
  35. * 1
  36. * @param projectId
  37. * @return
  38. * @throws IOException
  39. */
  40. public byte[] mergeAndExportPDFFirstPage(Integer projectId) throws IOException {
  41. //根据projectId查询专利信息
  42. QueryRequest queryRequest = new QueryRequest();
  43. queryRequest.setProjectId(projectId);
  44. PatentDTO patentDTO;
  45. try {
  46. patentDTO = esService.esSearch(queryRequest);
  47. } catch (Exception e) {
  48. throw new XiaoShiException("检索专利错误");
  49. }
  50. List<PatentColumnDTO> patents = patentDTO.getPatents();
  51. byte[][] pdfDocuments = this.getPDFByteByAppNo(patents);
  52. byte[] pdfData = new byte[0];
  53. // 判断 byte[][] 数组是否为空
  54. if (!this.isByteEmpty(pdfDocuments)) {
  55. pdfData = this.mergeFirstPage(pdfDocuments);
  56. }
  57. return pdfData;
  58. }
  59. /**
  60. * 判断文件数组是否为空
  61. * @param pdfDocuments
  62. * @return
  63. */
  64. private boolean isByteEmpty(byte[][] pdfDocuments) {
  65. return pdfDocuments == null || pdfDocuments.length == 0 || Arrays.stream(pdfDocuments).allMatch(array -> array == null || array.length == 0);
  66. }
  67. /**
  68. * 3
  69. * @param pdfDatas
  70. * @return
  71. * @throws IOException
  72. */
  73. public byte[] mergeFirstPage(byte[][] pdfDatas) throws IOException{
  74. //创建一个输出流
  75. try {
  76. // 创建一个PDFMergerUtility实例
  77. PDFMergerUtility merger = new PDFMergerUtility();
  78. PDDocument resultDocument = new PDDocument();
  79. //逐个处理每个pdf文件
  80. for (byte[] pdfData : pdfDatas) {
  81. // 加载每个输入的PDF文档
  82. PDDocument document = PDDocument.load(pdfData);
  83. // 获取第一页
  84. PDPage firstPage = document.getPage(0);
  85. // 创建一个新的文档用于保存第一页
  86. PDDocument firstPageDocument = new PDDocument();
  87. firstPageDocument.addPage(firstPage);
  88. // 将第一页添加到合并器中
  89. merger.appendDocument(resultDocument, firstPageDocument);
  90. // 关闭当前处理的文档
  91. document.close();
  92. firstPageDocument.close();
  93. }
  94. ByteArrayOutputStream out = new ByteArrayOutputStream();
  95. resultDocument.save(out);
  96. resultDocument.close();
  97. return out.toByteArray();
  98. } catch (FileNotFoundException e) {
  99. throw new FileNotFoundException();
  100. }
  101. }
  102. /**
  103. * 2
  104. * 根据查询到的专利信息获取它公告或公开专利的pdf文档数据流
  105. * @param patents
  106. * @return
  107. */
  108. public byte[][] getPDFByteByAppNo(List<PatentColumnDTO> patents){
  109. List<byte[]> pdfDatas = new ArrayList<>();
  110. //遍历传入专利集合
  111. for (PatentColumnDTO item : patents) {
  112. //判断申请号是否为空
  113. if (item.getAppNo() != null) {
  114. String appNo = item.getAppNo();
  115. Integer type = 1;
  116. //获取公告专利pdf文档guid
  117. String pdfGuid = FormatUtil.getPDFFormat(appNo, type);
  118. byte[] pdfData = new byte[0];
  119. try {
  120. pdfData = fileManagerService.downloadSystemFileFromFMS(pdfGuid);
  121. if (pdfData == null || pdfData.length == 0) {
  122. //获取公开专利pdf文档guid
  123. Integer type2 = 0;
  124. String pdfGuid2 = FormatUtil.getPDFFormat(appNo, type2);
  125. pdfData = fileManagerService.downloadSystemFileFromFMS(pdfGuid2);
  126. if (pdfData == null || pdfData.length == 0) {
  127. continue;
  128. } else {
  129. pdfDatas.add(pdfData);
  130. }
  131. } else {
  132. pdfDatas.add(pdfData);
  133. }
  134. } catch (IOException e) {
  135. e.printStackTrace();
  136. continue;//继续处理下一件专利
  137. }
  138. }
  139. }
  140. byte[][] pdfDataArray = new byte[pdfDatas.size()][];
  141. return pdfDatas.toArray(pdfDataArray);
  142. }
  143. }