Showing
1 changed file
with
18 additions
and
14 deletions
@@ -37,6 +37,8 @@ class PrintingPlugin extends PrintingPlatform { | @@ -37,6 +37,8 @@ class PrintingPlugin extends PrintingPlatform { | ||
37 | PrintingPlatform.instance = PrintingPlugin(); | 37 | PrintingPlatform.instance = PrintingPlugin(); |
38 | } | 38 | } |
39 | 39 | ||
40 | + static const String _frameId = '__net_nfet_printing__'; | ||
41 | + | ||
40 | @override | 42 | @override |
41 | Future<PrintingInfo> info() async { | 43 | Future<PrintingInfo> info() async { |
42 | return const PrintingInfo( | 44 | return const PrintingInfo( |
@@ -61,8 +63,11 @@ class PrintingPlugin extends PrintingPlatform { | @@ -61,8 +63,11 @@ class PrintingPlugin extends PrintingPlatform { | ||
61 | } | 63 | } |
62 | 64 | ||
63 | final bool isChrome = js.context['chrome'] != null; | 65 | final bool isChrome = js.context['chrome'] != null; |
66 | + final bool isSafari = js.context['safari'] != null; | ||
67 | + // Maybe Firefox 75 will support iframe printing | ||
68 | + // https://bugzilla.mozilla.org/show_bug.cgi?id=911444 | ||
64 | 69 | ||
65 | - if (!isChrome) { | 70 | + if (!isChrome && !isSafari) { |
66 | final String pr = 'data:application/pdf;base64,${base64.encode(result)}'; | 71 | final String pr = 'data:application/pdf;base64,${base64.encode(result)}'; |
67 | final html.Window win = js.context['window']; | 72 | final html.Window win = js.context['window']; |
68 | win.open(pr, name); | 73 | win.open(pr, name); |
@@ -77,33 +82,32 @@ class PrintingPlugin extends PrintingPlatform { | @@ -77,33 +82,32 @@ class PrintingPlugin extends PrintingPlatform { | ||
77 | ); | 82 | ); |
78 | final String pdfUrl = html.Url.createObjectUrl(pdfFile); | 83 | final String pdfUrl = html.Url.createObjectUrl(pdfFile); |
79 | final html.HtmlDocument doc = js.context['document']; | 84 | final html.HtmlDocument doc = js.context['document']; |
80 | - final html.IFrameElement frame = doc.createElement('iframe'); | 85 | + |
86 | + final html.IFrameElement frame = | ||
87 | + doc.getElementById(_frameId) ?? doc.createElement('iframe'); | ||
81 | frame.setAttribute( | 88 | frame.setAttribute( |
82 | 'style', | 89 | 'style', |
83 | 'visibility: hidden; height: 0; width: 0; position: absolute;', | 90 | 'visibility: hidden; height: 0; width: 0; position: absolute;', |
84 | // 'height: 400px; width: 600px; position: absolute; z-index: 1000', | 91 | // 'height: 400px; width: 600px; position: absolute; z-index: 1000', |
85 | ); | 92 | ); |
86 | 93 | ||
94 | + frame.setAttribute('sandbox', 'allow-scripts'); | ||
95 | + frame.setAttribute('id', _frameId); | ||
87 | frame.setAttribute('src', pdfUrl); | 96 | frame.setAttribute('src', pdfUrl); |
88 | 97 | ||
89 | - frame.addEventListener('load', (html.Event event) { | 98 | + html.EventListener load; |
99 | + load = (html.Event event) { | ||
100 | + frame.removeEventListener('load', load); | ||
90 | final js.JsObject win = | 101 | final js.JsObject win = |
91 | js.JsObject.fromBrowserObject(frame)['contentWindow']; | 102 | js.JsObject.fromBrowserObject(frame)['contentWindow']; |
92 | - | ||
93 | - win.callMethod('addEventListener', <dynamic>[ | ||
94 | - 'afterprint', | ||
95 | - js.allowInterop<html.EventListener>((html.Event event) { | ||
96 | - frame.remove(); | ||
97 | - completer.complete(true); | ||
98 | - }), | ||
99 | - ]); | ||
100 | - | ||
101 | frame.focus(); | 103 | frame.focus(); |
102 | win.callMethod('print'); | 104 | win.callMethod('print'); |
103 | - }); | 105 | + completer.complete(true); |
106 | + }; | ||
104 | 107 | ||
105 | - doc.body.append(frame); | 108 | + frame.addEventListener('load', load); |
106 | 109 | ||
110 | + doc.body.append(frame); | ||
107 | return completer.future; | 111 | return completer.future; |
108 | } | 112 | } |
109 | 113 |
-
Please register or login to post a comment