diff --git a/public/functions/shared/detectEmptyPages.js b/public/functions/shared/detectEmptyPages.js index 2ee792b7..41385fd6 100644 --- a/public/functions/shared/detectEmptyPages.js +++ b/public/functions/shared/detectEmptyPages.js @@ -29,7 +29,7 @@ export async function detectEmptyPages(snapshot, whiteThreashold, PDFJS, OpenCV) } async function areImagesBlank(page, threshold) { - const images = getImagesOnPage(page, PDFJS); + const images = await getImagesOnPage(page, PDFJS); for (const image of images) { if(!isImageBlank(image, threshold)) return false; @@ -37,7 +37,7 @@ export async function detectEmptyPages(snapshot, whiteThreashold, PDFJS, OpenCV) return true; } - async function isImageBlank(image, threshold) { + function isImageBlank(image, threshold) { const src = new OpenCV.cv.Mat(image.width, image.height, OpenCV.cv.CV_8UC4); src.data.set(image.data); // Convert the image to grayscale diff --git a/public/functions/splitOn.js b/public/functions/splitOn.js index 148fb106..9c3dbb23 100644 --- a/public/functions/splitOn.js +++ b/public/functions/splitOn.js @@ -1,5 +1,6 @@ import { detectEmptyPages } from "./shared/detectEmptyPages.js"; import { getImagesOnPage } from "./shared/getImagesOnPage.js"; +import { createSubDocument } from "./shared/createSubDocument.js"; /** * @typedef {"BAR_CODE"|"QR_CODE"|"BLANK_PAGE"} SplitType @@ -39,18 +40,42 @@ export async function splitOn(snapshot, type, whiteThreashold, PDFJS, OpenCV, PD console.log("Split At Pages: ", splitAtPages); + // Remove detected Pages & Split const pdfDoc = await PDFLib.PDFDocument.load(snapshot); - // TODO: Remove detected Pages & Split + const numberOfPages = pdfDoc.getPages().length; - return pdfDoc.save(); + let pagesArray = []; + let splitAfter = splitAtPages.shift(); + const subDocuments = []; + + for (let i = 0; i < numberOfPages; i++) { + console.log(i); + if(i == splitAfter) { + if(pagesArray.length > 0) { + subDocuments.push(await createSubDocument(pdfDoc, pagesArray, PDFLib)); + pagesArray = []; + } + splitAfter = splitAtPages.shift(); + } + else { // Skip splitAtPage + console.log("PagesArray") + pagesArray.push(i); + } + } + if(pagesArray.length > 0) { + subDocuments.push(await createSubDocument(pdfDoc, pagesArray, PDFLib)); + } + pagesArray = []; + + return subDocuments; async function getPagesWithQRCode(snapshot) { const pdfDoc = await PDFJS.getDocument(snapshot).promise; const pagesWithQR = []; - for (let i = 1; i <= pdfDoc.numPages; i++) { - const page = await pdfDoc.getPage(i); + for (let i = 0; i < pdfDoc.numPages; i++) { + const page = await pdfDoc.getPage(i + 1); console.log("Checking page " + i); const images = await getImagesOnPage(page, PDFJS); diff --git a/public/traverseOperations.js b/public/traverseOperations.js index 48bc0d07..a831c2f9 100644 --- a/public/traverseOperations.js +++ b/public/traverseOperations.js @@ -93,8 +93,7 @@ export async function * traverseOperations(operations, input, Functions) { buffer: splitResult[j] }) } - - input = splits; + return splits; }); break; case "editMetadata": @@ -117,8 +116,17 @@ export async function * traverseOperations(operations, input, Functions) { break; case "splitOn": yield* oneToN(input, operation, async (input) => { - input.fileName += "_split"; - input.buffer = await Functions.splitOn(input.buffer, operation.values["type"], operation.values["whiteThreashold"]); + const splitResult = await Functions.splitOn(input.buffer, operation.values["type"], operation.values["whiteThreashold"]); + const splits = []; + for (let j = 0; j < splitResult.length; j++) { + splits.push({ + originalFileName: input.originalFileName, + fileName: input.fileName + "_split" + j, + buffer: splitResult[j] + }) + } + + return splits; }); break; default: @@ -138,13 +146,14 @@ export async function * traverseOperations(operations, input, Functions) { async function * oneToN(input, operation, callback) { if(Array.isArray(input)) { + let output = []; for (let i = 0; i < input.length; i++) { - await callback(input[i]); + output = output.concat(await callback(input[i])); } - yield* nextOperation(operation.operations, input); + yield* nextOperation(operation.operations, output); } else { - await callback(input); + input = await callback(input); yield* nextOperation(operation.operations, input); } }