pdf解析法院协助单特定字段,开源项目,结合若依项目进行开发,不连互联网,本地开发部署,前端使用vue3技术,后端用若依分离版spring botot技术,实现将pdf法院协助执行通知书中的特定字段如:时间、文号、户名、开户行、账号、划扣金额、保单号等识别出来,并插入数据库表。
1. 后端开发
(1) 添加 Maven 依赖
在若依分离版项目的 pom.xml 中添加必要的依赖项:
csharp
<dependencies>
<!-- Apache PDFBox -->
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.27</version>
</dependency>
<!-- 其他必要依赖 -->
</dependencies>
(2) 创建 PDF 解析服务
创建一个服务类用于解析 PDF 文件并提取特定字段。
java
package com.ruoyi.system.service.impl;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.springframework.stereotype.Service;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@Service
public class PdfParseService {
/**
* 解析 PDF 文件并提取特定字段
*/
public Map<String, String> extractFieldsFromPdf(File pdfFile) throws Exception {
Map<String, String> fields = new HashMap<>();
try (PDDocument document = PDDocument.load(pdfFile)) {
PDFTextStripper pdfStripper = new PDFTextStripper();
String content = pdfStripper.getText(document);
// 提取特定字段(根据实际模板调整正则表达式)
fields.put("notice_time", extractField(content, "通知时间:(\\S+)"));
fields.put("document_number", extractField(content, "文号:(\\S+)"));
fields.put("account_name", extractField(content, "户名:(\\S+)"));
fields.put("bank_name", extractField(content, "开户行:(\\S+)"));
fields.put("account_number", extractField(content, "账号:(\\S+)"));
fields.put("amount", extractField(content, "划扣金额:(\\S+)"));
fields.put("policy_number", extractField(content, "保单号:(\\S+)"));
}
return fields;
}
/**
* 使用正则表达式提取字段
*/
private String extractField(String text, String regex) {
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(text);
return matcher.find() ? matcher.group(1) : null;
}
}```
(3) 创建数据库操作服务
编写一个服务类将解析结果插入数据库。
```csharp
package com.ruoyi.system.service.impl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.mapper.CourtNoticeMapper;
import com.ruoyi.system.domain.CourtNotice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
@Service
public class CourtNoticeService {
@Autowired
private CourtNoticeMapper courtNoticeMapper;
/**
* 插入解析结果到数据库
*/
public void saveToDatabase(Map<String, String> fields) {
CourtNotice notice = new CourtNotice();
notice.setNoticeTime(DateUtils.parseDate(fields.get("notice_time")));
notice.setDocumentNumber(fields.get("document_number"));
notice.setAccountName(fields.get("account_name"));
notice.setBankName(fields.get("bank_name"));
notice.setAccountNumber(fields.get("account_number"));
notice.setAmount(Double.parseDouble(fields.getOrDefault("amount", "0")));
notice.setPolicyNumber(fields.get("policy_number"));
notice.setCreateTime(DateUtils.getNowDate());
courtNoticeMapper.insertCourtNotice(notice);
}
}
(4) 创建 Controller 接口
编写一个接口接收上传的 PDF 文件并调用上述服务。
csharp
package com.ruoyi.system.controller;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.service.PdfParseService;
import com.ruoyi.system.service.CourtNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
@RestController
@RequestMapping("/api/pdf")
public class PdfController extends BaseController {
@Autowired
private PdfParseService pdfParseService;
@Autowired
private CourtNoticeService courtNoticeService;
@PostMapping("/parse")
public AjaxResult parsePdf(@RequestParam("file") MultipartFile file) {
try {
// 保存上传的文件到临时目录
File tempFile = File.createTempFile("upload", ".pdf");
try (FileOutputStream fos = new FileOutputStream(tempFile)) {
fos.write(file.getBytes());
}
// 解析 PDF 并提取字段
var fields = pdfParseService.extractFieldsFromPdf(tempFile);
// 将解析结果插入数据库
courtNoticeService.saveToDatabase(fields);
return success("解析成功!");
} catch (Exception e) {
e.printStackTrace();
return error("解析失败:" + e.getMessage());
}
}
}
(5) 数据库表设计
创建一个用于存储解析结果的表。例如:
csharp
CREATE TABLE court_notice (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
notice_time DATETIME NOT NULL COMMENT '通知时间',
document_number VARCHAR(50) NOT NULL COMMENT '文号',
account_name VARCHAR(100) NOT NULL COMMENT '户名',
bank_name VARCHAR(100) NOT NULL COMMENT '开户行',
account_number VARCHAR(50) NOT NULL COMMENT '账号',
amount DECIMAL(10, 2) NOT NULL COMMENT '划扣金额',
policy_number VARCHAR(50) COMMENT '保单号',
create_time DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间'
);
2. 前端开发
(1) 安装 Axios
确保前端项目中已经安装了 axios:
(2) 创建上传组件
在若依分离版的前端项目中,创建一个页面用于上传 PDF 文件并与后端交互。
html
<template>
<div>
<el-upload
class="upload-demo"
drag
action="/api/pdf/parse"
:headers="headers"
:on-success="handleSuccess"
:on-error="handleError"
accept=".pdf"
>
<i class="el-icon-upload"></i>
<div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
<template #tip>
<div class="el-upload__tip">只能上传 PDF 文件</div>
</template>
</el-upload>
<div v-if="message">{{ message }}</div>
</div>
</template>
<script>
import { ref } from 'vue';
import axios from 'axios';
export default {
setup() {
const message = ref('');
const token = localStorage.getItem('token'); // 获取用户的登录令牌
const headers = {
Authorization: `Bearer ${token}`, // 若依分离版需要携带 JWT Token
};
const handleSuccess = (response) => {
console.log('成功响应:', response);
message.value = '解析成功!';
};
const handleError = (error) => {
console.error('错误信息:', error);
message.value = '解析失败:' + error.message;
};
return {
message,
headers,
handleSuccess,
handleError,
};
},
};
</script>
<style scoped>
.upload-demo {
width: 50%;
margin: auto;
}
</style>
3. 测试与部署
后端测试:
确保 PDF 文件上传接口正常工作。
验证解析逻辑是否能正确提取字段并插入数据库。
前端测试:
在前端页面上传 PDF 文件,验证解析结果和提示信息。
部署:
打包后端 Spring Boot 项目为 JAR 文件,并部署到服务器。
打包前端 Vue 项目为静态资源文件,并部署到 Nginx 或其他 Web 服务器。