TF-2708 Allow Chrome to download PDF after view (#2712)

This commit is contained in:
Dat Dang
2024-03-15 10:44:45 +07:00
committed by GitHub
parent 9ac7ebd787
commit 65b587b9a0
5 changed files with 69 additions and 10 deletions
+43
View File
@@ -1,3 +1,5 @@
import 'js_interop_stub.dart' if (dart.library.html) 'dart:js_interop';
import 'dart:typed_data';
import 'package:core/data/constants/constant.dart';
import 'package:core/presentation/extensions/html_extension.dart';
@@ -148,4 +150,45 @@ class HtmlUtils {
html.Url.revokeObjectUrl(url);
}
static get iframeFullScreenCssStyle => '''
position: fixed;
border: 0;
width: 100%;
height: 100%;''';
static String pdfViewer(Uint8List bytes) {
return '''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
</head>
<style>
body {
margin: 0;
}
iframe {
${HtmlUtils.iframeFullScreenCssStyle}
}
</style>
<body></body>
<script>
const bytesJs = new Uint8Array(${bytes.toJS}).buffer;
const blob = new Blob([bytesJs], { type: "application/pdf" });
const url = URL.createObjectURL(blob);
window.onload = function() {
const iframe = document.createElement("iframe");
iframe.src = url;
document.body.appendChild(iframe);
};
window.addEventListener("beforeunload", function listener(event) {
URL.revokeObjectURL(url);
window.removeEventListener("beforeunload", listener);
});
</script>
</html>''';
}
}
+5
View File
@@ -0,0 +1,5 @@
import 'dart:typed_data';
extension Uint8ListExtension on Uint8List {
dynamic get toJS => throw UnimplementedError();
}