pdf解析法院協助單特定字段,開源項目,結合若依項目進行開發,不連互聯網,本地開發部署,前端使用vue3技術,后端用若依分離版spring botot技術,實現將pdf法院協助執行通知書中的特定字段如:時間、文號、戶名、開戶行、賬號、劃扣金額、保單號等識別出來,并插入數據庫表。
1. 后端開發
(1) 添加 Maven 依賴
在若依分離版項目的 pom.xml 中添加必要的依賴項:
<dependencies><!-- Apache PDFBox --><dependency><groupId>org.apache.pdfbox</groupId><artifactId>pdfbox</artifactId><version>2.0.27</version></dependency><!-- 其他必要依賴 -->
</dependencies>
(2) 創建 PDF 解析服務
創建一個服務類用于解析 PDF 文件并提取特定字段。
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 {@Autowiredprivate 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 文件并調用上述服務。
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 {@Autowiredprivate PdfParseService pdfParseService;@Autowiredprivate 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) 數據庫表設計
創建一個用于存儲解析結果的表。例如:
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 文件并與后端交互。
<template><div><el-uploadclass="upload-demo"dragaction="/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 服務器。