一般找人做網(wǎng)站多少錢全球搜是什么公司
文章目錄
- 參考文章
- 技術(shù)棧
- 需求
- 解析發(fā)票類型
- 1. 最終項(xiàng)目結(jié)構(gòu)
- 1.1 說明
- 2. 相關(guān)代碼
- 2.1 導(dǎo)入相應(yīng)的maven依賴
- 2.2 實(shí)體類
- 2.3 工具類
- 2.4 三層架構(gòu)
- controller
- service
- mapper
參考文章
參考文章
技術(shù)棧
SpringBoot+Vue
需求
本文主要是實(shí)現(xiàn)提取發(fā)票中的部分內(nèi)容,并實(shí)現(xiàn)自動(dòng)回填到頁面中對應(yīng)位置。
解析發(fā)票類型
1. 最終項(xiàng)目結(jié)構(gòu)
新建一個(gè)Springboot項(xiàng)目
1.1 說明
- 實(shí)體 entity 包下
- 主要有兩個(gè)實(shí)體,分別是發(fā)票實(shí)體NewInvoice,和發(fā)票備注實(shí)體Note
- 工具 utils 包下
- PDF類用于解析PDF文件
- InvoiceRegexEnum 類是發(fā)票信息的正則表達(dá)式枚舉類,提取發(fā)票信息所用到的正則表達(dá)式都在這個(gè)枚舉類中
2. 相關(guān)代碼
2.1 導(dǎo)入相應(yīng)的maven依賴
<!-- 添加依賴開始位置--><dependency><groupId>org.apache.pdfbox</groupId><artifactId>pdfbox</artifactId><version>2.0.21</version></dependency><dependency><groupId>org.apache.pdfbox</groupId><artifactId>fontbox</artifactId><version>2.0.21</version></dependency><dependency><groupId>org.apache.pdfbox</groupId><artifactId>jempbox</artifactId><version>1.8.13</version></dependency><dependency><groupId>org.apache.pdfbox</groupId><artifactId>xmpbox</artifactId><version>2.0.0</version></dependency><dependency><groupId>org.apache.pdfbox</groupId><artifactId>preflight</artifactId><version>2.0.0</version></dependency><dependency><groupId>org.apache.pdfbox</groupId><artifactId>pdfbox-tools</artifactId><version>2.0.0</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>5.0.0</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>5.0.0</version></dependency><dependency><groupId>com.google.zxing</groupId><artifactId>core</artifactId><version>3.1.0</version></dependency><dependency><groupId>com.google.zxing</groupId><artifactId>javase</artifactId><version>3.1.0</version></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.12.0</version></dependency><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.11.0</version></dependency><dependency><groupId>commons-codec</groupId><artifactId>commons-codec</artifactId></dependency><!-- 添加依賴結(jié)束位置-->
2.2 實(shí)體類
電子發(fā)票實(shí)體類NewInvoice
package com.example.pdf.entity;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;import java.math.BigDecimal;/*** 電子發(fā)票實(shí)體類*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class NewInvoice {
// private String fileAbsolutePath; // 文件絕對路徑private String number; // 發(fā)票號碼private String date; // 開票日期
// private String sellerName; // 銷售方名稱private BigDecimal amount; // 合計(jì)金額private BigDecimal taxAmount; // 合計(jì)稅額private BigDecimal totalAmount; // 價(jià)稅合計(jì)金額private Note Detail; // 發(fā)票備注信息private String note; // 備注}
備注實(shí)體類Note
package com.example.pdf.entity;import lombok.Data;/*** 發(fā)票備注信息*/
@Data
public class Note {private String buyer; // 購方private String buyerAccount; // 購方銀行賬號private String seller; // 銷方private String sellerAccount; // 銷方銀行賬號private String payee; // 收款人private String checker; // 復(fù)核人
// @Override
// public String toString() {
// return "購方開戶銀行:" + buyer + "; "+
// "銀行賬號:" + buyerAccount + ";" +"\n"+
// "銷方開戶銀行:" + seller + "; "+
// "銀行賬號:" + sellerAccount +";" +"\n\n"+
// " 收款人:" + payee + ";" +" "+
// " 復(fù)核人:" + checker +";";
// }
}
2.3 工具類
正則表達(dá)式枚舉類
package com.example.pdf.utils;/*** 正則表達(dá)式枚舉類*/
public enum InvoiceRegexEnum {/*** 機(jī)器編碼、發(fā)票代碼、發(fā)票號碼、開票日期和校驗(yàn)碼的提取正則*/REGULAR_A("機(jī)器編號:(?<machineNumber>\\d{12})|發(fā)票代碼:(?<code>\\d{12})|發(fā)票號碼:(?<number>\\d{8})|:(?<date>\\d{4}年\\d{2}月\\d{2}日)|校驗(yàn)碼:(?<checksum>\\d{20}|\\S{4,})"),/*** 新版發(fā)票的機(jī)器編碼、發(fā)票代碼、發(fā)票號碼、開票日期和校驗(yàn)碼的提取正則*/REGULAR_A_NEW("發(fā)票號碼:(?<number>\\d{20})|:(?<date>\\d{4}年\\d{2}月\\d{2}日)|(售名稱|銷名稱):(?<name>\\S*)"),/*** 發(fā)票號碼備用提取正則*/REGULAR_A_1("(國制|制普通發(fā)票)(?<number>\\d{8})"),/*** 發(fā)票號碼跨行提取正則*/REGULAR_A_1R("發(fā)票號碼:(?<number>\\d{7})[\\s\\S]*?(\\d+)"),/*** 開票日期備用提取正則*/REGULAR_A_2("開票日期:(?<date>\\d{4}\\d{2}月\\d{2}日)"),/*** 發(fā)票代碼備用提取正則*/REGULAR_A_3("發(fā)票代碼(?<code>\\d{12})"),/*** 發(fā)票代碼跨行提取正則*/REGULAR_A_3R("發(fā)票代碼:(?<code>\\d{10})[\\s\\S]*?(\\d+)"),/*** 金額、稅額提取正則,匹配形如 "合計(jì)¥?金額¥?稅額" 的文本*/REGULAR_B("合計(jì)¥?(?<amount>[^ \\f\\n\\r\\t\\v*]*)(?:¥?(?<taxAmount>\\S*)|\\*+)\\s"),/*** 金額提取正則,用于匹配結(jié)果有誤的修正*/REGULAR_BR("合計(jì)¥(?<amount>\\d+\\.\\d+)"),/*** 金額、稅額備用提取正則*/REGULAR_B_1("合\\u0020*計(jì)\\u0020*¥?(?<amount>[^ ]*)\\u0020+¥?(?:(?<taxAmount>\\S*)|\\*+)\\s"),/*** 價(jià)稅合計(jì)提取正則,匹配“價(jià)稅合計(jì)(大寫)XXX(小寫)¥YYY”格式的文本*/REGULAR_C("價(jià)稅合計(jì)\\u0028大寫\\u0029(?<amountString>\\S*)\\u0028小寫\\u0029¥?(?<amount>\\S*)\\s"),/*** 收款人、復(fù)核、開票人、銷售方提取正則,匹配格式為“收款人:xxx復(fù)核:xxx開票人:xxx銷售方”的字符串*/REGULAR_D("收款人:(?<payee>\\S*)復(fù)核:(?<reviewer>\\S*)開票人:(?<drawer>\\S*)銷售方"),/*** 發(fā)票類型提取正則,匹配"xxx通發(fā)票"格式的發(fā)票類型*/REGULAR_E("(?<p>\\S*)通發(fā)票"),/*** 發(fā)票類型提取正則,匹配"xxx用發(fā)票"格式的發(fā)票類型*/REGULAR_E_1("(?<p>\\S*)用發(fā)票"),/*** 發(fā)票類型提取 - 輔助正則*/REGULAR_E_AUX("(?:國|統(tǒng)|一|發(fā)|票|監(jiān)|制)"),/*** 購買方信息提取正則*/REGULAR_F("名稱:(?<name>\\S*)|納稅人識別號:(?<code>\\S*)|地址、電話:(?<address>\\S*)|開戶行及賬號:(?<account>\\S*)|電子支付標(biāo)識:(?<account2>\\S*)"),/*** 針對深圳發(fā)票的銷售方名稱提取正則*/REGULAR_FR("名稱:(?<name>\\S*)"),/*** 處理除了金額和稅額之外的其他文本元素正則*/REGULAR_G("^(-?\\d+)(\\.\\d+)?$"),/*** 備注信息提取正則*/REGULAR_A_NOTE_BUYER("購方開戶銀行:(?<buyer>[^;]+);"),REGULAR_A_NOTE_BUYERACCOUNT("銀行賬號:(?<buyerAccount>\\d+)(?=[,;])"),REGULAR_A_NOTE_SELLER("銷方開戶銀行:(?<seller>.*?)(?=[,;]|\\Z)"),REGULAR_A_NOTE_SELLERACCOUNT("銀行賬號:(?<sellerAccount>\\d+)(?=[,;]|\\Z)"),REGULAR_A_NOTE_PAYEE("收款人:(?<payee>.*?)(?=[,;]|\\Z)"),REGULAR_A_NOTE_CHECKER("復(fù)核人:(?<checker>.*?)(?=[,;]|\\Z)"),/*** 檢查當(dāng)前詳細(xì)項(xiàng)字符串是否符合特定條件正則*/REGULAR_H("\\S+\\d*(%|免稅|不征稅|出口零稅率|普通零稅率)\\S*"),REGULAR_H_1("^ *\\d*(%|免稅|不征稅|出口零稅率|普通零稅率)\\S*"),REGULAR_H_2("\\S+\\d+%[\\-\\d]+\\S*"),REGULAR_H_3("^ *\\d*(%|免稅|不征稅|出口零稅率|普通零稅率)\\S*");private final String regex;InvoiceRegexEnum(String regex) {this.regex = regex;}public String getRegex() {return regex;}
}
解析PDF工具類
package com.example.pdf.utils;import com.example.pdf.entity.NewInvoice;
import com.example.pdf.entity.Note;
import lombok.extern.slf4j.Slf4j;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.text.PDFTextStripper;
import org.springframework.web.multipart.MultipartFile;import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;import static com.example.pdf.utils.InvoiceRegexEnum.*;@Slf4j
public class PDF {private static final String PDF_EXTENSION = ".pdf";/*** 調(diào)用該方法將前端接受到的文件暫存** @param file*/public static NewInvoice parsePdfFile(MultipartFile file) {NewInvoice newInvoice = new NewInvoice();try {// 創(chuàng)建一個(gè)臨時(shí)文件Path tempFile = Files.createTempFile("tempPrefix", ".pdf");File tempFilePath = tempFile.toFile();// 將MultipartFile的內(nèi)容寫入到臨時(shí)文件try (FileOutputStream fos = new FileOutputStream(tempFilePath)) {fos.write(file.getBytes());}// 現(xiàn)在你可以使用臨時(shí)文件的路徑來調(diào)用你的解析方法newInvoice = extractInvoiceInformation(tempFilePath.getAbsolutePath());// 刪除臨時(shí)文件,或者在某些情況下保留它tempFilePath.delete();} catch (IOException e) {// 處理異常e.printStackTrace();}// 返回值return newInvoice;}/*** 提取發(fā)票信息*//*** 提取發(fā)票信息** @param filePath 發(fā)票地址* @return*/public static NewInvoice extractInvoiceInformation(String filePath) {// 指定要處理的文件夾路徑NewInvoice newInvoice1 = newPdfProcessInvoicesInFile(filePath);String note = "";if (newInvoice1.getDetail().getBuyer() != null) {note = "購方開戶銀行:" + newInvoice1.getDetail().getBuyer() + "; ";} else {note = "";}if (newInvoice1.getDetail().getBuyerAccount() != null) {note = note + "銀行賬號:" + newInvoice1.getDetail().getBuyerAccount() + "; ";} else {note = note + "";}if (newInvoice1.getDetail().getSeller() != null) {note = note + "銷方開戶銀行:" + newInvoice1.getDetail().getSeller() + "; ";} else {note = note + "";}if (newInvoice1.getDetail().getSellerAccount() != null) {note = note + "銀行賬號:" + newInvoice1.getDetail().getSellerAccount() + "; ";} else {note = note + "";}if (newInvoice1.getDetail().getPayee() != null) {note = note + " 收款人:" + newInvoice1.getDetail().getPayee() + ";" + " ";} else {note = note + "";}if (newInvoice1.getDetail().getChecker() != null) {note = note + " 復(fù)核人:" + newInvoice1.getDetail().getChecker() + ";";} else {note = note + "";}newInvoice1.setNote(note);return newInvoice1;}/*** 處理指定的PDF發(fā)票文件** @param filePath 文件路徑* @return 包含提取信息的 NewInvoice 列表*/public static NewInvoice newPdfProcessInvoicesInFile(String filePath) {File file = new File(filePath);NewInvoice returnResult = new NewInvoice();if (isPdfFile(file)) {NewInvoice result = extractInvoice(file.getAbsolutePath()); // 提取文件內(nèi)容if (result != null) {returnResult = createProcessedInvoice(result);// 創(chuàng)建一個(gè)新的發(fā)票對象} else {handleExtractionError(file);}}return returnResult;}/*** 創(chuàng)建一個(gè)處理后的 NewInvoice 對象** @param result 原始的 NewInvoice 對象* @return 處理后的 NewInvoice 對象*/private static NewInvoice createProcessedInvoice(NewInvoice result) {NewInvoice returnResult = new NewInvoice();returnResult.setNumber(result.getNumber());returnResult.setDate(result.getDate());returnResult.setTotalAmount(result.getTotalAmount());returnResult.setAmount(result.getAmount());returnResult.setTaxAmount(result.getTaxAmount());returnResult.setDetail(result.getDetail());return returnResult;}/*** 處理提取失敗的情況,輸出錯(cuò)誤信息** @param file 提取失敗的文件*/private static void handleExtractionError(File file) {log.warn("文件: {}\t提取失敗~~~\n", file.getName());}/*** 檢查文件是否為PDF文件** @param file 要檢查的文件* @return 如果是PDF文件,返回 true,否則返回 false*/private static boolean isPdfFile(File file) {return file.isFile() && file.getName().toLowerCase().endsWith(PDF_EXTENSION);}/*** 從本地文件或URL中提取發(fā)票信息。** @param filePath 本地文件路徑或發(fā)票的URL。* @return 包含提取信息的 NewInvoice 對象。*/private static NewInvoice extractInvoice(String filePath) {File sourceFile = new File(filePath);if (!sourceFile.exists()) {log.error("指定的源文件不存在");}NewInvoice result = null;try {// 調(diào)用函數(shù)解析PDF ,返回 發(fā)票對象【基本信息】result = extract(sourceFile);} catch (Exception e) {e.printStackTrace();result = new NewInvoice();}return result;}/*** 解析PDF 文件,返回發(fā)票對象** @param file PDF文件* @return* @throws IOException*/public static NewInvoice extract(File file) throws IOException {NewInvoice invoice = new NewInvoice(); // 新建發(fā)票對象// 接收一個(gè)表示 PDF 文件路徑的字符串作為參數(shù),并返回一個(gè) PDDocument 對象。// 這個(gè)對象代表了整個(gè)PDF 文檔,可以通過這個(gè)對象來訪問文檔的各個(gè)部分PDDocument doc = PDDocument.load(file);// 從 PDDocument 對象 doc 中獲取第一頁,并將這個(gè)頁面對象賦值給PDPage類型的變量// PDPage 對象代表了文檔中的一個(gè)頁面PDPage firstPage = doc.getPage(0);// 獲取頁面裁剪框?qū)挾?#xff0c;并將寬度四舍五入為整數(shù)// 【頁面裁剪寬度定義了頁面上用于顯示內(nèi)容的區(qū)域】int pageWidth = Math.round(firstPage.getCropBox().getWidth());// PDFTextStripper 用于從PDF文檔中提取文本的工具PDFTextStripper textStripper = new PDFTextStripper(); // 創(chuàng)建一個(gè)實(shí)例textStripper.setSortByPosition(true); // 提取文本時(shí)按照物理位置進(jìn)行排序// 提取整個(gè)文檔的所有文本內(nèi)容,并將這些文本內(nèi)容作為一個(gè)長字符串返回String fullText = textStripper.getText(doc);// 頁面翻轉(zhuǎn)? 不重要if (firstPage.getRotation() != 0) {pageWidth = Math.round(firstPage.getCropBox().getHeight());}// 處理文本中可能有錯(cuò)誤的符號String allText = replace(fullText).replaceAll("(", "(").replaceAll(")", ")").replaceAll("¥", "¥");// 提取 新版發(fā)票的機(jī)器編碼、發(fā)票代碼、發(fā)票號碼、開票日期和檢驗(yàn)碼{Pattern pattern = Pattern.compile(REGULAR_A_NEW.getRegex()); // 新版發(fā)票的機(jī)器編碼、發(fā)票代碼、發(fā)票號碼、開票日期和檢驗(yàn)碼的提取正則Pattern patternNumber = Pattern.compile(REGULAR_A_1.getRegex());// 發(fā)票號碼備用提取正則Pattern patternDate = Pattern.compile(REGULAR_A_2.getRegex()); // 開票日期備用提取正則// matcer 類對于輸入字符串進(jìn)行解釋和匹配操作,這些操作是基于某個(gè)Pattern對象定義的規(guī)則(正則表達(dá)式)進(jìn)行的。// 檢查allText 字符串中是否匹配pattern中定義的正則表達(dá)式的文本Matcher matcher = pattern.matcher(allText);while (matcher.find()) {// 在輸入字符串a(chǎn)llText中查找與模式匹配的第一個(gè)子序列// 如果 提取到發(fā)票號碼,則設(shè)置發(fā)票號碼if (matcher.group("number") != null) {invoice.setNumber(matcher.group("number"));} else if (matcher.group("date") != null) {String rawDate = matcher.group("date"); // 發(fā)票日期,解析日期并設(shè)置日期try {SimpleDateFormat inputDateFormat = new SimpleDateFormat("yyyy年MM月dd日");SimpleDateFormat outputDateFormat = new SimpleDateFormat("yyyy-MM-dd");Date parsedDate = inputDateFormat.parse(rawDate);String formattedDate = outputDateFormat.format(parsedDate);invoice.setDate(formattedDate);} catch (ParseException e) {System.out.println("無法解析日期:" + rawDate);}}// 如果沒有提取到的話使用備用在進(jìn)行提取if (matcher.group("number") == null) {Matcher matcher2 = patternNumber.matcher(allText);if (matcher2.find()) {invoice.setNumber(matcher2.group("number"));}}if (matcher.group("date") == null) {Matcher matcher3 = patternDate.matcher(allText);if (matcher3.find()) {String rawDate = matcher3.group("date");try {SimpleDateFormat inputDateFormat = new SimpleDateFormat("yyyyMM月dd日");SimpleDateFormat outputDateFormat = new SimpleDateFormat("yyyy-MM-dd");Date parsedDate = inputDateFormat.parse(rawDate);String formattedDate = outputDateFormat.format(parsedDate);invoice.setDate(formattedDate);} catch (Exception e) {System.out.println("無法解析日期:" + rawDate);}}}}}// 提取 金額、稅額等{Pattern pattern = Pattern.compile(REGULAR_B.getRegex()); // 金額、稅額提取正則,匹配形如“合計(jì)¥?金額¥?稅額”的文本Matcher matcher = pattern.matcher(allText);if (matcher.find()) {try {invoice.setAmount(new BigDecimal(matcher.group("amount")));} catch (Exception e) {// 不處理}try {invoice.setTaxAmount(new BigDecimal(matcher.group("taxAmount")));} catch (Exception e) {invoice.setTaxAmount(new BigDecimal(0));}}}// 如果沒有提取到,則再使用備用的正則進(jìn)行提取if (null == invoice.getAmount()) {Pattern pattern = Pattern.compile(REGULAR_B_1.getRegex());Matcher matcher = pattern.matcher(fullText);if (matcher.find()) {try {invoice.setAmount(new BigDecimal(matcher.group("amount")));} catch (Exception e) {invoice.setAmount(new BigDecimal(0));}try {invoice.setTaxAmount(new BigDecimal(matcher.group("taxAmount")));} catch (Exception e) {invoice.setTaxAmount(new BigDecimal(0));}}}invoice.setTotalAmount(invoice.getAmount().add(invoice.getTaxAmount()));// 先創(chuàng)建一個(gè)發(fā)票備注實(shí)例Note note = new Note();// 提取發(fā)票備注信息{// 提取購方開戶銀行Pattern patternBuyer = Pattern.compile(REGULAR_A_NOTE_BUYER.getRegex()); // 提取備注信息Pattern patternBuyerAccount = Pattern.compile(REGULAR_A_NOTE_BUYERACCOUNT.getRegex()); // 提取備注信息Pattern patternSeller = Pattern.compile(REGULAR_A_NOTE_SELLER.getRegex()); // 提取備注信息Pattern patternSellerAccount = Pattern.compile(REGULAR_A_NOTE_SELLERACCOUNT.getRegex()); // 提取備注信息Pattern patternPayee = Pattern.compile(REGULAR_A_NOTE_PAYEE.getRegex()); // 提取備注信息Pattern patternChecker = Pattern.compile(REGULAR_A_NOTE_CHECKER.getRegex()); // 提取備注信息Matcher matcher0 = patternBuyer.matcher(allText);if (matcher0.find()) {// 如果查詢到的話就設(shè)置備注信息try {note.setBuyer(new String(matcher0.group("buyer")));} catch (Exception e) {// 不處理}}Matcher matcher1 = patternBuyerAccount.matcher(allText);if (matcher1.find()) {// 如果查詢到的話就設(shè)置備注信息try {note.setBuyerAccount(new String(matcher1.group("buyerAccount")));} catch (Exception e) {// 不處理}}Matcher matcher2 = patternSeller.matcher(allText);if (matcher2.find()) {// 如果查詢到的話就設(shè)置備注信息try {note.setSeller(new String(matcher2.group("seller")));} catch (Exception e) {// 不處理}}Matcher matcher3 = patternSellerAccount.matcher(allText);if (matcher3.find()) {// 如果查詢到的話就設(shè)置備注信息try {note.setSellerAccount(new String(matcher3.group("sellerAccount")));} catch (Exception e) {// 不處理}}Matcher matcher4 = patternPayee.matcher(allText);if (matcher4.find()) {// 如果查詢到的話就設(shè)置備注信息try {note.setPayee(new String(matcher4.group("payee")));} catch (Exception e) {// 不處理}}Matcher matcher5 = patternChecker.matcher(allText);if (matcher5.find()) {// 如果查詢到的話就設(shè)置備注信息try {note.setChecker(new String(matcher5.group("checker")));} catch (Exception e) {// 不處理}}}invoice.setDetail(note);return invoice;}/*** 替換字符串中的空格、全角空格、冒號和特殊空白字符為標(biāo)準(zhǔn)字符。** @param str 要進(jìn)行替換的字符串* @return 替換后的字符串*/private static String replace(String str) {return str.replaceAll(" ", "").replaceAll(" ", "").replaceAll(":", ":").replaceAll(" ", "");}}
2.4 三層架構(gòu)
controller
package com.example.pdf.controller;import com.example.pdf.entity.NewInvoice;
import com.example.pdf.service.InvoiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;@RestController
@RequestMapping("/invoice")
public class InvoiceController {@AutowiredInvoiceService invoiceService;/*** @param*/
// @PostMapping
// public void insert() {
// invoiceService.save();
// }@CrossOrigin(origins = "http://localhost:8081", allowedHeaders = "*", allowCredentials = "true")@PostMapping("/upload")public ResponseEntity<Object> uploadFile(@RequestParam("file") MultipartFile file) {try {// 調(diào)用你的文件解析服務(wù)NewInvoice parsedData = invoiceService.parsePdfFile(file);// 返回解析后的數(shù)據(jù)return ResponseEntity.ok(parsedData);} catch (Exception e) {return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error parsing file");}}}
service
service
package com.example.pdf.service;import com.example.pdf.entity.NewInvoice;
import org.springframework.web.multipart.MultipartFile;public interface InvoiceService {
// void save();NewInvoice parsePdfFile(MultipartFile file);
}
serviceImpl
package com.example.pdf.service.impl;import com.example.pdf.entity.NewInvoice;
import com.example.pdf.mapper.InvoiceMapper;
import com.example.pdf.service.InvoiceService;
import com.example.pdf.utils.PDF;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;@Service
public class InvoiceServiceImpl implements InvoiceService {@AutowiredInvoiceMapper invoiceMapper;
//
// @Override
// public void save(){
// //獲得發(fā)票對象
// InvoiceSubset invoiceSubset = extractInvoiceInformation("D:\\00-sqq\\idea\\test\\dzfp_24952000000116465179_深圳必維華法商品檢定有限公司東莞分公司_20240726105216.pdf");
// //調(diào)用mapper將發(fā)票對象存入到數(shù)據(jù)庫中
// invoiceMapper.save(invoiceSubset);
// }/*** 調(diào)用解析文件的方法,解析上傳的文件* @param file* @return*/@Overridepublic NewInvoice parsePdfFile(MultipartFile file) {NewInvoice newInvoice = PDF.parsePdfFile(file);return newInvoice;}
}
mapper
因?yàn)闆]有操作數(shù)據(jù)庫,所以沒有使用到mapper層