PDFExportFirstPageService.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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.dto.es.EsCustomFieldDTO;
  5. import cn.cslg.pas.common.model.request.QueryRequest;
  6. import cn.cslg.pas.common.utils.FormatUtil;
  7. import cn.cslg.pas.common.vo.business.PatentNoVO;
  8. import cn.cslg.pas.common.vo.es.EsCustomFieldBatchVO;
  9. import cn.cslg.pas.exception.XiaoShiException;
  10. import cn.cslg.pas.service.business.es.EsCustomFieldService;
  11. import cn.cslg.pas.service.business.es.EsPatentService;
  12. import cn.cslg.pas.service.business.es.EsService;
  13. import cn.cslg.pas.service.common.FileManagerService;
  14. import lombok.extern.slf4j.Slf4j;
  15. import org.apache.pdfbox.multipdf.PDFMergerUtility;
  16. import org.apache.pdfbox.io.MemoryUsageSetting;
  17. import org.apache.pdfbox.pdmodel.PDDocument;
  18. import org.apache.pdfbox.pdmodel.PDPage;
  19. import org.apache.pdfbox.rendering.PDFRenderer;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.stereotype.Service;
  22. import org.springframework.util.CollectionUtils;
  23. import java.io.ByteArrayOutputStream;
  24. import java.io.FileNotFoundException;
  25. import java.io.IOException;
  26. import java.util.ArrayList;
  27. import java.util.Arrays;
  28. import java.util.List;
  29. /**
  30. * @Author xiexiang
  31. * @Date 2024/1/4
  32. */
  33. @Slf4j
  34. @Service
  35. public class PDFExportFirstPageService {
  36. @Autowired
  37. private FileManagerService fileManagerService;
  38. @Autowired
  39. private EsService esService;
  40. @Autowired
  41. private EsCustomFieldService esCustomFieldService;
  42. @Autowired
  43. private EsPatentService patentService;
  44. public byte[] mergePDFFirstPage(EsCustomFieldBatchVO EsVO) throws IOException {
  45. //初始化
  46. Integer startNum = EsVO.getStartNumber();
  47. Integer endNum = EsVO.getEndNumber();
  48. List<String> isAdd = EsVO.getIsAdd();
  49. List<String> isDel = EsVO.getIsDelete();
  50. // 创建一个PDFMergerUtility实例
  51. try {
  52. if (!CollectionUtils.isEmpty(isAdd) && startNum <= 1 && endNum < 1) {
  53. isAdd.removeAll(isDel);
  54. //创建一个输出流
  55. byte[] result = this.mergePerPatentPDF(isAdd);
  56. return result;
  57. } else if (startNum > 1 && endNum > 0){
  58. List<String> patentNos = new ArrayList<>();
  59. try {
  60. patentNos = esCustomFieldService.getPatentNos(EsVO);
  61. byte[] result = this.mergePerPatentPDF(patentNos);
  62. return result;
  63. } catch (Exception e) {
  64. throw new XiaoShiException("");
  65. }
  66. } else {
  67. return new byte[0];
  68. }
  69. } catch (FileNotFoundException e) {
  70. throw new FileNotFoundException();
  71. }
  72. }
  73. // /**
  74. // * 1
  75. // * @param EsVO
  76. // * @return
  77. // * @throws IOException
  78. // */
  79. // public byte[] mergeAndExportPDFFirstPage(EsCustomFieldBatchVO EsVO) throws IOException {
  80. // //根据projectId查询专利信息
  81. // QueryRequest queryRequest = new QueryRequest();
  82. // queryRequest.setProjectId(projectId);
  83. // PatentDTO patentDTO;
  84. // try {
  85. // patentDTO = esService.esSearch(queryRequest);
  86. // } catch (Exception e) {
  87. // throw new XiaoShiException("检索专利错误");
  88. // }
  89. // List<PatentColumnDTO> patents = patentDTO.getPatents();
  90. // byte[][] pdfDocuments = this.getPDFByteByAppNo(patents);
  91. // byte[] pdfData = new byte[0];
  92. // // 判断 byte[][] 数组是否为空
  93. // if (!this.isByteEmpty(pdfDocuments)) {
  94. // pdfData = this.mergeFirstPage(pdfDocuments);
  95. // }
  96. // return pdfData;
  97. // }
  98. /**
  99. * 判断文件数组是否为空
  100. * @param pdfDocuments
  101. * @return
  102. */
  103. private boolean isByteEmpty(byte[][] pdfDocuments) {
  104. return pdfDocuments == null || pdfDocuments.length == 0 || Arrays.stream(pdfDocuments).allMatch(array -> array == null || array.length == 0);
  105. }
  106. /**
  107. * 3
  108. * @param pdfDatas
  109. * @return
  110. * @throws IOException
  111. */
  112. public byte[] mergeFirstPage(byte[][] pdfDatas) throws IOException{
  113. //创建一个输出流
  114. try {
  115. // 创建一个PDFMergerUtility实例
  116. PDFMergerUtility merger = new PDFMergerUtility();
  117. PDDocument resultDocument = new PDDocument();
  118. //逐个处理每个pdf文件
  119. for (byte[] pdfData : pdfDatas) {
  120. // 加载每个输入的PDF文档
  121. PDDocument document = PDDocument.load(pdfData);
  122. // 获取第一页
  123. PDPage firstPage = document.getPage(0);
  124. // 创建一个新的文档用于保存第一页
  125. PDDocument firstPageDocument = new PDDocument();
  126. firstPageDocument.addPage(firstPage);
  127. // 将第一页添加到合并器中
  128. merger.appendDocument(resultDocument, firstPageDocument);
  129. // 关闭当前处理的文档
  130. document.close();
  131. firstPageDocument.close();
  132. }
  133. ByteArrayOutputStream out = new ByteArrayOutputStream();
  134. resultDocument.save(out);
  135. resultDocument.close();
  136. return out.toByteArray();
  137. } catch (FileNotFoundException e) {
  138. throw new FileNotFoundException();
  139. }
  140. }
  141. /**
  142. * 2
  143. * 根据查询到的专利信息获取它公告或公开专利的pdf文档数据流
  144. * @param patents
  145. * @return
  146. */
  147. public byte[][] getPDFByteByAppNo(List<PatentColumnDTO> patents){
  148. List<byte[]> pdfDatas = new ArrayList<>();
  149. //遍历传入专利集合
  150. for (PatentColumnDTO item : patents) {
  151. //判断申请号是否为空
  152. if (item.getAppNo() != null) {
  153. String appNo = item.getAppNo();
  154. Integer type = 1;
  155. //获取公告专利pdf文档guid
  156. String pdfGuid = FormatUtil.getPDFFormat(appNo, type);
  157. byte[] pdfData = new byte[0];
  158. try {
  159. pdfData = fileManagerService.downloadSystemFileFromFMS(pdfGuid);
  160. if (pdfData == null || pdfData.length == 0) {
  161. //获取公开专利pdf文档guid
  162. Integer type2 = 0;
  163. String pdfGuid2 = FormatUtil.getPDFFormat(appNo, type2);
  164. pdfData = fileManagerService.downloadSystemFileFromFMS(pdfGuid2);
  165. if (pdfData == null || pdfData.length == 0) {
  166. continue;
  167. } else {
  168. pdfDatas.add(pdfData);
  169. }
  170. } else {
  171. pdfDatas.add(pdfData);
  172. }
  173. } catch (IOException e) {
  174. e.printStackTrace();
  175. continue;//继续处理下一件专利
  176. }
  177. }
  178. }
  179. byte[][] pdfDataArray = new byte[pdfDatas.size()][];
  180. return pdfDatas.toArray(pdfDataArray);
  181. }
  182. public byte[] mergePerPatentPDF(List<String> patentNos) throws IOException {
  183. PDFMergerUtility merger = new PDFMergerUtility();
  184. PDDocument resultDocument = new PDDocument();
  185. for (String patentNo : patentNos) {
  186. PatentNoVO patentNoVO = new PatentNoVO();
  187. patentNoVO.setPatentNo(patentNo);
  188. try {
  189. PatentColumnDTO patentColumnDTO = patentService.selectPatentDetail(patentNoVO);
  190. if (patentColumnDTO != null) {
  191. //判断申请号是否为空
  192. if (patentColumnDTO.getAppNo() != null) {
  193. String appNo = patentColumnDTO.getAppNo();
  194. Integer type = 1;
  195. //获取公告专利pdf文档guid
  196. String pdfGuid = FormatUtil.getPDFFormat(appNo, type);
  197. byte[] pdfData = new byte[0];
  198. try {
  199. pdfData = fileManagerService.downloadSystemFileFromFMS(pdfGuid);
  200. if (pdfData == null || pdfData.length == 0) {
  201. //获取公开专利pdf文档guid
  202. Integer type2 = 0;
  203. String pdfGuid2 = FormatUtil.getPDFFormat(appNo, type2);
  204. pdfData = fileManagerService.downloadSystemFileFromFMS(pdfGuid2);
  205. if (pdfData == null || pdfData.length == 0) {
  206. continue;
  207. } else {
  208. // 加载输入的PDF文档
  209. PDDocument document = PDDocument.load(pdfData);
  210. // 获取第一页
  211. PDPage firstPage = document.getPage(0);
  212. // 创建一个新的文档用于保存第一页
  213. PDDocument firstPageDocument = new PDDocument();
  214. firstPageDocument.addPage(firstPage);
  215. // 将第一页添加到合并器中
  216. merger.appendDocument(resultDocument, firstPageDocument);
  217. // 关闭当前处理的文档
  218. document.close();
  219. firstPageDocument.close();
  220. }
  221. } else {
  222. // 加载每个输入的PDF文档
  223. PDDocument document = PDDocument.load(pdfData);
  224. // 获取第一页
  225. PDPage firstPage = document.getPage(0);
  226. // 创建一个新的文档用于保存第一页
  227. PDDocument firstPageDocument = new PDDocument();
  228. firstPageDocument.addPage(firstPage);
  229. // 将第一页添加到合并器中
  230. merger.appendDocument(resultDocument, firstPageDocument);
  231. // 关闭当前处理的文档
  232. document.close();
  233. firstPageDocument.close();
  234. }
  235. } catch (IOException e) {
  236. e.printStackTrace();
  237. continue;//继续处理下一件专利
  238. }
  239. } else {
  240. continue;
  241. }
  242. } else {
  243. continue;
  244. }
  245. } catch (Exception e) {
  246. continue;
  247. }
  248. }
  249. ByteArrayOutputStream out = new ByteArrayOutputStream();
  250. resultDocument.save(out);
  251. resultDocument.close();
  252. return out.toByteArray();
  253. }
  254. }