Made split pdf functions conform to the new design pattern.
This commit is contained in:
44
shared-operations/src/functions/splitPagesByPreset.ts
Normal file
44
shared-operations/src/functions/splitPagesByPreset.ts
Normal 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;
|
||||
};
|
||||
Reference in New Issue
Block a user