Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { MindeeError, MindeeMimeTypeError } from "../../errors";
import { InvoiceSplitterV1 } from "../../product";
import { LocalInputSource } from "../../input";
import { ExtractedInvoiceSplitterImage } from "./extractedInvoiceSplitterImage";
import { loadPdfWithFallback } from "../../pdf/pdfOperation";

async function splitPdf(pdfDoc: PDFDocument, invoicePageGroups: number[][]): Promise<ExtractedInvoiceSplitterImage[]> {
if (invoicePageGroups.length === 0) {
Expand Down Expand Up @@ -35,7 +36,7 @@ async function getPdfDoc(inputFile: LocalInputSource): Promise<PDFDocument> {
throw new MindeeMimeTypeError("Invoice Splitter is only compatible with pdf documents.");
}

const pdfDoc = await PDFDocument.load(inputFile.fileObject);
const pdfDoc = await loadPdfWithFallback(inputFile.fileObject);
if (pdfDoc.getPageCount() < 2) {
throw new MindeeError("Invoice Splitter is only compatible with multi-page-pdf documents.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ExtractedMultiReceiptImage } from "./extractedMultiReceiptImage";
import { LocalInputSource } from "../../input";
import { extractFromPage } from "../common";
import { PositionField } from "../../parsing/standard";
import { loadPdfWithFallback } from "../../pdf/pdfOperation";

/**
* Given a page and a set of coordinates, extracts & assigns individual receipts to an ExtractedMultiReceiptImage
Expand Down Expand Up @@ -37,7 +38,7 @@ async function loadPdfDoc(inputFile: LocalInputSource) {
'" Currently supported types are .png, .jpg and .pdf'
);
} else if (inputFile.isPdf()) {
pdfDoc = await PDFDocument.load(inputFile.fileObject);
pdfDoc = await loadPdfWithFallback(inputFile.fileObject);
} else {
pdfDoc = await PDFDocument.create();
let image: PDFImage;
Expand Down
3 changes: 2 additions & 1 deletion src/pdf/pdfCompressor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as fs from "node:fs";
import { Poppler } from "node-poppler";
import { PDFDocument, PDFFont, PDFPage, rgb, StandardFonts } from "pdf-lib";
import { compressImage } from "../imageOperations";
import { loadPdfWithFallback } from "./pdfOperation";

/**
* Compresses each page of a provided PDF buffer.
Expand Down Expand Up @@ -128,7 +129,7 @@ async function compressPagesWithQuality(
disableSourceText: boolean,
extractedText: ExtractedPdfInfo | null
): Promise<Buffer[]> {
const pdfDoc = await PDFDocument.load(pdfData);
const pdfDoc = await loadPdfWithFallback(pdfData);
const compressedPages: Buffer[] = [];

for (let i = 0; i < extractedPdfInfo.pages.length; i++) {
Expand Down
40 changes: 34 additions & 6 deletions src/pdf/pdfOperation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,44 @@ import { PDFDocument } from "pdf-lib";
import { PageOptions, PageOptionsOperation } from "../input";
import { MindeeError } from "../errors";
import { logger } from "../logger";
import { Poppler } from "node-poppler";
import { readFile } from "fs/promises";
import tmp from "tmp";
import fs from "node:fs";

export interface SplitPdf {
file: Buffer;
totalPagesRemoved: number;
}

/**
* Attempts to load the file using pdf-lib, and falls back to node-poppler if unable to.
* @param file File buffer to be opened.
*/
export async function loadPdfWithFallback(file: string | Buffer) {
const document = await PDFDocument.load(file, { ignoreEncryption: true });
if (!document.isEncrypted) {
return document;
}
const poppler = new Poppler();

const tmpPdfOutput = tmp.fileSync();
const tmpPdfOutputPath = tmpPdfOutput.name;

try {
await poppler.pdfToCairo(file, tmpPdfOutputPath, {
pdfFile: true,
antialias: "default",
});

const convertedPdf = await readFile(tmpPdfOutputPath);
return await PDFDocument.load(convertedPdf, { ignoreEncryption: true });
} finally {
await fs.promises.unlink(tmpPdfOutputPath);
}
}


/**
* Cut pages from a pdf file. If pages index are out of bound, it will throw an error.
* @param file
Expand All @@ -19,9 +51,7 @@ export async function extractPages(
file: Buffer,
pageOptions: PageOptions
): Promise<SplitPdf> {
const currentPdf = await PDFDocument.load(file, {
ignoreEncryption: true,
});
const currentPdf = await loadPdfWithFallback(file);

const newPdf = await PDFDocument.create();

Expand Down Expand Up @@ -84,8 +114,6 @@ export async function extractPages(
}

export async function countPages(file: Buffer): Promise<number> {
const currentPdf = await PDFDocument.load(file, {
ignoreEncryption: true,
});
const currentPdf = await loadPdfWithFallback(file);
return currentPdf.getPageCount();
}
2 changes: 1 addition & 1 deletion tests/data