Committed by
David PHAM-VAN
Implement alternative location for PDF.js
Showing
3 changed files
with
35 additions
and
9 deletions
| @@ -8,6 +8,7 @@ | @@ -8,6 +8,7 @@ | ||
| 8 | - Output image sized to cropBox instead of mediaBox (iOS) [garrettApproachableGeek] | 8 | - Output image sized to cropBox instead of mediaBox (iOS) [garrettApproachableGeek] |
| 9 | - Replace Activity with Context for Service Compatibility (Android) [Heinrich] | 9 | - Replace Activity with Context for Service Compatibility (Android) [Heinrich] |
| 10 | - Deprecate support for `convertHtml` | 10 | - Deprecate support for `convertHtml` |
| 11 | +- Implement alternative location for PDF.js [Aleksei] | ||
| 11 | 12 | ||
| 12 | ## 5.11.1 | 13 | ## 5.11.1 |
| 13 | 14 |
| @@ -43,7 +43,7 @@ for documentation. | @@ -43,7 +43,7 @@ for documentation. | ||
| 43 | <true/> | 43 | <true/> |
| 44 | ``` | 44 | ``` |
| 45 | 45 | ||
| 46 | -5. If you want to manually set the PdfJs library version for the web, a small script | 46 | +5. If you want to manually set the Pdf.js library version for the web, a small script |
| 47 | has to be added to your `web/index.html` file, just before `</head>`. | 47 | has to be added to your `web/index.html` file, just before `</head>`. |
| 48 | Otherwise it is loaded automatically: | 48 | Otherwise it is loaded automatically: |
| 49 | 49 | ||
| @@ -52,6 +52,20 @@ for documentation. | @@ -52,6 +52,20 @@ for documentation. | ||
| 52 | var dartPdfJsVersion = "3.2.146"; | 52 | var dartPdfJsVersion = "3.2.146"; |
| 53 | </script> | 53 | </script> |
| 54 | ``` | 54 | ``` |
| 55 | + 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>`. | ||
| 56 | + | ||
| 57 | + ```html | ||
| 58 | + <script> | ||
| 59 | + var dartPdfJsBaseUrl = "https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.2.146/"; | ||
| 60 | + </script> | ||
| 61 | + ``` | ||
| 62 | + It is possible to use local directory which will be resolved to the host where the web app is running. | ||
| 63 | + | ||
| 64 | + ```html | ||
| 65 | + <script> | ||
| 66 | + var dartPdfJsBaseUrl = "assets/js/pdf/3.2.146/"; | ||
| 67 | + </script> | ||
| 68 | + ``` | ||
| 55 | 69 | ||
| 56 | 6. For Windows and Linux, you can force the pdfium version and architecture | 70 | 6. For Windows and Linux, you can force the pdfium version and architecture |
| 57 | on your main `CMakeLists.txt` with: | 71 | on your main `CMakeLists.txt` with: |
| @@ -33,6 +33,9 @@ import 'src/printer.dart'; | @@ -33,6 +33,9 @@ import 'src/printer.dart'; | ||
| 33 | import 'src/printing_info.dart'; | 33 | import 'src/printing_info.dart'; |
| 34 | import 'src/raster.dart'; | 34 | import 'src/raster.dart'; |
| 35 | 35 | ||
| 36 | +const _dartPdfJsVersion = 'dartPdfJsVersion'; | ||
| 37 | +const _dartPdfJsBaseUrl = 'dartPdfJsBaseUrl'; | ||
| 38 | + | ||
| 36 | /// Print plugin targeting Flutter on the Web | 39 | /// Print plugin targeting Flutter on the Web |
| 37 | class PrintingPlugin extends PrintingPlatform { | 40 | class PrintingPlugin extends PrintingPlatform { |
| 38 | /// Registers this class as the default instance of [PrintingPlugin]. | 41 | /// Registers this class as the default instance of [PrintingPlugin]. |
| @@ -51,14 +54,11 @@ class PrintingPlugin extends PrintingPlatform { | @@ -51,14 +54,11 @@ class PrintingPlugin extends PrintingPlatform { | ||
| 51 | final _loading = Mutex(); | 54 | final _loading = Mutex(); |
| 52 | 55 | ||
| 53 | bool get _hasPdfJsLib => js.context.callMethod('eval', <String>[ | 56 | bool get _hasPdfJsLib => js.context.callMethod('eval', <String>[ |
| 54 | - 'typeof pdfjsLib !== "undefined" && pdfjsLib.GlobalWorkerOptions.workerSrc!="";' | 57 | + 'typeof pdfjsLib !== "undefined" && pdfjsLib.GlobalWorkerOptions.workerSrc != "";' |
| 55 | ]); | 58 | ]); |
| 56 | 59 | ||
| 57 | - String get _selectPdfJsVersion => js.context.hasProperty('dartPdfJsVersion') | ||
| 58 | - ? js.context['dartPdfJsVersion'] | ||
| 59 | - : _pdfJsVersion; | ||
| 60 | - | ||
| 61 | - String get _pdfJsUrlBase => '$_pdfJsCdnPath@$_selectPdfJsVersion/'; | 60 | + /// The base URL for loading pdf.js library |
| 61 | + late String _pdfJsUrlBase; | ||
| 62 | 62 | ||
| 63 | Future<void> _initPlugin() async { | 63 | Future<void> _initPlugin() async { |
| 64 | await _loading.acquire(); | 64 | await _loading.acquire(); |
| @@ -86,10 +86,21 @@ class PrintingPlugin extends PrintingPlatform { | @@ -86,10 +86,21 @@ class PrintingPlugin extends PrintingPlatform { | ||
| 86 | } | 86 | } |
| 87 | js.context['module'] = 0; | 87 | js.context['module'] = 0; |
| 88 | 88 | ||
| 89 | + // Check if the source of PDF.js library is overridden via | ||
| 90 | + // [dartPdfJsBaseUrl] JavaScript variable. | ||
| 91 | + if (js.context.hasProperty(_dartPdfJsBaseUrl)) { | ||
| 92 | + _pdfJsUrlBase = js.context[_dartPdfJsBaseUrl] as String; | ||
| 93 | + } else { | ||
| 94 | + final pdfJsVersion = js.context.hasProperty(_dartPdfJsVersion) | ||
| 95 | + ? js.context[_dartPdfJsVersion] | ||
| 96 | + : _pdfJsVersion; | ||
| 97 | + _pdfJsUrlBase = '$_pdfJsCdnPath@$pdfJsVersion/build/'; | ||
| 98 | + } | ||
| 99 | + | ||
| 89 | final script = html.ScriptElement() | 100 | final script = html.ScriptElement() |
| 90 | ..type = 'text/javascript' | 101 | ..type = 'text/javascript' |
| 91 | ..async = true | 102 | ..async = true |
| 92 | - ..src = '$_pdfJsUrlBase/build/pdf.min.js'; | 103 | + ..src = '${_pdfJsUrlBase}pdf.min.js'; |
| 93 | assert(html.document.head != null); | 104 | assert(html.document.head != null); |
| 94 | html.document.head!.append(script); | 105 | html.document.head!.append(script); |
| 95 | await script.onLoad.first; | 106 | await script.onLoad.first; |
| @@ -100,7 +111,7 @@ class PrintingPlugin extends PrintingPlatform { | @@ -100,7 +111,7 @@ class PrintingPlugin extends PrintingPlatform { | ||
| 100 | } | 111 | } |
| 101 | 112 | ||
| 102 | js.context['pdfjsLib']['GlobalWorkerOptions']['workerSrc'] = | 113 | js.context['pdfjsLib']['GlobalWorkerOptions']['workerSrc'] = |
| 103 | - '$_pdfJsUrlBase/build/pdf.worker.min.js'; | 114 | + '${_pdfJsUrlBase}pdf.worker.min.js'; |
| 104 | 115 | ||
| 105 | // Restore module and exports | 116 | // Restore module and exports |
| 106 | if (module != null) { | 117 | if (module != null) { |
-
Please register or login to post a comment