|
@@ -17,7 +17,10 @@ import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.awt.image.BufferedImage;
|
|
|
import java.io.File;
|
|
|
+import java.io.FileInputStream;
|
|
|
import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
|
|
|
/**
|
|
@@ -36,18 +39,44 @@ public class PatentPDFService extends ServiceImpl<PatentInstructionMapper, Paten
|
|
|
log.info("开始处理【导出多件专利pdf首页】的业务,业务参数为patentNos:{}", patentNos);
|
|
|
|
|
|
//根据专利号patentNos查询出所有pdf文档数据
|
|
|
- List<PatentInstruction> patentInstructions = this.list(new LambdaQueryWrapper<PatentInstruction>().in(PatentInstruction::getPatentNo, patentNos));
|
|
|
+ List<PatentInstruction> patentInstructions = this.list(new LambdaQueryWrapper<PatentInstruction>().in(PatentInstruction::getPatentNo, patentNos).groupBy(PatentInstruction::getPatentNo));
|
|
|
|
|
|
- for (PatentInstruction patentInstruction : patentInstructions) {
|
|
|
+ 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);
|
|
|
- File pdfFirstPageFile = new File("合并多件专利的pdf首页.pdf");
|
|
|
- neededDoc.save(pdfFirstPageFile);
|
|
|
+ String newFilePath = "专利号" + patentInstruction.getPatentNo() + "说明书pdf首页.pdf";
|
|
|
+ //File file = new File(newFilePath);
|
|
|
+ neededDoc.save(newFilePath);
|
|
|
+ filePaths.add(newFilePath);
|
|
|
+ neededDoc.close();
|
|
|
+ doc.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ //开始合并上面生成的所有专利说明书首页pdf文件
|
|
|
+ PDFMergerUtility mergePdf = new PDFMergerUtility();
|
|
|
+ List<InputStream> inputStreams = new ArrayList<>();
|
|
|
+ for (String filePath : filePaths) {
|
|
|
+ inputStreams.add(new FileInputStream(filePath));
|
|
|
+ }
|
|
|
+ mergePdf.addSources(inputStreams);
|
|
|
+ // 设置合并生成pdf文件名称
|
|
|
+ mergePdf.setDestinationFileName("多件专利说明书pdf首页合并后的pdf.pdf");
|
|
|
+ // 合并PDF
|
|
|
+ mergePdf.mergeDocuments();
|
|
|
+ for (InputStream in : inputStreams) {
|
|
|
+ if (in != null) {
|
|
|
+ in.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
+ //最后删除所有pdf首页临时文件
|
|
|
+ for (String filePath : filePaths) {
|
|
|
+ new File(filePath).delete();
|
|
|
}
|
|
|
|
|
|
}
|