From 921a8c431c30e6d99af2cb3a64aedd8c144169b9 Mon Sep 17 00:00:00 2001 From: Felix Kaspar Date: Thu, 16 Nov 2023 01:56:17 +0100 Subject: [PATCH] More Types, Fixed isImageBlank --- server-node/declarations/PdfcpuWrapper.d.ts | 3 +++ .../src/functions/common/detectEmptyPages.ts | 9 +++++---- .../src/functions/common/getImagesOnPage.ts | 2 +- shared-operations/src/wasm/pdfcpu/pdfcpu-wrapper-node.js | 8 ++++---- 4 files changed, 13 insertions(+), 9 deletions(-) create mode 100644 server-node/declarations/PdfcpuWrapper.d.ts diff --git a/server-node/declarations/PdfcpuWrapper.d.ts b/server-node/declarations/PdfcpuWrapper.d.ts new file mode 100644 index 00000000..f0e2e814 --- /dev/null +++ b/server-node/declarations/PdfcpuWrapper.d.ts @@ -0,0 +1,3 @@ +declare module '@stirling-pdf/shared-operations/src/wasm/pdfcpu/pdfcpu-wrapper-node.js' { + export function oneToOne(wasmArray: string[], snapshot: Uint8Array): Promise; +} \ No newline at end of file diff --git a/shared-operations/src/functions/common/detectEmptyPages.ts b/shared-operations/src/functions/common/detectEmptyPages.ts index 297236ca..8ab6fdbc 100644 --- a/shared-operations/src/functions/common/detectEmptyPages.ts +++ b/shared-operations/src/functions/common/detectEmptyPages.ts @@ -1,9 +1,9 @@ import { PdfFile } from '../../wrappers/PdfFile'; import { PDFPageProxy } from "pdfjs-dist/types/src/display/api.js"; -import { Image } from 'image-js'; +import { Image, ImageKind } from 'image-js'; -import { getImagesOnPage } from "./getImagesOnPage.js"; +import { getImagesOnPage, PDFJSImage } from "./getImagesOnPage.js"; export async function detectEmptyPages(file: PdfFile, whiteThreashold: number): Promise { const pdfDoc = await file.pdfJsDocument; @@ -43,8 +43,9 @@ async function areImagesBlank(page: PDFPageProxy, threshold: number): Promise { - var img = await Image.load(image); +// TODO: Fix this function +async function isImageBlank(image: PDFJSImage, threshold: number): Promise { + var img = new Image(image.width, image.height, image.data, { kind: "RGB" as ImageKind }); // TODO: Maybe respect image.kind and convert accordingly, needs to be tested with a pdf with alpha-image var grey = img.grey(); var mean = grey.getMean(); return mean[0] <= threshold; diff --git a/shared-operations/src/functions/common/getImagesOnPage.ts b/shared-operations/src/functions/common/getImagesOnPage.ts index e036e317..01131261 100644 --- a/shared-operations/src/functions/common/getImagesOnPage.ts +++ b/shared-operations/src/functions/common/getImagesOnPage.ts @@ -8,7 +8,7 @@ export type PDFJSImage = { height: number; interpolate?: any; kind: number; - data: Uint8Array; + data: Uint8ClampedArray; }; export async function getImagesOnPage(page: PDFPageProxy): Promise { diff --git a/shared-operations/src/wasm/pdfcpu/pdfcpu-wrapper-node.js b/shared-operations/src/wasm/pdfcpu/pdfcpu-wrapper-node.js index 2fede912..60a9f4cd 100644 --- a/shared-operations/src/wasm/pdfcpu/pdfcpu-wrapper-node.js +++ b/shared-operations/src/wasm/pdfcpu/pdfcpu-wrapper-node.js @@ -74,12 +74,12 @@ export async function oneToOne(wasmArray, snapshot) { } console.log("Nuping Done"); + /* TODO: + * Make this more elegant, this waits for the write to finish. + * Maybe replace wasmfs with https://github.com/streamich/memfs + */ await checkExistsWithTimeout("/output.pdf", 1000); console.log("Write started..."); - - - // TODO: Make this more elegant, this waits for the write to finish. - // Maybe replace wasmfs with https://github.com/streamich/memfs let fileSize; while (true) { fileSize = fs.statSync("/output.pdf").size;