pdfrx icon indicating copy to clipboard operation
pdfrx copied to clipboard

Make pdfrx support Flutter Web with wasm

Open thudaydelight opened this issue 2 years ago • 5 comments

I need to use Flutter Web with wasm for the application.

``

../../.pub-cache/hosted/pub.dev/pdfrx-1.0.50/lib/src/web/pdf.js.dart:7:1: Error: JS interop library 'dart:js_util' can't be imported when compiling to Wasm. Try using 'dart:js_interop' or 'dart:js_interop_unsafe' instead. import 'dart:js_util'; ^ ../../.pub-cache/hosted/pub.dev/pdfrx-1.0.50/lib/src/web/pdf.js.dart:122:14: Warning: Initializers for parameters are ignored on static interop external functions. Declare a forwarding non-external function with this initializer, or remove the initializer. double offsetX = 0, ^ ../../.pub-cache/hosted/pub.dev/pdfrx-1.0.50/lib/src/web/pdf.js.dart:123:14: Warning: Initializers for parameters are ignored on static interop external functions. Declare a forwarding non-external function with this initializer, or remove the initializer. double offsetY = 0, ^ ../../.pub-cache/hosted/pub.dev/pdfrx-1.0.50/lib/src/web/pdf.js.dart:124:12: Warning: Initializers for parameters are ignored on static interop external functions. Declare a forwarding non-external function with this initializer, or remove the initializer. bool dontFlip = false}); ^ ../../.pub-cache/hosted/pub.dev/pdfrx-1.0.50/lib/src/web/pdf.js.dart:168:14: Warning: Initializers for parameters are ignored on static interop external functions. Declare a forwarding non-external function with this initializer, or remove the initializer. String intent = 'display', ^ ../../.pub-cache/hosted/pub.dev/pdfrx-1.0.50/lib/src/web/pdf.js.dart:169:11: Warning: Initializers for parameters are ignored on static interop external functions. Declare a forwarding non-external function with this initializer, or remove the initializer. int annotationMode = 1, ^ ../../.pub-cache/hosted/pub.dev/pdfrx-1.0.50/lib/src/web/pdf.js.dart:170:12: Warning: Initializers for parameters are ignored on static interop external functions. Declare a forwarding non-external function with this initializer, or remove the initializer. bool renderInteractiveForms = false, ^ ../../.pub-cache/hosted/pub.dev/pdfrx-1.0.50/lib/src/web/pdf.js.dart:207:10: Warning: Initializers for parameters are ignored on static interop external functions. Declare a forwarding non-external function with this initializer, or remove the initializer. bool includeMarkedContent = false, ^ ../../.pub-cache/hosted/pub.dev/pdfrx-1.0.50/lib/src/web/pdf.js.dart:208:10: Warning: Initializers for parameters are ignored on static interop external functions. Declare a forwarding non-external function with this initializer, or remove the initializer. bool disableNormalization = false, ^ ../../.pub-cache/hosted/pub.dev/pdfrx-1.0.50/lib/src/web/pdf.js.dart:267:12: Warning: Initializers for parameters are ignored on static interop external functions. Declare a forwarding non-external function with this initializer, or remove the initializer. String intent = 'display', ^ ../../.pub-cache/hosted/pub.dev/pdfrx-1.0.50/lib/src/web/pdfrx_web.dart:6:1: Error: JS interop library 'dart:js_util' can't be imported when compiling to Wasm. Try using 'dart:js_interop' or 'dart:js_interop_unsafe' instead. import 'dart:js_util' as js_util; ^ Command: /Users/daydelight/fvm/versions/master/bin/cache/dart-sdk/bin/dart compile wasm --packages=.dart_tool/package_config.json --extra-compiler-option=--dart-sdk=/Users/daydelight/fvm/versions/master/bin/cache/dart-sdk --extra-compiler-option=--platform=/Users/daydelight/fvm/versions/master/bin/cache/flutter_web_sdk/kernel/dart2wasm_platform.dill --extra-compiler-option=--delete-tostring-package-uri=dart:ui --extra-compiler-option=--delete-tostring-package-uri=package:flutter --extra-compiler-option=--import-shared-memory --extra-compiler-option=--shared-memory-max-pages=32768 -Ddart.vm.product=true -DFLUTTER_WEB_CANVASKIT_URL=https://www.gstatic.com/flutter-canvaskit/4f6b832c8e333775f2c217a821050543a110826e/ -DFLUTTER_WEB_AUTO_DETECT=false -DFLUTTER_WEB_USE_SKIA=false -DFLUTTER_WEB_USE_SKWASM=true --extra-compiler-option=--depfile=/Users/daydelight/study/webassembly/.dart_tool/flutter_build/b49f125fa6955c85159dd2d865c4d882/dart2wasm.d -O4 --no-name-section -o /Users/daydelight/study/webassembly/.dart_tool/flutter_build/b49f125fa6955c85159dd2d865c4d882/main.dart.wasm /Users/daydelight/study/webassembly/.dart_tool/flutter_build/b49f125fa6955c85159dd2d865c4d882/main.dart #0 RunResult.throwException (package:flutter_tools/src/base/process.dart:125:5) #1 _DefaultProcessUtils.run (package:flutter_tools/src/base/process.dart:321:19) #2 Dart2WasmTarget.build (package:flutter_tools/src/build_system/targets/web.dart:297:5) #3 _BuildInstance._invokeInternal (package:flutter_tools/src/build_system/build_system.dart:875:9) #4 Future.wait. (dart:async/future.dart:524:21) #5 _BuildInstance.invokeTarget (package:flutter_tools/src/build_system/build_system.dart:813:32) #6 Future.wait. (dart:async/future.dart:524:21) #7 _BuildInstance.invokeTarget (package:flutter_tools/src/build_system/build_system.dart:813:32) #8 FlutterBuildSystem.build (package:flutter_tools/src/build_system/build_system.dart:642:16) #9 WebBuilder.buildWeb (package:flutter_tools/src/web/compile.dart:92:34) #10 BuildWebCommand.runCommand (package:flutter_tools/src/commands/build_web.dart:230:5) #11 FlutterCommand.run. (package:flutter_tools/src/runner/flutter_command.dart:1394:27) #12 AppContext.run. (package:flutter_tools/src/base/context.dart:153:19) #13 CommandRunner.runCommand (package:args/command_runner.dart:212:13) #14 FlutterCommandRunner.runCommand. (package:flutter_tools/src/runner/flutter_command_runner.dart:372:9) #15 AppContext.run. (package:flutter_tools/src/base/context.dart:153:19) #16 FlutterCommandRunner.runCommand (package:flutter_tools/src/runner/flutter_command_runner.dart:308:5) #17 run.. (package:flutter_tools/runner.dart:119:9) #18 AppContext.run. (package:flutter_tools/src/base/context.dart:153:19) #19 main (package:flutter_tools/executable.dart:93:3)

