Committed by
David PHAM-VAN
Add cMapUrl and cMapPacked property
Showing
4 changed files
with
42 additions
and
19 deletions
| @@ -6,6 +6,7 @@ | @@ -6,6 +6,7 @@ | ||
| 6 | - Add custom pages builder to PdfPreview widget [Milad akarie] | 6 | - Add custom pages builder to PdfPreview widget [Milad akarie] |
| 7 | - Update Image dependency | 7 | - Update Image dependency |
| 8 | - Fix canChangeOrientation option not appearing bug [Bilal Raja] | 8 | - Fix canChangeOrientation option not appearing bug [Bilal Raja] |
| 9 | +- Add Support cmaps option on printing web [Koji Wakamiya] | ||
| 9 | 10 | ||
| 10 | ## 5.9.3 | 11 | ## 5.9.3 |
| 11 | 12 |
| @@ -43,14 +43,13 @@ for documentation. | @@ -43,14 +43,13 @@ 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 javascript | ||
| 47 | - library and a small script has to be added to your `web/index.html` file, just | ||
| 48 | - before `</head>`. Otherwise it is loaded automatically: | 46 | +5. If you want to manually set the PdfJs library version for the web, a small script |
| 47 | + has to be added to your `web/index.html` file, just before `</head>`. | ||
| 48 | + Otherwise it is loaded automatically: | ||
| 49 | 49 | ||
| 50 | ```html | 50 | ```html |
| 51 | - <script src="//cdnjs.cloudflare.com/ajax/libs/pdf.js/2.8.335/pdf.min.js"></script> | ||
| 52 | - <script type="text/javascript"> | ||
| 53 | - pdfjsLib.GlobalWorkerOptions.workerSrc = "//cdnjs.cloudflare.com/ajax/libs/pdf.js/2.8.335/pdf.worker.min.js"; | 51 | + <script> |
| 52 | + var dartPdfJsVersion = "3.2.146"; | ||
| 54 | </script> | 53 | </script> |
| 55 | ``` | 54 | ``` |
| 56 | 55 |
| @@ -45,8 +45,9 @@ class PrintingPlugin extends PrintingPlatform { | @@ -45,8 +45,9 @@ class PrintingPlugin extends PrintingPlatform { | ||
| 45 | 45 | ||
| 46 | static const String _frameId = '__net_nfet_printing__'; | 46 | static const String _frameId = '__net_nfet_printing__'; |
| 47 | 47 | ||
| 48 | - static const _pdfJsVersion = | ||
| 49 | - 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.13.216'; | 48 | + static const _pdfJsCdnPath = 'https://unpkg.com/pdfjs-dist'; |
| 49 | + | ||
| 50 | + static const _pdfJsVersion = '3.2.146'; | ||
| 50 | 51 | ||
| 51 | final _loading = Mutex(); | 52 | final _loading = Mutex(); |
| 52 | 53 | ||
| @@ -54,6 +55,12 @@ class PrintingPlugin extends PrintingPlatform { | @@ -54,6 +55,12 @@ class PrintingPlugin extends PrintingPlatform { | ||
| 54 | 'typeof pdfjsLib !== "undefined" && pdfjsLib.GlobalWorkerOptions.workerSrc!="";' | 55 | 'typeof pdfjsLib !== "undefined" && pdfjsLib.GlobalWorkerOptions.workerSrc!="";' |
| 55 | ]); | 56 | ]); |
| 56 | 57 | ||
| 58 | + String get _selectPdfJsVersion => js.context.hasProperty('dartPdfJsVersion') | ||
| 59 | + ? js.context['dartPdfJsVersion'] | ||
| 60 | + : _pdfJsVersion; | ||
| 61 | + | ||
| 62 | + String get _pdfJsUrlBase => '$_pdfJsCdnPath@$_selectPdfJsVersion/'; | ||
| 63 | + | ||
| 57 | Future<void> _initPlugin() async { | 64 | Future<void> _initPlugin() async { |
| 58 | await _loading.acquire(); | 65 | await _loading.acquire(); |
| 59 | 66 | ||
| @@ -61,7 +68,7 @@ class PrintingPlugin extends PrintingPlatform { | @@ -61,7 +68,7 @@ class PrintingPlugin extends PrintingPlatform { | ||
| 61 | final script = ScriptElement() | 68 | final script = ScriptElement() |
| 62 | ..type = 'text/javascript' | 69 | ..type = 'text/javascript' |
| 63 | ..async = true | 70 | ..async = true |
| 64 | - ..src = '$_pdfJsVersion/pdf.min.js'; | 71 | + ..src = '$_pdfJsUrlBase/build/pdf.min.js'; |
| 65 | assert(document.head != null); | 72 | assert(document.head != null); |
| 66 | document.head!.append(script); | 73 | document.head!.append(script); |
| 67 | await script.onLoad.first; | 74 | await script.onLoad.first; |
| @@ -72,8 +79,9 @@ class PrintingPlugin extends PrintingPlatform { | @@ -72,8 +79,9 @@ class PrintingPlugin extends PrintingPlatform { | ||
| 72 | require.callMethod('config', <dynamic>[ | 79 | require.callMethod('config', <dynamic>[ |
| 73 | js.JsObject.jsify({ | 80 | js.JsObject.jsify({ |
| 74 | 'paths': { | 81 | 'paths': { |
| 75 | - 'pdfjs-dist/build/pdf': '$_pdfJsVersion/pdf.min', | ||
| 76 | - 'pdfjs-dist/build/pdf.worker': '$_pdfJsVersion/pdf.worker.min', | 82 | + 'pdfjs-dist/build/pdf': '$_pdfJsUrlBase/build/pdf.min', |
| 83 | + 'pdfjs-dist/build/pdf.worker': | ||
| 84 | + '$_pdfJsUrlBase/build/pdf.worker.min', | ||
| 77 | } | 85 | } |
| 78 | }) | 86 | }) |
| 79 | ]); | 87 | ]); |
| @@ -82,7 +90,11 @@ class PrintingPlugin extends PrintingPlatform { | @@ -82,7 +90,11 @@ class PrintingPlugin extends PrintingPlatform { | ||
| 82 | 90 | ||
| 83 | js.context.callMethod('require', <dynamic>[ | 91 | js.context.callMethod('require', <dynamic>[ |
| 84 | js.JsObject.jsify( | 92 | js.JsObject.jsify( |
| 85 | - ['pdfjs-dist/build/pdf', 'pdfjs-dist/build/pdf.worker']), | 93 | + [ |
| 94 | + 'pdfjs-dist/build/pdf', | ||
| 95 | + 'pdfjs-dist/build/pdf.worker', | ||
| 96 | + ], | ||
| 97 | + ), | ||
| 86 | (dynamic app) { | 98 | (dynamic app) { |
| 87 | js.context['pdfjsLib'] = app; | 99 | js.context['pdfjsLib'] = app; |
| 88 | completer.complete(); | 100 | completer.complete(); |
| @@ -93,7 +105,7 @@ class PrintingPlugin extends PrintingPlatform { | @@ -93,7 +105,7 @@ class PrintingPlugin extends PrintingPlatform { | ||
| 93 | } | 105 | } |
| 94 | 106 | ||
| 95 | js.context['pdfjsLib']['GlobalWorkerOptions']['workerSrc'] = | 107 | js.context['pdfjsLib']['GlobalWorkerOptions']['workerSrc'] = |
| 96 | - '$_pdfJsVersion/pdf.worker.min.js'; | 108 | + '$_pdfJsUrlBase/build/pdf.worker.min.js'; |
| 97 | } | 109 | } |
| 98 | 110 | ||
| 99 | _loading.release(); | 111 | _loading.release(); |
| @@ -288,10 +300,18 @@ class PrintingPlugin extends PrintingPlatform { | @@ -288,10 +300,18 @@ class PrintingPlugin extends PrintingPlatform { | ||
| 288 | ) async* { | 300 | ) async* { |
| 289 | await _initPlugin(); | 301 | await _initPlugin(); |
| 290 | 302 | ||
| 291 | - final t = PdfJs.getDocument(Settings()..data = document); | 303 | + final settings = Settings()..data = document; |
| 304 | + | ||
| 305 | + if (!_hasPdfJsLib) { | ||
| 306 | + settings | ||
| 307 | + ..cMapUrl = '$_pdfJsUrlBase/cmaps/' | ||
| 308 | + ..cMapPacked = true; | ||
| 309 | + } | ||
| 310 | + | ||
| 311 | + final jsDoc = PdfJs.getDocument(settings); | ||
| 292 | try { | 312 | try { |
| 293 | - final d = await promiseToFuture<PdfJsDoc>(t.promise); | ||
| 294 | - final numPages = d.numPages; | 313 | + final doc = await promiseToFuture<PdfJsDoc>(jsDoc.promise); |
| 314 | + final numPages = doc.numPages; | ||
| 295 | 315 | ||
| 296 | final html.CanvasElement canvas = | 316 | final html.CanvasElement canvas = |
| 297 | js.context['document'].createElement('canvas'); | 317 | js.context['document'].createElement('canvas'); |
| @@ -300,8 +320,9 @@ class PrintingPlugin extends PrintingPlatform { | @@ -300,8 +320,9 @@ class PrintingPlugin extends PrintingPlatform { | ||
| 300 | final _pages = | 320 | final _pages = |
| 301 | pages ?? Iterable<int>.generate(numPages, (index) => index); | 321 | pages ?? Iterable<int>.generate(numPages, (index) => index); |
| 302 | 322 | ||
| 303 | - for (final i in _pages) { | ||
| 304 | - final page = await promiseToFuture<PdfJsPage>(d.getPage(i + 1)); | 323 | + for (final pageIndex in _pages) { |
| 324 | + final page = | ||
| 325 | + await promiseToFuture<PdfJsPage>(doc.getPage(pageIndex + 1)); | ||
| 305 | try { | 326 | try { |
| 306 | final viewport = | 327 | final viewport = |
| 307 | page.getViewport(Settings()..scale = dpi / PdfPageFormat.inch); | 328 | page.getViewport(Settings()..scale = dpi / PdfPageFormat.inch); |
| @@ -339,7 +360,7 @@ class PrintingPlugin extends PrintingPlatform { | @@ -339,7 +360,7 @@ class PrintingPlugin extends PrintingPlatform { | ||
| 339 | } | 360 | } |
| 340 | } | 361 | } |
| 341 | } finally { | 362 | } finally { |
| 342 | - t.destroy(); | 363 | + jsDoc.destroy(); |
| 343 | } | 364 | } |
| 344 | } | 365 | } |
| 345 | } | 366 | } |
| @@ -37,6 +37,8 @@ class Settings { | @@ -37,6 +37,8 @@ class Settings { | ||
| 37 | external set scale(double value); | 37 | external set scale(double value); |
| 38 | external set canvasContext(CanvasRenderingContext2D value); | 38 | external set canvasContext(CanvasRenderingContext2D value); |
| 39 | external set viewport(PdfJsViewport value); | 39 | external set viewport(PdfJsViewport value); |
| 40 | + external set cMapUrl(String value); | ||
| 41 | + external set cMapPacked(bool value); | ||
| 40 | } | 42 | } |
| 41 | 43 | ||
| 42 | @anonymous | 44 | @anonymous |
-
Please register or login to post a comment