|
@@ -1,10 +1,15 @@
|
|
|
package cn.cslg.pas.service.patentPDF;
|
|
|
|
|
|
+import cn.cslg.pas.common.model.dto.PatentDTO;
|
|
|
+import cn.cslg.pas.common.model.vo.PatentVO;
|
|
|
import cn.cslg.pas.common.utils.FileUtils;
|
|
|
+import cn.cslg.pas.common.utils.StringUtils;
|
|
|
import cn.cslg.pas.domain.PatentInstruction;
|
|
|
import cn.cslg.pas.mapper.PatentInstructionMapper;
|
|
|
import cn.cslg.pas.service.PatentInstructionService;
|
|
|
+import cn.cslg.pas.service.PatentService;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
@@ -15,13 +20,12 @@ import org.apache.pdfbox.rendering.ImageType;
|
|
|
import org.apache.pdfbox.rendering.PDFRenderer;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
import java.awt.image.BufferedImage;
|
|
|
-import java.io.File;
|
|
|
-import java.io.FileInputStream;
|
|
|
-import java.io.IOException;
|
|
|
-import java.io.InputStream;
|
|
|
+import java.io.*;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* 查询专利的说明书pdf首页
|
|
@@ -33,52 +37,69 @@ import java.util.List;
|
|
|
@Slf4j
|
|
|
@Service
|
|
|
public class PatentPDFService extends ServiceImpl<PatentInstructionMapper, PatentInstruction> {
|
|
|
+ private final PatentService patentService;
|
|
|
private final FileUtils fileUtils;
|
|
|
|
|
|
- public void queryPatentPdfFirstPages(List<String> patentNos) throws IOException {
|
|
|
- log.info("开始处理【导出多件专利pdf首页】的业务,业务参数为patentNos:{}", patentNos);
|
|
|
+ public void queryPatentPdfFirstPages(PatentVO params, HttpServletResponse response) throws IOException {
|
|
|
+ log.info("开始处理【导出多件专利pdf首页】的业务,业务参数为:{}", params);
|
|
|
+
|
|
|
+ //根据筛选条件,直接调用查询
|
|
|
+ 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).groupBy(PatentInstruction::getPatentNo));
|
|
|
|
|
|
- ArrayList<String> filePaths = new ArrayList<>(); //所有专利说明书首页pdf文件路径
|
|
|
+ ArrayList<String> filePaths = new ArrayList<>(); //存放所有专利说明书首页pdf的文件路径
|
|
|
for (PatentInstruction patentInstruction : patentInstructions) { //遍历,将所有专利说明书pdf的首页取出生成新的Pdf文件存在本地
|
|
|
String filePath = fileUtils.getSystemPath() + patentInstruction.getUrl();
|
|
|
PDDocument doc = PDDocument.load(new File(filePath));
|
|
|
Splitter splitter = new Splitter();
|
|
|
splitter.setStartPage(1);
|
|
|
splitter.setEndPage(1);
|
|
|
- PDDocument neededDoc = splitter.split(doc).get(0);
|
|
|
- String newFilePath = "专利号" + patentInstruction.getPatentNo() + "说明书pdf首页.pdf";
|
|
|
- //File file = new File(newFilePath);
|
|
|
- neededDoc.save(newFilePath);
|
|
|
+ PDDocument needDoc = splitter.split(doc).get(0);
|
|
|
+ String newFilePath = "专利号" + patentInstruction.getPatentNo() + "的说明书pdf首页.pdf";
|
|
|
+ needDoc.save(newFilePath);
|
|
|
filePaths.add(newFilePath);
|
|
|
- neededDoc.close();
|
|
|
+ needDoc.close();
|
|
|
doc.close();
|
|
|
}
|
|
|
|
|
|
//开始合并上面生成的所有专利说明书首页pdf文件
|
|
|
- PDFMergerUtility mergePdf = new PDFMergerUtility();
|
|
|
- List<InputStream> inputStreams = new ArrayList<>();
|
|
|
+ PDFMergerUtility pdfMerger = new PDFMergerUtility();
|
|
|
for (String filePath : filePaths) {
|
|
|
- inputStreams.add(new FileInputStream(filePath));
|
|
|
+ pdfMerger.addSource(filePath);
|
|
|
}
|
|
|
- mergePdf.addSources(inputStreams);
|
|
|
// 设置合并生成pdf文件名称
|
|
|
- mergePdf.setDestinationFileName("多件专利说明书pdf首页合并后的pdf.pdf");
|
|
|
+ String mergedFilePath = StringUtils.getUUID() + ".pdf";
|
|
|
+ pdfMerger.setDestinationFileName(mergedFilePath);
|
|
|
// 合并PDF
|
|
|
- mergePdf.mergeDocuments();
|
|
|
- for (InputStream in : inputStreams) {
|
|
|
- if (in != null) {
|
|
|
- in.close();
|
|
|
- }
|
|
|
- }
|
|
|
+ pdfMerger.mergeDocuments();
|
|
|
|
|
|
- //最后删除所有pdf首页临时文件
|
|
|
+ //最后要记得删除所有pdf首页临时文件
|
|
|
for (String filePath : filePaths) {
|
|
|
new File(filePath).delete();
|
|
|
}
|
|
|
|
|
|
+ InputStream fis = new BufferedInputStream(new FileInputStream(mergedFilePath));
|
|
|
+ byte[] buffer = new byte[fis.available()];
|
|
|
+ fis.read(buffer);
|
|
|
+ fis.close();
|
|
|
+ // 清空response
|
|
|
+ response.reset();
|
|
|
+ // 设置response的Header
|
|
|
+ response.addHeader("Content-Disposition", "attachment;filename=" + new String(mergedFilePath.getBytes()));
|
|
|
+ response.addHeader("Content-Length", "" + new File(mergedFilePath).length());
|
|
|
+ OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
|
|
|
+ response.setContentType("application/octet-stream");
|
|
|
+ toClient.write(buffer);
|
|
|
+ toClient.flush();
|
|
|
+ toClient.close();
|
|
|
+
|
|
|
+ //最后要记得删除合并后的pdf临时文件
|
|
|
+ new File(mergedFilePath).delete();
|
|
|
+
|
|
|
}
|
|
|
|
|
|
}
|