2023-11-17 00:32:09 +03:00
|
|
|
|
2024-01-04 20:17:54 -05:00
|
|
|
import { PdfFile } from "../wrappers/PdfFile.js";
|
|
|
|
import { detectEmptyPages } from "./common/detectEmptyPages.js";
|
|
|
|
import { getPages } from "./common/getPagesByIndex.js";
|
|
|
|
import { invertSelection } from "./common/pageIndexesUtils.js";
|
2023-11-17 00:32:09 +03:00
|
|
|
|
2024-01-04 20:17:54 -05:00
|
|
|
export interface RemoveBlankPagesParamsType {
|
2023-11-17 00:32:09 +03:00
|
|
|
file: PdfFile;
|
|
|
|
whiteThreashold: number;
|
|
|
|
}
|
|
|
|
export async function removeBlankPages(params: RemoveBlankPagesParamsType) {
|
|
|
|
const { file, whiteThreashold } = params;
|
2023-11-18 19:56:23 +03:00
|
|
|
const pdfDoc = await file.pdfLibDocument;
|
|
|
|
const pageCount = pdfDoc.getPageCount();
|
2023-11-17 00:32:09 +03:00
|
|
|
|
|
|
|
const emptyPages = await detectEmptyPages(file, whiteThreashold);
|
|
|
|
console.debug("Empty Pages: ", emptyPages);
|
2024-01-04 20:17:54 -05:00
|
|
|
const pagesToKeep = invertSelection(emptyPages, pageCount);
|
2023-11-17 00:45:37 +03:00
|
|
|
|
|
|
|
const newFile = await getPages(file, pagesToKeep);
|
2024-01-04 20:17:54 -05:00
|
|
|
newFile.filename += "_removedBlanks";
|
2023-11-17 00:45:37 +03:00
|
|
|
return newFile;
|
2023-11-17 00:32:09 +03:00
|
|
|
}
|