2023-11-08 00:11:49 +01:00
import { PDFDocument } from 'pdf-lib' ;
2023-11-08 02:24:16 +01:00
import { detectEmptyPages } from "./common/detectEmptyPages.js" ;
2023-10-26 19:56:23 +02:00
2023-11-10 19:08:07 +01:00
export async function removeBlankPages ( snapshot : string | ArrayBuffer | Uint8Array , whiteThreashold : number ) {
2023-10-24 16:09:10 +02:00
2023-11-08 01:33:22 +01:00
const emptyPages = await detectEmptyPages ( snapshot , whiteThreashold ) ;
2023-10-24 16:09:10 +02:00
2023-10-24 19:31:14 +02:00
console . log ( "Empty Pages: " , emptyPages ) ;
2023-10-24 16:09:10 +02:00
2023-11-08 00:11:49 +01:00
const pdfDoc = await PDFDocument . load ( snapshot ) ;
2023-10-24 16:09:10 +02:00
2023-10-24 19:31:14 +02:00
// Reverse the array before looping in order to keep the indecies at the right pages. E.g. if you delete page 5 page 7 becomes page 6, if you delete page 7 page 5 remains page 5
emptyPages . reverse ( ) . forEach ( pageIndex = > {
pdfDoc . removePage ( pageIndex ) ;
} )
2023-10-24 16:09:10 +02:00
2023-10-24 19:31:14 +02:00
return pdfDoc . save ( ) ;
2023-10-24 16:09:10 +02:00
} ;