thegrizzlylabs / geniusscan-sdk-demo

Demo apps for Genius Scan SDK
30 stars 38 forks source link

Generate pdf from Enhanced Url gives error #65

Closed komritza closed 1 year ago

komritza commented 1 year ago

Hello again, I am trying to generate a pdf from Enhanced Url but I am getting this in the console

E/MethodChannel#flutter_genius_scan(15904): Failed to handle method call
E/MethodChannel#flutter_genius_scan(15904): java.lang.NullPointerException
E/MethodChannel#flutter_genius_scan(15904):     at java.io.File.<init>(File.java:283)
E/MethodChannel#flutter_genius_scan(15904):     at com.geniusscansdk.scanflow.PluginBridge.deserializeDocumentGeneratorConfiguration(PluginBridge.java:239)
E/MethodChannel#flutter_genius_scan(15904):     at com.geniusscansdk.scanflow.PluginBridge.generateDocument(PluginBridge.java:217)
E/MethodChannel#flutter_genius_scan(15904):     at com.geniusscansdk.flutter.FlutterGeniusScanPlugin.onMethodCall(FlutterGeniusScanPlugin.java:86)
E/MethodChannel#flutter_genius_scan(15904):     at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:262)
E/MethodChannel#flutter_genius_scan(15904):     at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:295)
E/MethodChannel#flutter_genius_scan(15904):     at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:319)
E/MethodChannel#flutter_genius_scan(15904):     at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12)
E/MethodChannel#flutter_genius_scan(15904):     at android.os.Handler.handleCallback(Handler.java:942)
E/MethodChannel#flutter_genius_scan(15904):     at android.os.Handler.dispatchMessage(Handler.java:99)
E/MethodChannel#flutter_genius_scan(15904):     at android.os.Looper.loopOnce(Looper.java:226)
E/MethodChannel#flutter_genius_scan(15904):     at android.os.Looper.loop(Looper.java:313)
E/MethodChannel#flutter_genius_scan(15904):     at android.app.ActivityThread.main(ActivityThread.java:8741)
E/MethodChannel#flutter_genius_scan(15904):     at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#flutter_genius_scan(15904):     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
E/MethodChannel#flutter_genius_scan(15904):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067)
I/flutter (15904): PlatformException(error, null, null, java.lang.NullPointerException
I/flutter (15904):  at java.io.File.<init>(File.java:283)
I/flutter (15904):  at com.geniusscansdk.scanflow.PluginBridge.deserializeDocumentGeneratorConfiguration(PluginBridge.java:239)
I/flutter (15904):  at com.geniusscansdk.scanflow.PluginBridge.generateDocument(PluginBridge.java:217)
I/flutter (15904):  at com.geniusscansdk.flutter.FlutterGeniusScanPlugin.onMethodCall(FlutterGeniusScanPlugin.java:86)
I/flutter (15904):  at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:262)
I/flutter (15904):  at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:295)
I/flutter (15904):  at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:319)
I/flutter (15904):  at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12)
I/flutter (15904):  at android.os.Handler.handleCallback(Handler.java:942)
I/flutter (15904):  at android.os.Handler.dispatchMessage(Handler.java:99)
I/flutter (15904):  at android.os.Looper.loopOnce(Looper.java:226)
I/flutter (15904):  at android.os.Lo

This is the method that tries to generate a PDF

  @override
  Future<String> generatePdf(List<String> imageFilePaths) async {
    try {
      final tempDir = await getTemporaryDirectory();
      final filePath =
          File('${tempDir.path}/$prefixForPdfFileName-${DateTime.now()}.pdf')
              .path;

      final pages = <Map<String, String>>[];

      for (final p in imageFilePaths) {
        pages.add({'imageUrl': p});
      }

      final document = {'pages': pages};

      final documentGenerationConfiguration = {'outputFileUrl': filePath};

      await FlutterGeniusScan.generateDocument(
          document, documentGenerationConfiguration);

      return filePath;
    } on Exception catch (e) {
      // ignore: avoid_print
      print(e);
      return '';
    }
  }

imageFilePaths contains urls that are scanned with genius scan. It looks something like this: file:///storage/emulated/0/Android/data/{app_identifier}/files/0c262497-af25-49c7-beae-174f96a7a11e-enhanced.jpg}]

I also tried to replace file:// with an empty String but I get the same output.

bvirlet commented 1 year ago

It looks like outputFileUrl is invalid. What's in there?

komritza commented 1 year ago

Yes you were right (again :D) My outputFileUrl was invalid. Thanks a lot for your help!

bvirlet commented 1 year ago

We should probably throw a more descriptive error, though.

komritza commented 1 year ago

Yes it was pretty confusing to read the error log. Then when you said that the problem is with outputFileUrl i have checked how it was called and I had space in the name as well as : and . . So my file name was something like this fmx-app-2022-12-22 11:36:44.678153.pdf .

Maybe this will help you with what went wrong in my case.