flutter_inappwebview icon indicating copy to clipboard operation
flutter_inappwebview copied to clipboard

Window.print() with popups pages not working

Open A7mdlbanna opened this issue 9 months ago • 1 comments

  • [x] I have read the Getting Started section
  • [x] I have already searched for the same problem

Environment

Technology Version
Flutter version 3.19.0
Plugin version 6.0.0
Android version 10+
iOS version
macOS version
Xcode version
Google Chrome version

Device information:

Description

I'm using the package to display a webpage that contains a print service done by popping a blank page containing an HTML receipt and then using window.print() js function Expected behavior: the blank page pops displaying the content and then the function is executed and an OS print page appears with the content

Current behavior: a blank page is popped but with a white background and no functions are exected the log says that the popped page is about:blank#blocked and the console log gives me a document error (probably for executing window.print() on a blocked page)

Steps to reproduce

  1. run the following code with any website that has a print mechanism
Dart Code
void main() async{
WidgetsFlutterBinding.ensureInitialized();
runApp(const WebViewScreen());
}

class WebViewScreen extends StatefulWidget {
const WebViewScreen({Key? key}) : super(key: key);

@override
State<WebViewScreen> createState() => _WebViewScreenState();
}

class _WebViewScreenState extends State<WebViewScreen> with WidgetsBindingObserver {

InAppWebViewController? webViewController;
InAppWebViewSettings settings = InAppWebViewSettings(
    isInspectable: kDebugMode,
    mediaPlaybackRequiresUserGesture: false,
    allowsInlineMediaPlayback: true,
    iframeAllow: "camera; microphone",
    iframeAllowFullscreen: true);

PullToRefreshController? pullToRefreshController;
String url = "";
double progress = 0;
final urlController = TextEditingController();


@override
void initState() {
  pullToRefreshController = kIsWeb
      ? null
      : PullToRefreshController(
    settings: PullToRefreshSettings(
      color: Colors.blue,
    ),
    onRefresh: () async {
      if (defaultTargetPlatform == TargetPlatform.android) {
        webViewController?.reload();
      } else if (defaultTargetPlatform == TargetPlatform.iOS) {
        webViewController?.loadUrl(
            urlRequest:
            URLRequest(url: await webViewController?.getUrl()));
      }
    },
  );
  super.initState();
}

@override
Widget build(BuildContext context) {
  return Scaffold(
    body: SafeArea(
      child: InAppWebView(
        initialUrlRequest: URLRequest(url: WebUri("<any website containing printing>")),
        initialSettings: settings,
        pullToRefreshController: pullToRefreshController,
        onWebViewCreated: (controller) async {
          webViewController = controller;
          setState(() {});

          if (defaultTargetPlatform != TargetPlatform.android || await WebViewFeature.isFeatureSupported(WebViewFeature.WEB_MESSAGE_LISTENER)) {
            await controller.addWebMessageListener(WebMessageListener(
              jsObjectName: "login",
              onPostMessage: (message, sourceOrigin, isMainFrame, replyProxy) {
                print('message $message');
              },
            ));
          }
        },
        onLoadStart: (controller, url) {
          setState(() {
            this.url = url.toString();
            urlController.text = this.url;
          });
        },
        onPermissionRequest: (controller, request) async {
          return PermissionResponse(
              resources: request.resources,
              action: PermissionResponseAction.GRANT);
        },
        shouldOverrideUrlLoading: (controller, navigationAction) async {
          print(navigationAction.request.url);
          var uri = navigationAction.request.url!;
          if (uri.toString().contains('wa.me')) {
            if(await canLaunchUrl(uri)){
              launchUrl(uri, mode: LaunchMode.externalApplication);
            }
            return NavigationActionPolicy.CANCEL;
          }

          return NavigationActionPolicy.ALLOW;
        },
        onLoadStop: (controller, url) async {
          pullToRefreshController?.endRefreshing();
          setState(() {
            this.url = url.toString();
            urlController.text = this.url;
          });
        },
        onReceivedError: (controller, request, error) {
          pullToRefreshController?.endRefreshing();
        },
        onProgressChanged: (controller, progress) {
          if (progress == 100) {
            pullToRefreshController?.endRefreshing();
          }
          setState(() {
            this.progress = progress / 100;
            urlController.text = url;
          });
        },
        onUpdateVisitedHistory: (controller, url, androidIsReload) {
          setState(() {
            this.url = url.toString();
            urlController.text = this.url;
          });
        },
        onConsoleMessage: (controller, consoleMessage) {
          if (kDebugMode) {
            print('Console: $consoleMessage');
          }
        },
      ),
    ),
  );
}
}

  1. press the print button and wait for the result

