Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 31 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"commander": "~9.4.1",
"file-type": "~16.5.4",
"form-data": "~3.0.1",
"node-poppler": "^7.2.2",
"node-poppler": "^9.0.1",
"pdf.js-extract": "^0.2.1",
"sharp": "^0.33.5",
"tmp": "^0.2.3",
Expand Down
60 changes: 58 additions & 2 deletions src/imageOperations/common/extractedImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { writeFileSync } from "node:fs";
import path from "node:path";
import { logger } from "../../logger";
import { BufferInput } from "../../input";
import { MIMETYPES } from "../../input/sources/localInputSource";
import { Poppler } from "node-poppler";
import { writeFile } from "fs/promises";

/**
* Generic class for image extraction
Expand All @@ -23,9 +26,35 @@ export class ExtractedImage {
*
* @param outputPath Path to save the file to.
*/
saveToFile(outputPath: string) {
async saveToFileAsync(outputPath: string) {
const fileExt = path.extname(outputPath).toLowerCase();
if (!MIMETYPES.has(fileExt)) {
throw new MindeeError(`Unsupported file extension: ${fileExt}`);
}

try {
writeFileSync(path.resolve(outputPath), this.buffer);
let outputBuffer: Buffer = this.buffer;
if (fileExt !== ".pdf") {
const poppler = new Poppler();
const options: Record<string, unknown> = {
firstPageToConvert: 1,
lastPageToConvert: 1,
singleFile: true,
};

if (fileExt === ".png") {
options.pngFile = true;
} else if (fileExt === ".jpg" || fileExt === ".jpeg") {
options.jpegFile = true;
} else if (fileExt === ".tiff" || fileExt === ".tif") {
options.tiffFile = true;
}

const result = await poppler.pdfToCairo(this.buffer, undefined, options);
outputBuffer = Buffer.from(result, "latin1");
}

await writeFile(path.resolve(outputPath), outputBuffer);
logger.info(`File saved successfully to ${path.resolve(outputPath)}.`);
} catch (e) {
if (e instanceof TypeError) {
Expand All @@ -37,6 +66,33 @@ export class ExtractedImage {
}


/**
* Attempts to saves the document to a file synchronously.
* Throws an error if the file extension is not supported or if the file could not be saved to disk for some reason.
*
* @param outputPath Path to save the file to.
*/
saveToFile(outputPath: string) {
const fileExt = path.extname(outputPath).toLowerCase();
if (fileExt !== ".pdf") {
throw new MindeeError(
`Unsupported file extension: ${fileExt}. For image formats, use saveToFileAsync() instead.`
);
} else {
try {
writeFileSync(path.resolve(outputPath), this.buffer);
logger.info(`File saved successfully to ${path.resolve(outputPath)}.`);
} catch (e) {
if (e instanceof TypeError) {
throw new MindeeError("Invalid path/filename provided.");
} else {
throw e;
}
}
}
}


/**
* Return the file as a Mindee-compatible BufferInput source.
*
Expand Down
2 changes: 1 addition & 1 deletion src/input/sources/localInputSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
INPUT_TYPE_PATH, INPUT_TYPE_BUFFER
} from "./inputSource";

const MIMETYPES = new Map<string, string>([
export const MIMETYPES = new Map<string, string>([
[".pdf", "application/pdf"],
[".heic", "image/heic"],
[".jpg", "image/jpeg"],
Expand Down
45 changes: 37 additions & 8 deletions tests/v1/api/multiReceiptsReconstruction.spec.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
import { expect } from "chai";
import { promises as fs } from "fs";
import * as path from "path";
import { Document } from "../../../src";
import { Document, PathInput } from "../../../src";
import { MultiReceiptsDetectorV1 } from "../../../src/product";
import { extractReceipts } from "../../../src/imageOperations";
import { PathInput } from "../../../src";
import { V1_PRODUCT_PATH } from "../../index";
import { RESOURCE_PATH, V1_PRODUCT_PATH } from "../../index";

describe("MindeeV1 - A Multi-Receipt Document", () => {
it("should be split into the proper receipts", async () => {
let extractedReceipts: any[];
let sourceDoc: PathInput;
before(async () => {

const jsonData = await fs.readFile(
path.join(V1_PRODUCT_PATH, "multi_receipts_detector/response_v1/complete.json")
);
const sourceDoc = new PathInput(
{ inputPath: path.join(V1_PRODUCT_PATH, "multi_receipts_detector/default_sample.jpg") }
);

sourceDoc = new PathInput({
inputPath: path.join(V1_PRODUCT_PATH, "multi_receipts_detector/default_sample.jpg"),
});
await sourceDoc.init();

const response = JSON.parse(jsonData.toString());
const doc = new Document(MultiReceiptsDetectorV1, response.document);
const extractedReceipts = await extractReceipts(sourceDoc, doc.inference);
extractedReceipts = await extractReceipts(sourceDoc, doc.inference);
});
it("should be split into the proper receipts", async () => {
expect(extractedReceipts.length).to.be.equals(6);
let i = 0;
for (const extractedReceipt of extractedReceipts) {
Expand All @@ -29,4 +35,27 @@ describe("MindeeV1 - A Multi-Receipt Document", () => {
i++;
}
});

it("should be saved locally", async () => {
let i = 0;
for (const extractedReceipt of extractedReceipts) {
extractedReceipt.saveToFile(path.join(RESOURCE_PATH, `output/extracted_receipt_${i}.pdf`));
await extractedReceipt.saveToFileAsync(path.join(RESOURCE_PATH, `output/extracted_receipt_${i}.png`));
await extractedReceipt.saveToFileAsync(path.join(RESOURCE_PATH, `output/extracted_receipt_${i}.jpg`));
const pdfStat = await fs.stat(path.join(RESOURCE_PATH, `output/extracted_receipt_${i}.pdf`));
expect(pdfStat.size).to.be.greaterThan(500000); // Arbitrary to assert noticeable discrepancies between OSs.
const jpgStat = await fs.stat(path.join(RESOURCE_PATH, `output/extracted_receipt_${i}.jpg`));
expect(jpgStat.size).to.be.greaterThan(40000);
const pngStat = await fs.stat(path.join(RESOURCE_PATH, `output/extracted_receipt_${i}.png`));
expect(pngStat.size).to.be.greaterThan(300000);
i++;
}
}).timeout(20000);
after(async () => {
for (let i = 0; i < extractedReceipts.length; i++) {
await fs.unlink(path.join(RESOURCE_PATH, `output/extracted_receipt_${i}.pdf`));
await fs.unlink(path.join(RESOURCE_PATH, `output/extracted_receipt_${i}.jpg`));
await fs.unlink(path.join(RESOURCE_PATH, `output/extracted_receipt_${i}.png`));
}
});
});