pdfium-lib icon indicating copy to clipboard operation
pdfium-lib copied to clipboard

Add an alternative wasm in release use the the `MODULARIZE` option when build?

Open Priestch opened this issue 4 years ago • 5 comments

I found it's not easy to custom the path of pdfium.wasm, although Module["locateFile"] make that possible, but Module must live in the global scope, it take some time to make it work when using common bundle tools. I suggest to add an alternative wasm in release use the the MODULARIZE option when build, it will be easier when integrate. Related doc.

Priestch avatar Nov 12 '21 08:11 Priestch

Hi,

I think that @cetinsert can help you better because he is expert in WASM.

Thanks.

paulocoutinhox avatar Dec 29 '21 06:12 paulocoutinhox

@Priestch – why do you need a custom path?

Is something hard-coded in the current build? I was able to load it just fine with relative URLs from any domain / directory.

Do you want to hide the name pdfium.wasm from showing up in the DevTools / Network tab o_O? If yes, why!?

CetinSert avatar Dec 29 '21 08:12 CetinSert

@cetinsert I want to custom onRuntimeInitialized and locateFile. Custom locateFile to hide details between different projects, but until now I realize that seems impossible, and custom onRuntimeInitialized to make sure the wasm file is ready when try to process pdf document.

Priestch avatar Dec 30 '21 03:12 Priestch

Hi @Priestch,

You can see here how i do the same thing to know when it is loaded: https://github.com/paulo-coutinho/pdfium-lib/blob/master/extras/wasm/template/index.html#L84-L92

paulocoutinhox avatar Dec 30 '21 04:12 paulocoutinhox

@paulo-coutinho It depends on the global Module variable.

it take some time to make it work when using common bundle tools.

when use the MODULARIZE, it will use a function to create the Module object, and can be customed like this.

const Module = createModule({onRuntimeInitialized() {...}})

Priestch avatar Dec 30 '21 04:12 Priestch

I think MODULARIZE should be the default option, As @Priestch specified, it can prevent the global variable pollution, it will be problematic if one page needs to use multiple WASM library, like if you build a web page that can open different kinds of files, like pdf, zip and so on, it's highly possible that you need to load a lot of wasm library. Without relying on the global variable Module would be much helpful. Also it would be useful for lazy loading WASM files. Like

import { createPdifumModule } from 'pdfium.js'
import { createEpubModule } from 'epub.js',

function whenUserSelectFile() {
   if (isEpub) {
   // It will be more helpful if user want to support caching wasm file, for example, they can save 
  // wasmBinary into indexeddb, next time they can use it as fallback if network request is down
   // then they can do
   // const wasmBinary = await loadPdifumWasmBinary();
   // const module = await createPdfiumModule({wasmBinaray});
    const module = await createPdifumModule();
   } else if (isPdf) {
    const module = await createPdifumModule();
   }
}

input.addEventListener('input', whenUserSelectFile);

jichang avatar Oct 20 '22 23:10 jichang

Hi,

I will refactor all and remake a lot of things in this project. And when i start it, i will change this too.

Im only finishing another open source project that is a requirement for me.

Stay tuned here and feel free to donate to help project development.

Thanks.

paulocoutinhox avatar Oct 21 '22 01:10 paulocoutinhox

Hi,

All problems was fixed in new release.

You can check here: https://github.com/paulocoutinhox/pdfium-lib/releases/tag/5407

The demo is here: https://pdfviewer.github.io/

I will see this problem to change to module.

Feel free to donate to help project development.

Thanks.

paulocoutinhox avatar Nov 08 '22 18:11 paulocoutinhox

This was done in latest PR.

After it built with success, i will merge.

paulocoutinhox avatar Nov 08 '22 22:11 paulocoutinhox

Hi,

Everything was done on latest release.

Feel free to donate to help project development.

Thanks.

paulocoutinhox avatar Nov 08 '22 23:11 paulocoutinhox