Skip to content

Commit 38c579a

Browse files
authored
🐛 fixes to extractors (#340)
1 parent 3ce956d commit 38c579a

6 files changed

Lines changed: 58 additions & 29 deletions

File tree

src/main/java/com/mindee/image/ExtractedImage.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.mindee.image;
22

3+
import com.mindee.MindeeException;
34
import com.mindee.input.LocalInputSource;
45
import java.awt.image.BufferedImage;
56
import java.io.ByteArrayOutputStream;
@@ -62,9 +63,13 @@ public void writeToFile(String outputPath) throws IOException {
6263
*/
6364
public void writeToFile(Path outputPath) throws IOException {
6465
if (!Files.isDirectory(outputPath)) {
65-
throw new IllegalArgumentException("Provided path is not a directory.");
66+
throw new MindeeException("Provided path is not a directory.");
67+
}
68+
try {
69+
ImageIO.write(this.image, this.saveFormat, outputPath.resolve(this.filename).toFile());
70+
} catch (IOException e) {
71+
throw new MindeeException("Could not save file " + this.filename + ".", e);
6672
}
67-
ImageIO.write(this.image, this.saveFormat, outputPath.resolve(this.filename).toFile());
6873
}
6974

7075
/**

src/main/java/com/mindee/pdf/BasePDFExtractor.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,19 @@ public BasePDFExtractor(LocalInputSource source) throws IOException {
5555
}
5656
}
5757

58-
public ExtractedPDF extractSinglePage(
59-
List<Integer> pageNumbers,
58+
public ExtractedPDF extractSingleDocument(
59+
List<Integer> pageIndexes,
6060
boolean closeOriginal
6161
) throws IOException {
62-
if (pageNumbers.isEmpty()) {
62+
if (pageIndexes.isEmpty()) {
6363
throw new MindeeException("Empty indexes not allowed for extraction.");
6464
}
65-
var pdfBytes = createPdfFromExistingPdf(this.sourcePdf, pageNumbers, closeOriginal);
66-
return new ExtractedPDF(pdfBytes, makeFilename(pageNumbers));
65+
var pdfBytes = createPdfFromExistingPdf(this.sourcePdf, pageIndexes, closeOriginal);
66+
return new ExtractedPDF(
67+
pdfBytes,
68+
makeFilename(pageIndexes),
69+
pageIndexes.stream().mapToInt(Integer::intValue).toArray()
70+
);
6771
}
6872

6973
/**
@@ -73,11 +77,13 @@ public ExtractedPDF extractSinglePage(
7377
* @return A list of extracted files.
7478
* @throws IOException Throws if the file can't be accessed.
7579
*/
76-
public ExtractedPDFs extractSubDocuments(List<List<Integer>> pageIndexes) throws IOException {
80+
public ExtractedPDFs extractMultipleDocuments(
81+
List<List<Integer>> pageIndexes
82+
) throws IOException {
7783
var extractedPDFs = new ExtractedPDFs();
7884

7985
for (List<Integer> pageIndexElement : pageIndexes) {
80-
extractedPDFs.add(extractSinglePage(pageIndexElement, false));
86+
extractedPDFs.add(extractSingleDocument(pageIndexElement, false));
8187
}
8288
return extractedPDFs;
8389
}
Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.mindee.pdf;
22

3+
import com.mindee.MindeeException;
34
import com.mindee.input.LocalInputSource;
45
import java.io.IOException;
56
import java.nio.file.Files;
@@ -12,51 +13,68 @@
1213
*/
1314
@Getter
1415
public class ExtractedPDF {
16+
/**
17+
* PDF content as bytes.
18+
*/
1519
private final byte[] fileBytes;
20+
/**
21+
* Name of the file when writing to disk.
22+
*/
1623
private final String filename;
24+
/**
25+
* 0-based indexes of all pages taken from the original PDF.
26+
*/
27+
private final int[] pageIndexes;
28+
/**
29+
* The number of pages in this PDF file.
30+
*/
31+
private final int pageCount;
1732

1833
/**
1934
* Default constructor.
2035
*
2136
* @param fileBytes PDF file as bytes.
2237
* @param filename Name of the extracted file.
38+
* @param pageIndexes Two-element array: index of the first and last extracted page.
2339
*/
24-
public ExtractedPDF(byte[] fileBytes, String filename) {
40+
public ExtractedPDF(byte[] fileBytes, String filename, int[] pageIndexes) {
2541
this.fileBytes = fileBytes;
2642
this.filename = filename;
43+
this.pageIndexes = pageIndexes;
44+
this.pageCount = pageIndexes.length;
2745
}
2846

2947
/**
3048
* Write the extracted PDF to a file.
3149
*
3250
* @param outputPath the output path, it may be a file or a directory.
33-
* @throws IOException Throws if the file can't be accessed.
3451
*/
35-
public void writeToFile(Path outputPath) throws IOException {
52+
public void writeToFile(Path outputPath) throws MindeeException {
3653
if (!Files.isDirectory(outputPath)) {
37-
throw new IllegalArgumentException("Provided path is not a directory.");
54+
throw new MindeeException("Provided path is not a directory.");
55+
}
56+
try {
57+
Files.write(outputPath.resolve(this.filename), this.fileBytes);
58+
} catch (IOException e) {
59+
throw new MindeeException("Could not save file " + this.filename + ".", e);
3860
}
39-
40-
Files.write(outputPath.resolve(this.filename), this.fileBytes);
4161
}
4262

4363
/**
4464
* Write the extracted PDF to a file.
4565
*
4666
* @param outputPath the output path, it may be a file or a directory.
47-
* @throws IOException Throws if the file can't be accessed.
4867
*/
49-
public void writeToFile(String outputPath) throws IOException {
68+
public void writeToFile(String outputPath) throws MindeeException {
5069
writeToFile(Paths.get(outputPath));
5170
}
5271

5372
/**
5473
* Return the file in a format suitable for sending to MindeeClient for parsing.
5574
*
5675
* @return an instance of {@link LocalInputSource}
57-
* @throws IOException Throws if the file can't be accessed.
5876
*/
59-
public LocalInputSource asInputSource() throws IOException {
77+
public LocalInputSource asInputSource() {
6078
return new LocalInputSource(this.fileBytes, this.filename);
6179
}
6280
}

src/main/java/com/mindee/v1/fileoperations/PDFExtractor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public List<ExtractedPDF> extractInvoices(
4040
.map(InvoiceSplitterV1InvoicePageGroup::getPageIndexes)
4141
.collect(Collectors.toList());
4242

43-
return extractSubDocuments(indexes);
43+
return extractMultipleDocuments(indexes);
4444
}
4545

4646
/**
@@ -81,7 +81,7 @@ public List<ExtractedPDF> extractInvoices(
8181
}
8282
previousConfidence = confidence;
8383
}
84-
return extractSubDocuments(correctPageIndexes);
84+
return extractMultipleDocuments(correctPageIndexes);
8585
}
8686

8787
}

src/main/java/com/mindee/v2/fileoperations/Split.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ public Split(LocalInputSource inputSource) throws IOException {
1717
}
1818

1919
public ExtractedPDF extractSingleSplit(SplitRange splitRange) throws IOException {
20-
return this.pdfSplitter.extractSinglePage(splitRange.getPageRangeDistinct(), true);
20+
return this.pdfSplitter.extractSingleDocument(splitRange.getPageRangeDistinct(), true);
2121
}
2222

2323
public ExtractedPDFs extractMultipleSplits(ArrayList<SplitRange> splitRanges) throws IOException {
2424
return this.pdfSplitter
25-
.extractSubDocuments(
25+
.extractMultipleDocuments(
2626
splitRanges.stream().map(SplitRange::getPageRangeDistinct).collect(Collectors.toList())
2727
);
2828
}

src/test/java/com/mindee/v2/fileoperations/SplitTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static com.mindee.TestingUtilities.deleteRecursively;
44
import static com.mindee.TestingUtilities.getResourcePath;
55
import static com.mindee.TestingUtilities.getV2ResourcePath;
6+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
67
import static org.junit.jupiter.api.Assertions.assertEquals;
78

89
import com.mindee.input.LocalInputSource;
@@ -34,8 +35,7 @@ void singlePage_splitsCorrectly() throws IOException {
3435
.extractSingleSplit(doc.getInference().getResult().getSplits().get(0));
3536

3637
assertEquals("default_sample_pages-001-001.pdf", extractedSplit.getFilename());
37-
var asInputSource = extractedSplit.asInputSource();
38-
assertEquals(1, asInputSource.getPageCount());
38+
assertEquals(1, extractedSplit.getPageCount());
3939

4040
extractedSplit.writeToFile(outputPath);
4141
}
@@ -54,13 +54,13 @@ void multiplePages_splitsCorrectly() throws IOException {
5454

5555
var split0 = extractedSplits.get(0);
5656
assertEquals("default_sample_pages-001-001.pdf", split0.getFilename());
57-
var asInputSource0 = split0.asInputSource();
58-
assertEquals(1, asInputSource0.getPageCount());
57+
assertEquals(1, split0.getPageCount());
58+
assertArrayEquals(new int[] { 0 }, split0.getPageIndexes());
5959

6060
var split1 = extractedSplits.get(1);
6161
assertEquals("default_sample_pages-002-002.pdf", split1.getFilename());
62-
var asInputSource1 = split1.asInputSource();
63-
assertEquals(1, asInputSource1.getPageCount());
62+
assertEquals(1, split0.getPageCount());
63+
assertArrayEquals(new int[] { 1 }, split1.getPageIndexes());
6464

6565
extractedSplits.saveAllToDisk(outputPath);
6666
}

0 commit comments

Comments
 (0)