nbesli/pdf-merger-js

saveAsBlob not recognized

zaynekomichi opened this issue · 5 comments

React app using typescript,

merge.saveAsBlob() does not exist in PDFmerge
const mergeItems = async() => {
    const merger = new PDFMerger();
    for (const file of items) {
        await merger.add(file)
    }

    const mergedPdf = await merger.saveAsBlob();
    const url = URL.createObjectURL(mergedPdf);

    return setMergedPdfUrl(url);
}

pass type any to const merge

The function saveAsBlob is defined on the PDFMerger browser type. I don't know why this is a problem. Maybe somebody else that is using the browser version can shine some light on this?

Using it in tauri

@zaynekomichi Did you write the error messaeg yourself instead of copy pasting it? Because it is totally off.

You get a reference error on merge. Well, sure. The source code you provided only has a merger (with "r") variable.
Next the "PDFmerge" from your error message does not match the "PDFMerger" from the provided source code.

Nevertheless, you said you use tauri. I found this SO post potentially explaining the problem.

[...] The only NPM packages you can use are for the UI side of the Tauri application, and that also means they run inside the browser sandbox, and therefore they don't have native access to system resources beyond the web API. [...]

So I guess pdf-merger-js is missing access to the native web API's like Blob or File.
As you can se in the first lines of the script the globalObject only consideres native Web Environments:

const globalObject =
typeof globalThis === 'object'
? globalThis
: typeof window === 'object'
? window // Browser
: typeof self === 'object'
? self // Worker
: this

I pasted the message, will reproduce the error and attach an image