David PHAM-VAN

Fix Printing on WEB

1 # Changelog 1 # Changelog
2 2
  3 +## 3.7.2
  4 +
  5 +- Fix Printing on WEB
  6 +
3 ## 3.7.1 7 ## 3.7.1
4 8
5 - Fix Pdf Raster on WEB 9 - Fix Pdf Raster on WEB
@@ -70,19 +70,16 @@ class PrintingPlugin extends PrintingPlatform { @@ -70,19 +70,16 @@ 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');
77 80
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);  
82 -  
83 - return true;  
84 - }  
85 - 81 + // Chrome and Safari on a desktop computer
  82 + if ((isChrome || isSafari) && !isMobile) {
86 final completer = Completer<bool>(); 83 final completer = Completer<bool>();
87 final pdfFile = html.Blob( 84 final pdfFile = html.Blob(
88 <Uint8List>[Uint8List.fromList(result)], 85 <Uint8List>[Uint8List.fromList(result)],
@@ -117,6 +114,21 @@ class PrintingPlugin extends PrintingPlatform { @@ -117,6 +114,21 @@ class PrintingPlugin extends PrintingPlatform {
117 return completer.future; 114 return completer.future;
118 } 115 }
119 116
  117 + // All the others
  118 + final pdfFile = html.Blob(
  119 + <Uint8List>[Uint8List.fromList(result)],
  120 + 'application/pdf',
  121 + );
  122 + final pdfUrl = html.Url.createObjectUrl(pdfFile);
  123 + final html.HtmlDocument doc = js.context['document'];
  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;
  130 + }
  131 +
120 @override 132 @override
121 Future<bool> sharePdf( 133 Future<bool> sharePdf(
122 Uint8List bytes, 134 Uint8List bytes,
@@ -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"