-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAutoInvoiceSplitterExtractionExample.java
More file actions
56 lines (46 loc) · 2.25 KB
/
AutoInvoiceSplitterExtractionExample.java
File metadata and controls
56 lines (46 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import com.mindee.input.LocalInputSource;
import com.mindee.extraction.ExtractedPDF;
import com.mindee.extraction.PDFExtractor;
import com.mindee.v1.MindeeClient;
import com.mindee.v1.parsing.common.AsyncPredictResponse;
import com.mindee.v1.product.invoice.InvoiceV4;
import com.mindee.v1.product.invoicesplitter.InvoiceSplitterV1;
import java.io.File;
import java.io.IOException;
import java.util.List;
public class AutoInvoiceSplitterExtractionExample {
private static final String API_KEY = "my-api-key";
private static final MindeeClient mindeeClient = new MindeeClient(API_KEY);
public static void main(String[] args) throws IOException, InterruptedException {
String filePath = "/path/to/the/file.ext";
invoiceSplitterAutoExtraction(filePath);
}
private static void invoiceSplitterAutoExtraction(String filePath) throws IOException, InterruptedException {
LocalInputSource inputSource = new LocalInputSource(new File(filePath));
if (inputSource.isPdf() && new PDFExtractor(inputSource).getPageCount() > 1) {
parseMultiPage(inputSource);
} else {
parseSinglePage(inputSource);
}
}
private static void parseSinglePage(LocalInputSource inputSource) throws IOException, InterruptedException {
AsyncPredictResponse<InvoiceV4> invoiceResult = mindeeClient.enqueueAndParse(InvoiceV4.class, inputSource);
System.out.println(invoiceResult.getDocumentObj().toString());
}
private static void parseMultiPage(LocalInputSource inputSource) throws IOException, InterruptedException {
PDFExtractor extractor = new PDFExtractor(inputSource);
AsyncPredictResponse<InvoiceSplitterV1> invoiceSplitterResponse =
mindeeClient.enqueueAndParse(InvoiceSplitterV1.class, inputSource);
List<ExtractedPDF> extractedPdfs = extractor.extractInvoices(
invoiceSplitterResponse.getDocumentObj().getInference().getPrediction().getInvoicePageGroups(),
false
);
for (ExtractedPDF extractedPdf : extractedPdfs) {
// Optional: Save the files locally
// extractedPdf.writeToFile("output/path");
AsyncPredictResponse<InvoiceV4> invoiceResult =
mindeeClient.enqueueAndParse(InvoiceV4.class, extractedPdf.asInputSource());
System.out.println(invoiceResult.getDocumentObj().toString());
}
}
}