David PHAM-VAN

Fix Printing on WEB

# Changelog
## 3.7.2
- Fix Printing on WEB
## 3.7.1
- Fix Pdf Raster on WEB
... ...
... ... @@ -70,51 +70,63 @@ class PrintingPlugin extends PrintingPlatform {
return false;
}
final String userAgent = js.context['navigator']['userAgent'];
final isChrome = js.context['chrome'] != null;
final isSafari = js.context['safari'] != null;
// Maybe Firefox 75 will support iframe printing
final isMobile = userAgent.contains('Mobile');
// Maybe Firefox will support iframe printing
// https://bugzilla.mozilla.org/show_bug.cgi?id=911444
// final isFirefox = userAgent.contains('Firefox');
// Chrome and Safari on a desktop computer
if ((isChrome || isSafari) && !isMobile) {
final completer = Completer<bool>();
final pdfFile = html.Blob(
<Uint8List>[Uint8List.fromList(result)],
'application/pdf',
);
final pdfUrl = html.Url.createObjectUrl(pdfFile);
final html.HtmlDocument doc = js.context['document'];
final frame = doc.getElementById(_frameId) ?? doc.createElement('iframe');
frame.setAttribute(
'style',
'visibility: hidden; height: 0; width: 0; position: absolute;',
// 'height: 400px; width: 600px; position: absolute; z-index: 1000',
);
frame.setAttribute('id', _frameId);
frame.setAttribute('src', pdfUrl);
html.EventListener load;
load = (html.Event event) {
frame.removeEventListener('load', load);
final js.JsObject win =
js.JsObject.fromBrowserObject(frame)['contentWindow'];
frame.focus();
win.callMethod('print');
completer.complete(true);
};
if (!isChrome && !isSafari) {
final pr = 'data:application/pdf;base64,${base64.encode(result)}';
final html.Window win = js.context['window'];
win.open(pr, name);
frame.addEventListener('load', load);
return true;
doc.body.append(frame);
return completer.future;
}
final completer = Completer<bool>();
// All the others
final pdfFile = html.Blob(
<Uint8List>[Uint8List.fromList(result)],
'application/pdf',
);
final pdfUrl = html.Url.createObjectUrl(pdfFile);
final html.HtmlDocument doc = js.context['document'];
final frame = doc.getElementById(_frameId) ?? doc.createElement('iframe');
frame.setAttribute(
'style',
'visibility: hidden; height: 0; width: 0; position: absolute;',
// 'height: 400px; width: 600px; position: absolute; z-index: 1000',
);
frame.setAttribute('id', _frameId);
frame.setAttribute('src', pdfUrl);
html.EventListener load;
load = (html.Event event) {
frame.removeEventListener('load', load);
final js.JsObject win =
js.JsObject.fromBrowserObject(frame)['contentWindow'];
frame.focus();
win.callMethod('print');
completer.complete(true);
};
frame.addEventListener('load', load);
doc.body.append(frame);
return completer.future;
final html.AnchorElement link = doc.createElement('a');
link.href = pdfUrl;
link.target = '_blank';
link.click();
link.remove();
return true;
}
@override
... ... @@ -133,6 +145,7 @@ class PrintingPlugin extends PrintingPlatform {
link.href = pdfUrl;
link.download = filename;
link.click();
link.remove();
return true;
}
... ...
... ... @@ -4,7 +4,7 @@ description: Plugin that allows Flutter apps to generate and print documents to
homepage: https://github.com/DavBfr/dart_pdf/tree/master/printing
repository: https://github.com/DavBfr/dart_pdf
issue_tracker: https://github.com/DavBfr/dart_pdf/issues
version: 3.7.1
version: 3.7.2
environment:
sdk: ">=2.3.0 <3.0.0"
... ...