DavBfr / dart_pdf

Pdf creation module for dart/flutter
https://pub.dev/packages/pdf
Apache License 2.0
1.42k stars 632 forks source link

Printing fails on Web, with the error "TypeError: null: type 'Null' is not a subtype of type 'JSObject'" #1754

Closed nirmal-kc closed 1 month ago

nirmal-kc commented 1 month ago

Describe the bug Printing fails on Web, with the error "TypeError: null: type 'Null' is not a subtype of type 'JSObject'"

To Reproduce Download the latest code from GitHub repo. And run it on the latest version of Flutter

Expected behavior The print window must open.

Screenshots image Flutter Doctor

^[[A[✓] Flutter (Channel stable, 3.24.3, on macOS 15.0 24A335 darwin-arm64, locale en-IN)
[!] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
    ✗ cmdline-tools component is missing
      Run `path/to/sdkmanager --install "cmdline-tools;latest"`
      See https://developer.android.com/studio/command-line for more details.
    ✗ Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/to/macos-android-setup for more details.
⣷[✓] Xcode - develop for iOS and macOS (Xcode 16.0)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2024.1)
[✓] VS Code (version 1.93.1)
[✓] Connected device (4 available)
[✓] Network resources

Desktop (please complete the following information):

Sarkheelmughal commented 1 month ago

same issue with me

FadyShehataa commented 1 month ago

same issue with me. anyone get solution ?

blakix commented 1 month ago

same issue

nirmal-kc commented 1 month ago

The issue is thrown from printing_web.dart line no 196.

final isSafari = web.window['safari'].isUndefinedOrNull &&
        !userAgent.contains(RegExp(r'Version/14\.1\.'));

Replacing it with a simple try/catch block fixed the issue.

    bool isSafari;
    try {
      isSafari = web.window['safari'].isUndefinedOrNull &&
          !userAgent.contains(RegExp(r'Version/14\.1\.'));
    } catch (e) {
      isSafari = false;
    }

Can any of the contributors push this code to the repo? Thanks in advance.

blakix commented 1 month ago

Thanks nirmal-kc for your answer. It partially solved the problem for me. It only generates the PDF but the print window does not appear.

nirmal-kc commented 1 month ago

@blakix : My bad, I checked it in a hurry. Please replace the code for isChrome and isSafari as shown below. Let me know incase of any concerns.

    final userAgent = web.window.navigator.userAgent;
    //UserAgent can contain both Chrome and Safari for Chrome browser.
    //Ex:  Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.79 Safari/537.4
    final isChrome = userAgent.contains('Chrome');
    //UserAgent contains only Safari for Safari browser.
    //Ex: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/536.26.14 (KHTML, like Gecko) Version/6.0.1 Safari/536.26.14
    final isSafari =
        userAgent.contains('Safari') && !userAgent.contains('Chrome');
Sarkheelmughal commented 1 month ago

@nirmal-kc thanks now its working fine

blakix commented 1 month ago

Thanks nirmal-kc for all. It is perfect now. Thank you👌 find file here: C:\Users\user\AppData\Local\Pub\Cache\hosted\pub.dev\printing-5.13.3\lib\printing_web.dart

DavBfr commented 1 month ago

Pushed to master.

BrenoAugustoComandolli commented 4 weeks ago

Hi, I would like to know in which version this fix will come out.