Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Window.print() with popups pages not working #2130

Open
2 tasks done
A7mdlbanna opened this issue May 8, 2024 · 1 comment
Open
2 tasks done

Window.print() with popups pages not working #2130

A7mdlbanna opened this issue May 8, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@A7mdlbanna
Copy link

A7mdlbanna commented May 8, 2024

  • I have read the Getting Started section
  • 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 A7mdlbanna added the bug Something isn't working label May 8, 2024
Copy link

github-actions bot commented May 8, 2024

👋 @A7mdlbanna

NOTE: This comment is auto-generated.

Are you sure you have already searched for the same problem?

Some people open new issues but they didn't search for something similar or for the same issue. Please, search for it using the GitHub issue search box or on the official inappwebview.dev website, or, also, using Google, StackOverflow, etc. before posting a new one. You may already find an answer to your problem!

If this is really a new issue, then thank you for raising it. I will investigate it and get back to you as soon as possible. Please, make sure you have given me as much context as possible! Also, if you didn't already, post a code example that can replicate this issue.

In the meantime, you can already search for some possible solutions online! Because this plugin uses native WebView, you can search online for the same issue adding android WebView [MY ERROR HERE] or ios WKWebView [MY ERROR HERE] keywords.

Following these steps can save you, me, and other people a lot of time, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant