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
75 changes: 51 additions & 24 deletions package-lock.json

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

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,15 @@
"typescript": "^5.6.3"
},
"dependencies": {
"@cantoo/pdf-lib": "^2.3.2",
"commander": "~9.4.1",
"file-type": "~16.5.4",
"form-data": "~3.0.1",
"node-poppler": "^7.2.2",
"pdf-lib": "^1.17.1",
"pdf.js-extract": "^0.2.1",
"sharp": "^0.33.5",
"tmp": "^0.2.3"
"tmp": "^0.2.3",
"tslib": "^2.8.1"
},
"keywords": [
"typescript",
Expand Down
2 changes: 1 addition & 1 deletion src/imageOperations/common/imageExtractor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PDFDocument, PDFPage } from "pdf-lib";
import { PDFDocument, PDFPage } from "@cantoo/pdf-lib";
import { getMinMaxX, getMinMaxY, Polygon } from "../../geometry";

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PDFDocument } from "pdf-lib";
import { PDFDocument } from "@cantoo/pdf-lib";
import { MindeeError, MindeeMimeTypeError } from "../../errors";
import { InvoiceSplitterV1 } from "../../product";
import { LocalInputSource } from "../../input";
Expand Down Expand Up @@ -35,7 +35,10 @@ 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 PDFDocument.load(inputFile.fileObject, {
ignoreEncryption: true,
password: ""
});
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
@@ -1,4 +1,4 @@
import { PDFDocument, PDFImage, PDFPage } from "pdf-lib";
import { PDFDocument, PDFImage, PDFPage } from "@cantoo/pdf-lib";
import { MindeeError, MindeeMimeTypeError } from "../../errors";
import { Polygon } from "../../geometry";
import { MultiReceiptsDetectorV1 } from "../../product";
Expand Down Expand Up @@ -37,7 +37,10 @@ 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 PDFDocument.load(inputFile.fileObject, {
ignoreEncryption: true,
password: ""
});
} else {
pdfDoc = await PDFDocument.create();
let image: PDFImage;
Expand Down
7 changes: 5 additions & 2 deletions src/pdf/pdfCompressor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import tmp from "tmp";
import { ExtractedPdfInfo, extractTextFromPdf, hasSourceText } from "./pdfUtils";
import * as fs from "node:fs";
import { Poppler } from "node-poppler";
import { PDFDocument, PDFFont, PDFPage, rgb, StandardFonts } from "pdf-lib";
import { PDFDocument, PDFFont, PDFPage, rgb, StandardFonts } from "@cantoo/pdf-lib";
import { compressImage } from "../imageOperations";

/**
Expand Down Expand Up @@ -128,7 +128,10 @@ async function compressPagesWithQuality(
disableSourceText: boolean,
extractedText: ExtractedPdfInfo | null
): Promise<Buffer[]> {
const pdfDoc = await PDFDocument.load(pdfData);
const pdfDoc = await PDFDocument.load(pdfData, {
ignoreEncryption: true,
password: ""
});
const compressedPages: Buffer[] = [];

for (let i = 0; i < extractedPdfInfo.pages.length; i++) {
Expand Down
4 changes: 3 additions & 1 deletion src/pdf/pdfOperation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { errorHandler } from "../errors/handler";
import { PDFDocument } from "pdf-lib";
import { PDFDocument } from "@cantoo/pdf-lib";
import { PageOptions, PageOptionsOperation } from "../input";
import { MindeeError } from "../errors";
import { logger } from "../logger";
Expand All @@ -21,6 +21,7 @@ export async function extractPages(
): Promise<SplitPdf> {
const currentPdf = await PDFDocument.load(file, {
ignoreEncryption: true,
password: ""
});

const newPdf = await PDFDocument.create();
Expand Down Expand Up @@ -86,6 +87,7 @@ export async function extractPages(
export async function countPages(file: Buffer): Promise<number> {
const currentPdf = await PDFDocument.load(file, {
ignoreEncryption: true,
password: ""
});
return currentPdf.getPageCount();
}
43 changes: 43 additions & 0 deletions tests/pdf/pdfTypes.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import * as mindee from "../../src";
import path from "path";
import { expect } from "chai";
import * as pdf from "../../src/pdf";
import { PageOptions } from "../../src/input";
import { PageOptionsOperation } from "../../src";
import * as fs from "node:fs";

describe("Test pdf lib", () => {
let client: mindee.Client;
beforeEach(async () => {
client = new mindee.Client();
});
it("should open a simple XFA form PDF.", async () => {
const inputDoc = client.docFromPath(path.join(__dirname, "../data/file_types/pdf/XfaForm.pdf"));

await inputDoc.init();
expect(await pdf.countPages(inputDoc.fileObject)).to.eq(1);
});

it("should open an encrypted XFA form PDF.", async () => {
const inputDoc = client.docFromPath(path.join(__dirname, "../data/file_types/pdf/XfaForm_15p_encrypted.pdf"));

await inputDoc.init();
expect(await pdf.countPages(inputDoc.fileObject)).to.eq(15);
});


it("should be able to perform page operations on an encrypted XFA form PDF.", async () => {
const inputDoc = client.docFromPath(path.join(__dirname, "../data/file_types/pdf/XfaForm_15p_encrypted.pdf"));

await inputDoc.init();

const pageOptions: PageOptions = {
pageIndexes: [0, 1],
operation: PageOptionsOperation.KeepOnly,
onMinPages: 1,
};
const splitPdf = await pdf.extractPages(inputDoc.fileObject, pageOptions);
expect(splitPdf.totalPagesRemoved).to.eq(13);
expect(await pdf.countPages(splitPdf.file)).to.eq(2);
});
});