Dart Language: printing reports

2019-02-20 11:44发布

问题:

I would like to know how to print reports on Dart.

Basically, this is the activity flow:

  1. User clicks "Report" button.
  2. New browser window is created.
  3. Related data gets placed in the new window.

Of course that if there's any API to do that, I would use it.

So far, I've tried to create a new window, add onMessage listener on it and call postMessage on the main class to send data. However, it didn't work. The message never gets to the other side (main > new browser window).

Main Dart Class

var reportWindow;
void createReportWindow() {
    reportWindow = window.open("report.html", "");
}
void sendMessage(String message) {
    reportWindow.postMessage(message, "*");
}

Report Dart Class (linked to report.html)

void startListening() {
    window.onMessage.listen((e) {
        print(e.data.toString());
    });
}

回答1:

Update

Start sending from the report window to window.opener.postMessage(...) and then respond to (message.source as WindowBase).postMessage(...). This is probably a bug with the return value of window.open (see http://dartbug.com/20216 for more details).

Original

The code in this unit test should show you how it can be done https://github.com/dart-lang/core-elements/blob/master/test/core_media_query_test.dart.

I open a window from a new URL that loads its content and a Dart script (can of course also be compiled to JavaScript) from the server. The opened window communicates with the main window by sendMessage. This works in both directions.

Basically the second window is a second Dart browser application.

This might not exactly be what you were looking for but your requirement could realized this way I guess.

At this line https://github.com/dart-lang/core-elements/blob/master/test/core_media_query_test.dart#L66 the commented out code would probably fit for your requirement. I created the workaround below because the code is supposed to resize the second window from code in the main window to test the <core-media-query> element (doesn't work either btw.). But I assume you just need to open the window and the commented out code should do exactly that just fine.