Stacktrace/Logcat

LOG
[AndroidInAppWebViewController] (android) WebView ID 0 calling "onTitleChanged" using {title: a.glary.sa/ar/invoices}
[AndroidInAppWebViewController] (android) WebView ID 0 calling "onTitleChanged" using {title: فواتير المبيعات | الإبداع للإدارة السحابية}
I/AssistStructure( 5085): Flattened final assist data: 36300 bytes, containing 1 windows, 47 views
I/AssistStructure( 5085): Flattened final assist data: 39020 bytes, containing 1 windows, 47 views
I/AssistStructure( 5085): Flattened final assist data: 39372 bytes, containing 1 windows, 47 views
W/RenderInspector( 5085): QueueBuffer time out on loai.glary.sa/com.glary.gmc.MainActivity, count=1, avg=20 ms, max=20 ms.
I/flutter ( 5085): Console: ConsoleMessage{message: app loading .., messageLevel: LOG}
[AndroidInAppWebViewController] (android) WebView ID 0 calling "onConsoleMessage" using {messageLevel: 1, message: app loading ..}
I/flutter ( 5085): Console: ConsoleMessage{message: finish loading .., messageLevel: LOG}
I/flutter ( 5085): Console: ConsoleMessage{message: [object Object], messageLevel: LOG}
[AndroidInAppWebViewController] (android) WebView ID 0 calling "onConsoleMessage" using {messageLevel: 1, message: finish loading ..}
[AndroidInAppWebViewController] (android) WebView ID 0 calling "onConsoleMessage" using {messageLevel: 1, message: [object Object]}
[AndroidInAppWebViewController] (android) WebView ID 0 calling "shouldOverrideUrlLoading" using {request: {headers: null, method: GET, networkServiceType: null, allowsConstrainedNetworkAccess: null, cachePolicy: null, body: null, url: about:blank#blocked, allowsExpensiveNetworkAccess: null, attribution: null, assumesHTTP3Capable: null, httpShouldUsePipelining: null, allowsCellularAccess: null, httpShouldHandleCookies: null, timeoutInterval: null, mainDocumentURL: null}, sourceFrame: null, isRedirect: false, targetFrame: null, hasGesture: true, shouldPerformDownload: null, isForMainFrame: true, navigationType: null}
I/flutter ( 5085): about:blank#blocked
I/flutter ( 5085): Console: ConsoleMessage{message: jQuery.Deferred exception: Cannot read properties of null (reading 'document') TypeError: Cannot read properties of null (reading 'document')
I/flutter ( 5085):     at Object.print (https://a.glary.sa/js/app.js?3.412:2:1140219)
I/flutter ( 5085):     at HTMLDocument.<anonymous> (https://a.glary.sa/js/app.js?3.412:2:656366)
I/flutter ( 5085):     at l (https://a.glary.sa/js/app.js?3.412:2:3740830)
I/flutter ( 5085):     at u (https://a.glary.sa/js/app.js?3.412:2:3741132) undefined, messageLevel: WARNING}
[AndroidInAppWebViewController] (android) WebView ID 0 calling "onReceivedTouchIconUrl" using {precomposed: false, url: https://a.glary.sa/4545-04.ico}
[AndroidInAppWebViewController] (android) WebView ID 0 calling "onProgressChanged" using {progress: 10}
[AndroidInAppWebViewController] (android) WebView ID 0 calling "onProgressChanged" using {progress: 100}
[AndroidInAppWebViewController] (android) WebView ID 0 calling "onProgressChanged" using {progress: 100}
[AndroidInAppWebViewController] (android) WebView ID 0 calling "onTitleChanged" using {title: a.glary.sa/ar/invoices}
I/flutter ( 5085): Console: ConsoleMessage{message: jQuery.Deferred exception: Cannot read properties of null (reading 'document') TypeError: Cannot read properties of null (reading 'document')
I/flutter ( 5085):     at Object.print (https://a.glary.sa/js/app.js?3.412:2:1140219)
I/flutter ( 5085):     at HTMLDocument.<anonymous> (https://a.glary.sa/js/app.js?3.412:2:656366)
I/flutter ( 5085):     at l (https://a.glary.sa/js/app.js?3.412:2:3740830)
I/flutter ( 5085):     at u (https://a.glary.sa/js/app.js?3.412:2:3741132) undefined, messageLevel: WARNING}
I/flutter ( 5085): Console: ConsoleMessage{message: jQuery.Deferred exception: Cannot read properties of null (reading 'document') TypeError: Cannot read properties of null (reading 'document')
I/flutter ( 5085):     at Object.print (https://a.glary.sa/js/app.js?3.412:2:1140219)
I/flutter ( 5085):     at HTMLDocument.<anonymous> (https://a.glary.sa/js/app.js?3.412:2:656366)
I/flutter ( 5085):     at l (https://a.glary.sa/js/app.js?3.412:2:3740830)
I/flutter ( 5085):     at u (https://a.glary.sa/js/app.js?3.412:2:3741132) undefined, messageLevel: WARNING}
[AndroidInAppWebViewController] (android) WebView ID 0 calling "onConsoleMessage" using {messageLevel: 2, message: jQuery.Deferred exception: Cannot read properties of null (reading 'document') TypeError: Cannot read properties of null (reading 'document')
    at Object.print (https://a.glary.sa/js/app.js?3.412:2:1140219)
    at HTMLDocument.<anonymous> (https://a.glary.sa/js/app.js?3.412:2:656366)
    at l (https://a.glary.sa/js/app.js?3.412:2:3740830)
    at u (https://a.glary.sa/js/app.js?3.412:2:3741132) undefined}
[AndroidInAppWebViewController] (android) WebView ID 0 calling "onProgressChanged" using {progress: 10}
[AndroidInAppWebViewController] (android) WebView ID 0 calling "onConsoleMessage" using {messageLevel: 2, message: jQuery.Deferred exception: Cannot read properties of null (reading 'document') TypeError: Cannot read properties of null (reading 'document')
    at Object.print (https://a.glary.sa/js/app.js?3.412:2:1140219)
    at HTMLDocument.<anonymous> (https://a.glary.sa/js/app.js?3.412:2:656366)
    at l (https://a.glary.sa/js/app.js?3.412:2:3740830)
    at u (https://a.glary.sa/js/app.js?3.412:2:3741132) undefined}
[AndroidInAppWebViewController] (android) WebView ID 0 calling "onConsoleMessage" using {messageLevel: 2, message: jQuery.Deferred exception: Cannot read properties of null (reading 'document') TypeError: Cannot read properties of null (reading 'document')
    at Object.print (https://a.glary.sa/js/app.js?3.412:2:1140219)
    at HTMLDocument.<anonymous> (https://a.glary.sa/js/app.js?3.412:2:656366)
    at l (https://a.glary.sa/js/app.js?3.412:2:3740830)
    at u (https://a.glary.sa/js/app.js?3.412:2:3741132) undefined}
[AndroidInAppWebViewController] (android) WebView ID 0 calling "onLoadStart" using {url: about:blank#blocked}
[AndroidInAppWebViewController] (android) WebView ID 0 calling "onUpdateVisitedHistory" using {isReload: false, url: about:blank#blocked}
[AndroidInAppWebViewController] (android) WebView ID 0 calling "onProgressChanged" using {progress: 100}
[AndroidInAppWebViewController] (android) WebView ID 0 calling "onProgressChanged" using {progress: 100}
[AndroidInAppWebViewController] (android) WebView ID 0 calling "onTitleChanged" using {title: about:blank#blocked}
[AndroidInAppWebViewController] (android) WebView ID 0 calling "onLoadStop" using {url: about:blank#blocked}
[AndroidInAppWebViewController] (android) WebView ID 0 calling "onZoomScaleChanged" using {oldScale: 2.75, newScale: 1.1030611991882324}
[AndroidInAppWebViewController] (android) WebView ID 0 calling "onPageCommitVisible" using {url: about:blank#blocked}
W/RenderInspector( 5085): QueueBuffer time out on loai.glary.sa/com.glary.gmc.MainActivity, count=1, avg=16 ms, max=16 ms.
E/LongScreenshotUtils( 5085): inLargeScreen false
I/LongScreenshotUtils( 5085):  focus:true
I/LongScreenshotUtils( 5085): current ratio width:1.0, height:1.0

A7mdlbanna avatar May 08 '24 21:05 A7mdlbanna