Made split pdf functions conform to the new design pattern.

This commit is contained in:
Saud Fatayerji
2023-11-17 15:52:44 +03:00
parent b4251b56fe
commit 4c8a85726d
5 changed files with 129 additions and 134 deletions

View File

@@ -0,0 +1,44 @@
import { PdfFile } from '../wrappers/PdfFile.js';
import { splitPagesByIndex } from "./common/splitPagesByIndex.js";
import { detectEmptyPages } from "./common/detectEmptyPages.js";
import { detectQRCodePages } from "./common/detectQRCodePages.js";
export type SplitOnParamsType = {
file: PdfFile;
type: "BAR_CODE"|"QR_CODE"|"BLANK_PAGE";
whiteThreashold?: number;
}
export async function splitPagesByPreset(params: SplitOnParamsType): Promise<PdfFile[]> {
const { file, type, whiteThreashold } = params;
console.log("File: ", file);
let splitAtPages: number[];
switch (type) {
case "BAR_CODE":
// TODO: Implement
throw new Error("This split-type has not been implemented yet");
case "QR_CODE":
splitAtPages = await detectQRCodePages(file);
break;
case "BLANK_PAGE":
if (!whiteThreashold)
throw new Error("White threshold not provided");
splitAtPages = await detectEmptyPages(file, whiteThreashold);
break;
default:
throw new Error("An invalid split-type was provided.");
}
console.debug("Split At Pages: ", splitAtPages);
const newFiles = await splitPagesByIndex(file, splitAtPages);
for (let i = 0; i < newFiles.length; i++) {
newFiles[i].filename += "_split-"+i;
}
return newFiles;
};