Make pdfrx support Flutter Web with wasm
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)
Compiling lib/main.dart for the Web... 17.2s Error: Failed to compile application for the Web.
``
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
Thank you, i am waiting pdfrx support wasm when wasm will be supported on stable channel
What's the likelihood of getting wasm support, now that it is supported on the stable channel?
There are several pieces that can be building blocks of pdfrx wasm support:
- Flutter's support for WebAssembly (Wasm)
- wasm_ffi
- bblanchon/pdfium-binaries has precompiled wasm module
I didn't check the things deeply, but wasm_ffi lacks struct support and it may be a major blocking issue for the support.
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??
I'm guessing it'll need to be forked. I'm asking around to see about options.
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.
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.
@espresso3389 Any updates on this? Any way we can support?
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';
We still have many things to support wasm.
but is strange with flutter 3.24 i have the possibility to compile with wasm, and now i have errors
and do you have any schedule to do the library compatible with WASM?
this library could help you?
https://pub.dev/packages/wasm_ffi
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
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.
Thanks for this workaround