zhouc PDF拆分
This commit is contained in:
parent
965429050c
commit
ceb954b540
@ -35,7 +35,12 @@
|
||||
<artifactId>wmyun-module-infra-api</artifactId>
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- PDFBox 核心库 -->
|
||||
<dependency>
|
||||
<groupId>org.apache.pdfbox</groupId>
|
||||
<artifactId>pdfbox</artifactId>
|
||||
<version>2.0.29</version>
|
||||
</dependency>
|
||||
<!-- 业务组件 -->
|
||||
<dependency>
|
||||
<groupId>com.wmyun</groupId>
|
||||
@ -151,6 +156,12 @@
|
||||
<groupId>org.dromara.hutool</groupId>
|
||||
<artifactId>hutool-extra</artifactId> <!-- 邮件 -->
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.protobuf</groupId>
|
||||
<artifactId>protobuf-java</artifactId>
|
||||
<version>3.25.2</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
@ -0,0 +1,74 @@
|
||||
package com.wmyun.module.system.controller.admin.pdf;
|
||||
|
||||
import com.wmyun.module.system.controller.admin.pdf.vo.PdfResultVO;
|
||||
import com.wmyun.module.system.service.pdf.PdfSplitService;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.Value;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author zhouc
|
||||
* @date 2025/3/1 14:33
|
||||
* @className PDFController
|
||||
* @description
|
||||
*/
|
||||
@Tag(name = "管理后台 - PDF处理")
|
||||
@RestController
|
||||
@RequestMapping("/system/pdf")
|
||||
@Validated
|
||||
@Transactional
|
||||
public class PDFController {
|
||||
|
||||
@Resource
|
||||
private PdfSplitService pdfSplitService;
|
||||
|
||||
private String uploadDir; // 配置文件设置存储路径
|
||||
|
||||
@PostMapping("/split_result")
|
||||
public ResponseEntity<?> splitPdfresult(@RequestParam("file") MultipartFile file) {
|
||||
try {
|
||||
// 保存临时文件
|
||||
File tempFile = File.createTempFile("split-", ".pdf");
|
||||
file.transferTo(tempFile);
|
||||
|
||||
// 执行拆分
|
||||
List<PdfResultVO> resultList = pdfSplitService.splitPdf(
|
||||
tempFile,
|
||||
uploadDir
|
||||
);
|
||||
|
||||
// 返回结果
|
||||
return ResponseEntity.ok(resultList);
|
||||
|
||||
} catch (IOException e) {
|
||||
return ResponseEntity.status(500)
|
||||
.body(Map.of("error", "处理失败: " + e.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping("/split")
|
||||
public ResponseEntity<String> splitPdf(
|
||||
@RequestParam("file") MultipartFile file,
|
||||
@RequestParam(defaultValue = "1") int startPage,
|
||||
@RequestParam(defaultValue = "1") int endPage) {
|
||||
try {
|
||||
File tempFile = File.createTempFile("temp", ".pdf");
|
||||
file.transferTo(tempFile);
|
||||
String outputPath = "/output/";
|
||||
pdfSplitService.splitPdfByPages(tempFile, outputPath, startPage, endPage);
|
||||
return ResponseEntity.ok(" 拆分成功");
|
||||
} catch (IOException e) {
|
||||
return ResponseEntity.status(500).body(" 处理失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package com.wmyun.module.system.controller.admin.pdf.vo;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author zhouc
|
||||
* @date 2025/3/1 15:21
|
||||
* @className PdfResultVO
|
||||
* @description
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class PdfResultVO {
|
||||
private String fileName; // 拆分后的文件名
|
||||
private String filePath; // 服务器存储路径
|
||||
private Long fileSize; // 文件大小(字节)
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package com.wmyun.module.system.service.pdf;
|
||||
|
||||
import com.wmyun.module.system.controller.admin.pdf.vo.PdfResultVO;
|
||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public interface PdfSplitService {
|
||||
List<PdfResultVO> splitPdf(File sourceFile, String outputDir);
|
||||
|
||||
void splitPdfByPages(File sourceFile, String outputDir, int startPage, int endPage);
|
||||
|
||||
void splitAllPages(File sourceFile, String outputDir);
|
||||
}
|
@ -0,0 +1,76 @@
|
||||
package com.wmyun.module.system.service.pdf;
|
||||
|
||||
import com.wmyun.module.system.controller.admin.pdf.vo.PdfResultVO;
|
||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author zhouc
|
||||
* @date 2025/3/1 15:42
|
||||
* @className PdfSplitServiceImpl
|
||||
* @description
|
||||
*/
|
||||
public class PdfSplitServiceImpl {
|
||||
public List<PdfResultVO> splitPdf(File sourceFile, String outputDir) throws IOException {
|
||||
List<PdfResultVO> results = new ArrayList<>();
|
||||
try (PDDocument document = PDDocument.load(sourceFile)) {
|
||||
int totalPages = document.getNumberOfPages();
|
||||
|
||||
for (int i = 0; i < totalPages; i++) {
|
||||
// 创建单页文档
|
||||
PDDocument singlePageDoc = new PDDocument();
|
||||
singlePageDoc.addPage(document.getPage(i));
|
||||
|
||||
// 生成文件名和路径
|
||||
String fileName = "page_" + (i + 1) + ".pdf";
|
||||
File outputFile = new File(outputDir, fileName);
|
||||
singlePageDoc.save(outputFile);
|
||||
|
||||
// 构建返回VO
|
||||
results.add(new PdfResultVO(
|
||||
fileName,
|
||||
outputFile.getAbsolutePath(),
|
||||
outputFile.length()
|
||||
));
|
||||
singlePageDoc.close();
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
public void splitPdfByPages(File sourceFile, String outputDir, int startPage, int endPage) throws IOException {
|
||||
try (PDDocument document = PDDocument.load(sourceFile)) {
|
||||
int totalPages = document.getNumberOfPages();
|
||||
// 校验页码范围
|
||||
if (startPage < 1 || endPage > totalPages) {
|
||||
throw new IllegalArgumentException("页码超出范围");
|
||||
}
|
||||
|
||||
PDDocument newDoc = new PDDocument();
|
||||
for (int i = startPage - 1; i < endPage; i++) {
|
||||
newDoc.addPage(document.getPage(i));
|
||||
}
|
||||
|
||||
String fileName = sourceFile.getName().replace(".pdf",
|
||||
"_split_" + startPage + "-" + endPage + ".pdf");
|
||||
newDoc.save(new File(outputDir, fileName));
|
||||
newDoc.close();
|
||||
}
|
||||
}
|
||||
|
||||
public void splitAllPages(File sourceFile, String outputDir) throws IOException {
|
||||
try (PDDocument document = PDDocument.load(sourceFile)) {
|
||||
int totalPages = document.getNumberOfPages();
|
||||
for (int i = 0; i < totalPages; i++) {
|
||||
PDDocument singlePageDoc = new PDDocument();
|
||||
singlePageDoc.addPage(document.getPage(i));
|
||||
singlePageDoc.save(new File(outputDir, "page_" + (i + 1) + ".pdf"));
|
||||
singlePageDoc.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user