David PHAM-VAN

Use proper print dialog on Firefox

# Changelog
## 5.4.2
- Use proper print dialog on Firefox
## 5.4.1
- Always use HTTPS to download Google Fonts
... ...
... ... @@ -37,8 +37,7 @@ for documentation.
4. For MacOS add printing capability by opening macos directory in XCode
5. For the web, a javascript library and a small script has to be added to
your `web/index.html` file, just before
`<script src="main.dart.js" type="application/javascript"></script>`:
your `web/index.html` file, just before `</head>`:
```html
<script src="//cdnjs.cloudflare.com/ajax/libs/pdf.js/2.8.335/pdf.min.js"></script>
... ...
... ... @@ -43,6 +43,8 @@ class PrintingPlugin extends PrintingPlatform {
PrintingPlatform.instance = PrintingPlugin();
}
static const String _scriptId = '__net_nfet_printing_s__';
static const String _frameId = '__net_nfet_printing__';
@override
... ... @@ -99,59 +101,76 @@ class PrintingPlugin extends PrintingPlatform {
final isChrome = js.context['chrome'] != null;
final isSafari = js.context['safari'] != null;
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');
final isFirefox = userAgent.contains('Firefox');
// Chrome and Safari on a desktop computer
if ((isChrome || isSafari) && !isMobile) {
// Chrome, Safari, and Firefox on a desktop computer
if ((isChrome || isSafari || isFirefox) && !isMobile) {
final completer = Completer<bool>();
final pdfFile = html.Blob(
<Uint8List>[Uint8List.fromList(result)],
<Uint8List>[result],
'application/pdf',
);
final pdfUrl = html.Url.createObjectUrl(pdfFile);
final html.HtmlDocument doc = js.context['document'];
final script =
doc.getElementById(_scriptId) ?? doc.createElement('script');
script.setAttribute('id', _scriptId);
script.setAttribute('type', 'text/javascript');
script.innerText =
'''function ${_frameId}_print(){var f=document.getElementById('$_frameId');f.focus();f.contentWindow.print();}''';
doc.body!.append(script);
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',
);
if (isFirefox) {
// Set the iframe to be is visible on the page (guaranteed by fixed position) but hidden using opacity 0, because
// this works in Firefox. The height needs to be sufficient for some part of the document other than the PDF
// viewer's toolbar to be visible in the page
frame.setAttribute('style',
'width: 1px; height: 100px; position: fixed; left: 0; top: 0; opacity: 0; border-width: 0; margin: 0; padding: 0');
} else {
// Hide the iframe in other browsers
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);
final stopWatch = Stopwatch();
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);
Timer(Duration(milliseconds: isSafari ? 500 : 0), () {
try {
stopWatch.start();
js.context.callMethod('${_frameId}_print');
stopWatch.stop();
completer.complete(true);
} catch (e) {
print(e);
completer.complete(_getPdf(result));
}
});
};
frame.addEventListener('load', load);
doc.body!.append(frame);
return completer.future;
final res = await completer.future;
// If print() is synchronous
if (stopWatch.elapsedMilliseconds > 1000) {
frame.remove();
script.remove();
}
return res;
}
// 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 link = html.AnchorElement(href: pdfUrl);
link.target = '_blank';
doc.body?.append(link);
link.click();
link.remove();
return true;
return _getPdf(result);
}
@override
... ... @@ -163,14 +182,22 @@ class PrintingPlugin extends PrintingPlatform {
String? body,
List<String>? emails,
) async {
return _getPdf(bytes, filename: filename);
}
Future<bool> _getPdf(Uint8List bytes, {String? filename}) async {
final pdfFile = html.Blob(
<Uint8List>[Uint8List.fromList(bytes)],
<Uint8List>[bytes],
'application/pdf',
);
final pdfUrl = html.Url.createObjectUrl(pdfFile);
final html.HtmlDocument doc = js.context['document'];
final link = html.AnchorElement(href: pdfUrl);
link.download = filename;
if (filename != null) {
link.download = filename;
} else {
link.target = '_blank';
}
doc.body?.append(link);
link.click();
link.remove();
... ...