2023-10-26 19:56:23 +02:00
import { detectEmptyPages } from "./shared/detectEmptyPages.js" ;
2023-10-24 19:03:43 +02:00
export async function removeBlankPages ( snapshot , whiteThreashold , PDFJS , OpenCV , PDFLib ) {
2023-10-24 16:09:10 +02:00
2023-10-26 19:56:23 +02:00
const emptyPages = await detectEmptyPages ( snapshot , whiteThreashold , PDFJS , OpenCV ) ;
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-10-24 19:31:14 +02:00
const pdfDoc = await PDFLib . 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
} ;