Showing
3 changed files
with
50 additions
and
33 deletions
| @@ -70,51 +70,63 @@ class PrintingPlugin extends PrintingPlatform { | @@ -70,51 +70,63 @@ class PrintingPlugin extends PrintingPlatform { | ||
| 70 | return false; | 70 | return false; |
| 71 | } | 71 | } |
| 72 | 72 | ||
| 73 | + final String userAgent = js.context['navigator']['userAgent']; | ||
| 73 | final isChrome = js.context['chrome'] != null; | 74 | final isChrome = js.context['chrome'] != null; |
| 74 | final isSafari = js.context['safari'] != null; | 75 | final isSafari = js.context['safari'] != null; |
| 75 | - // Maybe Firefox 75 will support iframe printing | 76 | + final isMobile = userAgent.contains('Mobile'); |
| 77 | + // Maybe Firefox will support iframe printing | ||
| 76 | // https://bugzilla.mozilla.org/show_bug.cgi?id=911444 | 78 | // https://bugzilla.mozilla.org/show_bug.cgi?id=911444 |
| 79 | + // final isFirefox = userAgent.contains('Firefox'); | ||
| 80 | + | ||
| 81 | + // Chrome and Safari on a desktop computer | ||
| 82 | + if ((isChrome || isSafari) && !isMobile) { | ||
| 83 | + final completer = Completer<bool>(); | ||
| 84 | + final pdfFile = html.Blob( | ||
| 85 | + <Uint8List>[Uint8List.fromList(result)], | ||
| 86 | + 'application/pdf', | ||
| 87 | + ); | ||
| 88 | + final pdfUrl = html.Url.createObjectUrl(pdfFile); | ||
| 89 | + final html.HtmlDocument doc = js.context['document']; | ||
| 90 | + | ||
| 91 | + final frame = doc.getElementById(_frameId) ?? doc.createElement('iframe'); | ||
| 92 | + frame.setAttribute( | ||
| 93 | + 'style', | ||
| 94 | + 'visibility: hidden; height: 0; width: 0; position: absolute;', | ||
| 95 | + // 'height: 400px; width: 600px; position: absolute; z-index: 1000', | ||
| 96 | + ); | ||
| 97 | + | ||
| 98 | + frame.setAttribute('id', _frameId); | ||
| 99 | + frame.setAttribute('src', pdfUrl); | ||
| 100 | + | ||
| 101 | + html.EventListener load; | ||
| 102 | + load = (html.Event event) { | ||
| 103 | + frame.removeEventListener('load', load); | ||
| 104 | + final js.JsObject win = | ||
| 105 | + js.JsObject.fromBrowserObject(frame)['contentWindow']; | ||
| 106 | + frame.focus(); | ||
| 107 | + win.callMethod('print'); | ||
| 108 | + completer.complete(true); | ||
| 109 | + }; | ||
| 77 | 110 | ||
| 78 | - if (!isChrome && !isSafari) { | ||
| 79 | - final pr = 'data:application/pdf;base64,${base64.encode(result)}'; | ||
| 80 | - final html.Window win = js.context['window']; | ||
| 81 | - win.open(pr, name); | 111 | + frame.addEventListener('load', load); |
| 82 | 112 | ||
| 83 | - return true; | 113 | + doc.body.append(frame); |
| 114 | + return completer.future; | ||
| 84 | } | 115 | } |
| 85 | 116 | ||
| 86 | - final completer = Completer<bool>(); | 117 | + // All the others |
| 87 | final pdfFile = html.Blob( | 118 | final pdfFile = html.Blob( |
| 88 | <Uint8List>[Uint8List.fromList(result)], | 119 | <Uint8List>[Uint8List.fromList(result)], |
| 89 | 'application/pdf', | 120 | 'application/pdf', |
| 90 | ); | 121 | ); |
| 91 | final pdfUrl = html.Url.createObjectUrl(pdfFile); | 122 | final pdfUrl = html.Url.createObjectUrl(pdfFile); |
| 92 | final html.HtmlDocument doc = js.context['document']; | 123 | final html.HtmlDocument doc = js.context['document']; |
| 93 | - | ||
| 94 | - final frame = doc.getElementById(_frameId) ?? doc.createElement('iframe'); | ||
| 95 | - frame.setAttribute( | ||
| 96 | - 'style', | ||
| 97 | - 'visibility: hidden; height: 0; width: 0; position: absolute;', | ||
| 98 | - // 'height: 400px; width: 600px; position: absolute; z-index: 1000', | ||
| 99 | - ); | ||
| 100 | - | ||
| 101 | - frame.setAttribute('id', _frameId); | ||
| 102 | - frame.setAttribute('src', pdfUrl); | ||
| 103 | - | ||
| 104 | - html.EventListener load; | ||
| 105 | - load = (html.Event event) { | ||
| 106 | - frame.removeEventListener('load', load); | ||
| 107 | - final js.JsObject win = | ||
| 108 | - js.JsObject.fromBrowserObject(frame)['contentWindow']; | ||
| 109 | - frame.focus(); | ||
| 110 | - win.callMethod('print'); | ||
| 111 | - completer.complete(true); | ||
| 112 | - }; | ||
| 113 | - | ||
| 114 | - frame.addEventListener('load', load); | ||
| 115 | - | ||
| 116 | - doc.body.append(frame); | ||
| 117 | - return completer.future; | 124 | + final html.AnchorElement link = doc.createElement('a'); |
| 125 | + link.href = pdfUrl; | ||
| 126 | + link.target = '_blank'; | ||
| 127 | + link.click(); | ||
| 128 | + link.remove(); | ||
| 129 | + return true; | ||
| 118 | } | 130 | } |
| 119 | 131 | ||
| 120 | @override | 132 | @override |
| @@ -133,6 +145,7 @@ class PrintingPlugin extends PrintingPlatform { | @@ -133,6 +145,7 @@ class PrintingPlugin extends PrintingPlatform { | ||
| 133 | link.href = pdfUrl; | 145 | link.href = pdfUrl; |
| 134 | link.download = filename; | 146 | link.download = filename; |
| 135 | link.click(); | 147 | link.click(); |
| 148 | + link.remove(); | ||
| 136 | return true; | 149 | return true; |
| 137 | } | 150 | } |
| 138 | 151 |
| @@ -4,7 +4,7 @@ description: Plugin that allows Flutter apps to generate and print documents to | @@ -4,7 +4,7 @@ description: Plugin that allows Flutter apps to generate and print documents to | ||
| 4 | homepage: https://github.com/DavBfr/dart_pdf/tree/master/printing | 4 | homepage: https://github.com/DavBfr/dart_pdf/tree/master/printing |
| 5 | repository: https://github.com/DavBfr/dart_pdf | 5 | repository: https://github.com/DavBfr/dart_pdf |
| 6 | issue_tracker: https://github.com/DavBfr/dart_pdf/issues | 6 | issue_tracker: https://github.com/DavBfr/dart_pdf/issues |
| 7 | -version: 3.7.1 | 7 | +version: 3.7.2 |
| 8 | 8 | ||
| 9 | environment: | 9 | environment: |
| 10 | sdk: ">=2.3.0 <3.0.0" | 10 | sdk: ">=2.3.0 <3.0.0" |
-
Please register or login to post a comment