Aleksei
Committed by David PHAM-VAN

Implement alternative location for PDF.js

... ... @@ -8,6 +8,7 @@
- Output image sized to cropBox instead of mediaBox (iOS) [garrettApproachableGeek]
- Replace Activity with Context for Service Compatibility (Android) [Heinrich]
- Deprecate support for `convertHtml`
- Implement alternative location for PDF.js [Aleksei]
## 5.11.1
... ...
... ... @@ -43,7 +43,7 @@ for documentation.
<true/>
```
5. If you want to manually set the PdfJs library version for the web, a small script
5. If you want to manually set the Pdf.js library version for the web, a small script
has to be added to your `web/index.html` file, just before `</head>`.
Otherwise it is loaded automatically:
... ... @@ -52,6 +52,20 @@ for documentation.
var dartPdfJsVersion = "3.2.146";
</script>
```
5.1. If you want to manually set the alternative location for loading Pdf.js library for the web, the following script has to be added to your `web/index.html` file, just before `</head>`.
```html
<script>
var dartPdfJsBaseUrl = "https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.2.146/";
</script>
```
It is possible to use local directory which will be resolved to the host where the web app is running.
```html
<script>
var dartPdfJsBaseUrl = "assets/js/pdf/3.2.146/";
</script>
```
6. For Windows and Linux, you can force the pdfium version and architecture
on your main `CMakeLists.txt` with:
... ...
... ... @@ -33,6 +33,9 @@ import 'src/printer.dart';
import 'src/printing_info.dart';
import 'src/raster.dart';
const _dartPdfJsVersion = 'dartPdfJsVersion';
const _dartPdfJsBaseUrl = 'dartPdfJsBaseUrl';
/// Print plugin targeting Flutter on the Web
class PrintingPlugin extends PrintingPlatform {
/// Registers this class as the default instance of [PrintingPlugin].
... ... @@ -51,14 +54,11 @@ class PrintingPlugin extends PrintingPlatform {
final _loading = Mutex();
bool get _hasPdfJsLib => js.context.callMethod('eval', <String>[
'typeof pdfjsLib !== "undefined" && pdfjsLib.GlobalWorkerOptions.workerSrc!="";'
'typeof pdfjsLib !== "undefined" && pdfjsLib.GlobalWorkerOptions.workerSrc != "";'
]);
String get _selectPdfJsVersion => js.context.hasProperty('dartPdfJsVersion')
? js.context['dartPdfJsVersion']
: _pdfJsVersion;
String get _pdfJsUrlBase => '$_pdfJsCdnPath@$_selectPdfJsVersion/';
/// The base URL for loading pdf.js library
late String _pdfJsUrlBase;
Future<void> _initPlugin() async {
await _loading.acquire();
... ... @@ -86,10 +86,21 @@ class PrintingPlugin extends PrintingPlatform {
}
js.context['module'] = 0;
// Check if the source of PDF.js library is overridden via
// [dartPdfJsBaseUrl] JavaScript variable.
if (js.context.hasProperty(_dartPdfJsBaseUrl)) {
_pdfJsUrlBase = js.context[_dartPdfJsBaseUrl] as String;
} else {
final pdfJsVersion = js.context.hasProperty(_dartPdfJsVersion)
? js.context[_dartPdfJsVersion]
: _pdfJsVersion;
_pdfJsUrlBase = '$_pdfJsCdnPath@$pdfJsVersion/build/';
}
final script = html.ScriptElement()
..type = 'text/javascript'
..async = true
..src = '$_pdfJsUrlBase/build/pdf.min.js';
..src = '${_pdfJsUrlBase}pdf.min.js';
assert(html.document.head != null);
html.document.head!.append(script);
await script.onLoad.first;
... ... @@ -100,7 +111,7 @@ class PrintingPlugin extends PrintingPlatform {
}
js.context['pdfjsLib']['GlobalWorkerOptions']['workerSrc'] =
'$_pdfJsUrlBase/build/pdf.worker.min.js';
'${_pdfJsUrlBase}pdf.worker.min.js';
// Restore module and exports
if (module != null) {
... ...