Added support for PDFJS to PdfFile. Migrated some operations to use this new wrapper

This commit is contained in:
Saud Fatayerji
2023-11-12 04:46:09 +03:00
parent 3fad22c4fe
commit 0d915fcc33
12 changed files with 369 additions and 223 deletions

View File

@@ -1,19 +0,0 @@
import { PDFDocument } from 'pdf-lib';
export async function createSubDocument(pdfDoc: PDFDocument, pagesToExtractArray: number[]): Promise<Uint8Array> {
const subDocument = await PDFDocument.create();
// Check that array max number is not larger pdf pages number
if(Math.max(...pagesToExtractArray) >= pdfDoc.getPageCount()) {
throw new Error(`The PDF document only has ${pdfDoc.getPageCount()} pages and you tried to extract page ${Math.max(...pagesToExtractArray)}`);
}
const copiedPages = await subDocument.copyPages(pdfDoc, pagesToExtractArray);
for (let i = 0; i < copiedPages.length; i++) {
subDocument.addPage(copiedPages[i]);
}
return subDocument.save();
}

View File

@@ -1,11 +1,12 @@
import { DocumentInitParameters, PDFPageProxy } from "pdfjs-dist/types/src/display/api.js";
import * as PDFJS from 'pdfjs-dist';
import { PdfFile } from '../../wrappers/PdfFile';
import { PDFPageProxy } from "pdfjs-dist/types/src/display/api.js";
import { Image } from 'image-js';
import { getImagesOnPage } from "./getImagesOnPage.js";
export async function detectEmptyPages(snapshot: string | URL | ArrayBuffer | DocumentInitParameters, whiteThreashold: number) {
const pdfDoc = await PDFJS.getDocument(snapshot).promise;
export async function detectEmptyPages(file: PdfFile, whiteThreashold: number): Promise<number[]> {
const pdfDoc = await file.getAsPdfJs();
const emptyPages: number[] = [];
for (let i = 1; i <= pdfDoc.numPages; i++) {

View File

@@ -0,0 +1,47 @@
import { PdfFile, convertAllToPdfLibFile } from '../../wrappers/PdfFile';
export async function sortPdfs(
files: PdfFile[],
sortType: "orderProvided"|"byFileName"|"byDateModified"|"byDateCreated"|"byPDFTitle" = "orderProvided"
): Promise<PdfFile[]> {
const pdfLibFiles = await convertAllToPdfLibFile(files);
switch(sortType) {
case "byFileName":
pdfLibFiles.sort((a, b) => {
if (!a || !b) return 0;
const ad = a.filename, bd = b.filename;
if (!ad || !bd) return 0;
return ad.localeCompare(bd);
});
break;
case "byDateModified":
pdfLibFiles.sort((a, b) => {
const ad = a.pdfLib?.getModificationDate()?.getTime();
const bd = b.pdfLib?.getModificationDate()?.getTime();
if (!ad || !bd) return 0;
return ad > bd ? 1 : -1
});
break;
case "byDateCreated":
pdfLibFiles.sort((a, b) => {
const ad = a.pdfLib?.getCreationDate()?.getTime();
const bd = b.pdfLib?.getCreationDate()?.getTime();
if (!ad || !bd) return 0;
return ad > bd ? 1 : -1
});
break;
case "byPDFTitle":
pdfLibFiles.sort((a, b) => {
const ad = a.pdfLib?.getTitle();
const bd = b.pdfLib?.getTitle();
if (!ad || !bd) return 0;
return ad.localeCompare(bd);
});
break;
}
return pdfLibFiles;
}