Compiling lib/main.dart for the Web... 17.2s Error: Failed to compile application for the Web.

``

thudaydelight avatar Apr 01 '24 02:04 thudaydelight

Basically, because wasm build is not supported on stable channel, I don't ensure that pdfrx runs on it. But I've just removed dependency on js_util.

espresso3389 avatar Apr 02 '24 08:04 espresso3389

@espresso3389

Thank you, i am waiting pdfrx support wasm when wasm will be supported on stable channel

thudaydelight avatar Apr 04 '24 02:04 thudaydelight

What's the likelihood of getting wasm support, now that it is supported on the stable channel?

davestimpert avatar Jun 05 '24 13:06 davestimpert

There are several pieces that can be building blocks of pdfrx wasm support:

I didn't check the things deeply, but wasm_ffi lacks struct support and it may be a major blocking issue for the support.

espresso3389 avatar Jun 07 '24 06:06 espresso3389

And, wasm_ffi 0.9.4 was released 16 months ago. The maintainer is not actively maintaining it. Do you guys know any kind of alternative to wasm_ffi or should we (I or someone) fork it to add support of struct??

espresso3389 avatar Jun 07 '24 06:06 espresso3389

I'm guessing it'll need to be forked. I'm asking around to see about options.

kevmoo avatar Nov 02 '24 23:11 kevmoo

Yes, dart2wasm doesn't support dart:ffi yet - we intend to look into how we can best support FFI on the web. But for the time being one would need to go via JS.

The package:wasm_ffi seems to do exactly that. Commits seem to be landing in that repository (https://github.com/vm75/wasm_ffi/commits/main/), so if that package has sufficient support for what package:pdfrx needs or any missing support can be added, then it may be a good option to use it and possibly send some PRs to that repository.

mkustermann avatar Nov 04 '24 08:11 mkustermann

And, wasm_ffi 0.9.4 was released 16 months ago. The maintainer is not actively maintaining it. Do you guys know any kind of alternative to wasm_ffi or should we (I or someone) fork it to add support of struct??

Are there still missing features after the recent bursts of commits to this repo? I'm not too familiar with ffi.

pdfrx is the final dependency blocking us from rolling out WASM and we see huge improvements from it, it would be great to get an update on this.

thomasostfeld avatar Nov 18 '24 11:11 thomasostfeld

@espresso3389 Any updates on this? Any way we can support?

edhom avatar Dec 02 '24 09:12 edhom

With Flutter 3.27 its impossible to compile to WASM

pdfrx-1.0.94/lib/src/pdfium/pdfrx_pdfium.dart:3:1: Error: 'dart:ffi' can't be imported when compiling to Wasm. import 'dart:ffi';

JgomesAT avatar Dec 12 '24 08:12 JgomesAT

We still have many things to support wasm.

espresso3389 avatar Dec 12 '24 09:12 espresso3389

but is strange with flutter 3.24 i have the possibility to compile with wasm, and now i have errors

JgomesAT avatar Dec 12 '24 09:12 JgomesAT

and do you have any schedule to do the library compatible with WASM?

JgomesAT avatar Dec 12 '24 09:12 JgomesAT

this library could help you?

https://pub.dev/packages/wasm_ffi

JgomesAT avatar Dec 12 '24 09:12 JgomesAT

At this moment, no ETA/schedule for the issue. And Of course, I'm trying to use wasm_ffi.

The current web implementation is based on pdf.js and WASM version will be based on pdfium WASM version to keep the cosistency with other platforms. And it will be a huge change anyway.

At least, I should check the following things:

  • Whether single pdfium binding can be used for both web and others
    • if we could not do that, how we work around the issue
  • Some way to asynchronously call rendering methods and others ([isolate_manager(https://pub.dev/packages/isolate_manager)?)
    • if it's impossible, can we go with synchronous calls?
  • ffi and wasm_ffi syntax compatibility or any other kind of abstraction layer

espresso3389 avatar Dec 12 '24 10:12 espresso3389

FYI: 1.0.95 (59dcef1) introduces changes that enables build with --wasm option though it does not involve any real WASM codes. It just adds several workarounds for complation with --wasm.

espresso3389 avatar Dec 12 '24 13:12 espresso3389

Thanks for this workaround

JgomesAT avatar Dec 12 '24 13:12 JgomesAT