DavBfr / dart_pdf

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

Uncaught operation Platform._operatingSystem in web when deployed #1772

Open deibeeed opened 1 week ago

deibeeed commented 1 week ago

Hi. Help needed.

I am building a app that supports web. Pdf creation and printing works fine on local development but when I build the app for release, the UI gets stuck and from the console log, I receive this error:

Uncaught Error: Unsupported operation: Platform._operatingSystem
    at Object.c (main.dart.js:3626:19)
    at Object.c2g (main.dart.js:7279:15)
    at Object.c2h (main.dart.js:7313:16)
    at main.dart.js:172507:23
    at a.<computed> [as btx] (main.dart.js:24:36)
    at main.dart.js:172508:24
    at a.<computed> [as btw] (main.dart.js:24:36)
    at main.dart.js:152078:5
    at brx.a (main.dart.js:5007:63)
    at brx.$2 (main.dart.js:55828:14)

pdf and printing versions:

  pdf: ^3.11.1
  printing: ^5.13.4

Flutter command to build for web: fvm flutter build web -t lib/main_development.dart --release --dart-define=ENVIRONMENT=development --output build/web/development --web-renderer canvaskit

result from fvm flutter doctor -v

    • Flutter version 3.24.3 on channel stable at /home/deibeeed/fvm/versions/3.24.3@stable
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 2663184aa7 (8 weeks ago), 2024-09-11 16:27:48 -0500
    • Engine revision 36335019a8
    • Dart version 3.5.3
    • DevTools version 2.37.3

[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
    • Android SDK at /home/deibeeed/Android/Sdk
    • Platform android-35, build-tools 35.0.0
    • Java binary at: /home/deibeeed/.local/share/JetBrains/Toolbox/apps/android-studio/jbr/bin/java
    • Java version OpenJDK Runtime Environment (build 21.0.3+-12282718-b509.11)
    • All Android licenses accepted.

[✓] Chrome - develop for the web
    • Chrome at google-chrome

[✗] Linux toolchain - develop for Linux desktop
    ✗ clang++ is required for Linux development.
      It is likely available from your distribution (e.g.: apt install clang), or can be downloaded from https://releases.llvm.org/
    ✗ CMake is required for Linux development.
      It is likely available from your distribution (e.g.: apt install cmake), or can be downloaded from https://cmake.org/download/
    ✗ ninja is required for Linux development.
      It is likely available from your distribution (e.g.: apt install ninja-build), or can be downloaded from https://github.com/ninja-build/ninja/releases
    • pkg-config version 0.29.2
    ✗ GTK 3.0 development libraries are required for Linux development.
      They are likely available from your distribution (e.g.: apt install libgtk-3-dev)

[✓] Android Studio (version 2024.2)
    • Android Studio at /home/deibeeed/.local/share/JetBrains/Toolbox/apps/android-studio
    • Flutter plugin version 82.0.3
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 21.0.3+-12282718-b509.11)

[✓] IntelliJ IDEA Ultimate Edition (version 2024.1)
    • IntelliJ at /home/deibeeed/.local/share/JetBrains/Toolbox/apps/intellij-idea-ultimate-2
    • Flutter plugin version 81.0.2
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart

[✓] IntelliJ IDEA Ultimate Edition (version 2024.2)
    • IntelliJ at /home/deibeeed/.local/share/JetBrains/Toolbox/apps/intellij-idea-ultimate
    • Flutter plugin version 82.0.3
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart

[✓] Connected device (2 available)
    • Linux (desktop) • linux  • linux-x64      • Pop!_OS 22.04 LTS 6.6.10-76060610-generic
    • Chrome (web)    • chrome • web-javascript • Google Chrome 128.0.6613.84

[✓] Network resources
    • All expected network resources are available.

! Doctor found issues in 1 category.

Hoping that someone could show me the ropes.

Update

I was able to narrow down the issue to the printing library. I tried deploying to hosting a version that does not call Printing.layoutPdf() and the pdf was saved, no issues were thrown by the browser.

Again, I hope that someone can show me the ropes.. Thank you in advance!

deibeeed commented 5 days ago

Hi.

I resolved the issue by manually calling the web implementation of the printing library and checking if the application is running on web platform using kIsWeb from foundations.dart package.

The web implementation of printing.layoutPdf is found in package:printing/printing_web.dart.

import 'package:printing/printing.dart';
import 'package:printing/printing_web.dart';

.
.
.

if (kIsWeb) {
      return PrintingPlugin().layoutPdf(
        null,
        // [onLayout] will be called multiple times
        // when the user changes the printer or printer settings
        (PdfPageFormat format) {
          return _buildPdf(
            PdfPageFormat.a4,
            ...
          );
        },
        'print',
        PdfPageFormat.a4,
        true,
        true,
        OutputType.generic,
        false,
      );
    }

    return Printing.layoutPdf(
      // [onLayout] will be called multiple times
      // when the user changes the printer or printer settings
      onLayout: (PdfPageFormat format) {
        return _buildPdf(
          PdfPageFormat.a4,
          ...        );
      },
    );

The implementation is very hacky but it gets the job done. I was able to show print preview on web.

I'm not sure how to address or where the issue really is but I believe that there's an issue on how the printing for a certain platform is called, and I believe this is where the issue starts.

file: pubspec.yaml

flutter:
  plugin:
    platforms:
      android:
        package: net.nfet.flutter.printing
        pluginClass: PrintingPlugin
      ios:
        pluginClass: PrintingPlugin
      linux:
        pluginClass: PrintingPlugin
      macos:
        pluginClass: PrintingPlugin
      web:
        fileName: printing_web.dart
        pluginClass: PrintingPlugin
      windows:
        pluginClass: PrintingPlugin

Maintainer, contributors, If you think the issue doesn't make sense, please feel free to close the issue. I'll keep the issue open because I believe the issue is worth checking for.

Thank you for the great package! It really helped me in a lot of my projects that requires print capabilities.

cc: @DavBfr