package cn.cslg.pas.service.business; import cn.cslg.pas.common.dto.PatentColumnDTO; import cn.cslg.pas.common.dto.PatentDTO; import cn.cslg.pas.common.dto.es.EsCustomFieldDTO; import cn.cslg.pas.common.model.request.QueryRequest; import cn.cslg.pas.common.utils.FormatUtil; import cn.cslg.pas.common.vo.business.PatentNoVO; import cn.cslg.pas.common.vo.es.EsCustomFieldBatchVO; import cn.cslg.pas.exception.XiaoShiException; import cn.cslg.pas.service.business.es.EsCustomFieldService; import cn.cslg.pas.service.business.es.EsPatentService; import cn.cslg.pas.service.business.es.EsService; import cn.cslg.pas.service.common.FileManagerService; import lombok.extern.slf4j.Slf4j; import org.apache.pdfbox.multipdf.PDFMergerUtility; import org.apache.pdfbox.io.MemoryUsageSetting; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.rendering.PDFRenderer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import java.io.ByteArrayOutputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * @Author xiexiang * @Date 2024/1/4 */ @Slf4j @Service public class PDFExportFirstPageService { @Autowired private FileManagerService fileManagerService; @Autowired private EsService esService; @Autowired private EsCustomFieldService esCustomFieldService; @Autowired private EsPatentService patentService; public byte[] mergePDFFirstPage(EsCustomFieldBatchVO EsVO) throws IOException { //初始化 Integer startNum = EsVO.getStartNumber(); Integer endNum = EsVO.getEndNumber(); List isAdd = EsVO.getIsAdd(); List isDel = EsVO.getIsDelete(); // 创建一个PDFMergerUtility实例 try { if (!CollectionUtils.isEmpty(isAdd) && startNum <= 1 && endNum < 1) { isAdd.removeAll(isDel); //创建一个输出流 byte[] result = this.mergePerPatentPDF(isAdd); return result; } else if (startNum > 1 && endNum > 0){ List patentNos = new ArrayList<>(); try { patentNos = esCustomFieldService.getPatentNos(EsVO); byte[] result = this.mergePerPatentPDF(patentNos); return result; } catch (Exception e) { throw new XiaoShiException(""); } } else { return new byte[0]; } } catch (FileNotFoundException e) { throw new FileNotFoundException(); } } // /** // * 1 // * @param EsVO // * @return // * @throws IOException // */ // public byte[] mergeAndExportPDFFirstPage(EsCustomFieldBatchVO EsVO) throws IOException { // //根据projectId查询专利信息 // QueryRequest queryRequest = new QueryRequest(); // queryRequest.setProjectId(projectId); // PatentDTO patentDTO; // try { // patentDTO = esService.esSearch(queryRequest); // } catch (Exception e) { // throw new XiaoShiException("检索专利错误"); // } // List patents = patentDTO.getPatents(); // byte[][] pdfDocuments = this.getPDFByteByAppNo(patents); // byte[] pdfData = new byte[0]; // // 判断 byte[][] 数组是否为空 // if (!this.isByteEmpty(pdfDocuments)) { // pdfData = this.mergeFirstPage(pdfDocuments); // } // return pdfData; // } /** * 判断文件数组是否为空 * @param pdfDocuments * @return */ private boolean isByteEmpty(byte[][] pdfDocuments) { return pdfDocuments == null || pdfDocuments.length == 0 || Arrays.stream(pdfDocuments).allMatch(array -> array == null || array.length == 0); } /** * 3 * @param pdfDatas * @return * @throws IOException */ public byte[] mergeFirstPage(byte[][] pdfDatas) throws IOException{ //创建一个输出流 try { // 创建一个PDFMergerUtility实例 PDFMergerUtility merger = new PDFMergerUtility(); PDDocument resultDocument = new PDDocument(); //逐个处理每个pdf文件 for (byte[] pdfData : pdfDatas) { // 加载每个输入的PDF文档 PDDocument document = PDDocument.load(pdfData); // 获取第一页 PDPage firstPage = document.getPage(0); // 创建一个新的文档用于保存第一页 PDDocument firstPageDocument = new PDDocument(); firstPageDocument.addPage(firstPage); // 将第一页添加到合并器中 merger.appendDocument(resultDocument, firstPageDocument); // 关闭当前处理的文档 document.close(); firstPageDocument.close(); } ByteArrayOutputStream out = new ByteArrayOutputStream(); resultDocument.save(out); resultDocument.close(); return out.toByteArray(); } catch (FileNotFoundException e) { throw new FileNotFoundException(); } } /** * 2 * 根据查询到的专利信息获取它公告或公开专利的pdf文档数据流 * @param patents * @return */ public byte[][] getPDFByteByAppNo(List patents){ List pdfDatas = new ArrayList<>(); //遍历传入专利集合 for (PatentColumnDTO item : patents) { //判断申请号是否为空 if (item.getAppNo() != null) { String appNo = item.getAppNo(); Integer type = 1; //获取公告专利pdf文档guid String pdfGuid = FormatUtil.getPDFFormat(appNo, type); byte[] pdfData = new byte[0]; try { pdfData = fileManagerService.downloadSystemFileFromFMS(pdfGuid); if (pdfData == null || pdfData.length == 0) { //获取公开专利pdf文档guid Integer type2 = 0; String pdfGuid2 = FormatUtil.getPDFFormat(appNo, type2); pdfData = fileManagerService.downloadSystemFileFromFMS(pdfGuid2); if (pdfData == null || pdfData.length == 0) { continue; } else { pdfDatas.add(pdfData); } } else { pdfDatas.add(pdfData); } } catch (IOException e) { e.printStackTrace(); continue;//继续处理下一件专利 } } } byte[][] pdfDataArray = new byte[pdfDatas.size()][]; return pdfDatas.toArray(pdfDataArray); } public byte[] mergePerPatentPDF(List patentNos) throws IOException { PDFMergerUtility merger = new PDFMergerUtility(); PDDocument resultDocument = new PDDocument(); for (String patentNo : patentNos) { PatentNoVO patentNoVO = new PatentNoVO(); patentNoVO.setPatentNo(patentNo); try { PatentColumnDTO patentColumnDTO = patentService.selectPatentDetail(patentNoVO); if (patentColumnDTO != null) { //判断申请号是否为空 if (patentColumnDTO.getAppNo() != null) { String appNo = patentColumnDTO.getAppNo(); Integer type = 1; //获取公告专利pdf文档guid String pdfGuid = FormatUtil.getPDFFormat(appNo, type); byte[] pdfData = new byte[0]; try { pdfData = fileManagerService.downloadSystemFileFromFMS(pdfGuid); if (pdfData == null || pdfData.length == 0) { //获取公开专利pdf文档guid Integer type2 = 0; String pdfGuid2 = FormatUtil.getPDFFormat(appNo, type2); pdfData = fileManagerService.downloadSystemFileFromFMS(pdfGuid2); if (pdfData == null || pdfData.length == 0) { continue; } else { // 加载输入的PDF文档 PDDocument document = PDDocument.load(pdfData); // 获取第一页 PDPage firstPage = document.getPage(0); // 创建一个新的文档用于保存第一页 PDDocument firstPageDocument = new PDDocument(); firstPageDocument.addPage(firstPage); // 将第一页添加到合并器中 merger.appendDocument(resultDocument, firstPageDocument); // 关闭当前处理的文档 document.close(); firstPageDocument.close(); } } else { // 加载每个输入的PDF文档 PDDocument document = PDDocument.load(pdfData); // 获取第一页 PDPage firstPage = document.getPage(0); // 创建一个新的文档用于保存第一页 PDDocument firstPageDocument = new PDDocument(); firstPageDocument.addPage(firstPage); // 将第一页添加到合并器中 merger.appendDocument(resultDocument, firstPageDocument); // 关闭当前处理的文档 document.close(); firstPageDocument.close(); } } catch (IOException e) { e.printStackTrace(); continue;//继续处理下一件专利 } } else { continue; } } else { continue; } } catch (Exception e) { continue; } } ByteArrayOutputStream out = new ByteArrayOutputStream(); resultDocument.save(out); resultDocument.close(); return out.toByteArray(); } }