|
| 1 | +--- |
| 2 | +title: Using PDFViewer with PDF.js Version 4.x.x or Later |
| 3 | +description: Learn how to configure the Telerik UI for {{ site.framework }} PDFViewer to use PDF.js version 4.x.x for PDF processing. |
| 4 | +type: how-to |
| 5 | +page_title: Configuring the PDFViewer component to use PDF.js version 4.x.x |
| 6 | +slug: pdfviewer-pdfjs-script-workarounds |
| 7 | +tags: pdfviewer, pdf.js, version, 4.x.x, processing, script |
| 8 | +res_type: kb |
| 9 | +--- |
| 10 | + |
| 11 | +## Environment |
| 12 | +<table> |
| 13 | + <tbody> |
| 14 | + <tr> |
| 15 | + <td>Product Version</td> |
| 16 | + <td>2024.4.1112 or later</td> |
| 17 | + </tr> |
| 18 | + <tr> |
| 19 | + <td>Product</td> |
| 20 | + <td>PDFViewer for Progress® Telerik® {{ site.product_short }}</td> |
| 21 | + </tr> |
| 22 | + </tbody> |
| 23 | +</table> |
| 24 | + |
| 25 | +## Description |
| 26 | +The {% if site.core %}<a href="https://www.telerik.com/support/whats-new/aspnet-core-ui/release-history/telerik-ui-for-asp-net-core-2024-4-1112-(2024-q4)" target="_blank">2024 Q4 November release (version 2024.4.1112)</a>{% else%}<a href="https://www.telerik.com/support/whats-new/aspnet-mvc/release-history/telerik-ui-for-asp-net-mvc-2024-4-1112-(2024-q4)" target="_blank">2024 Q4 November release (version 2024.4.1112)</a>{% endif %} introduced a new common engine for the PDFViewer component and support for the latest <a href="https://mozilla.github.io/pdf.js/" target="_blank">PDF.js library version 4. x.x</a>. Since PDF.js 4 (versions 4.x.x) uses <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules" target="_blank">ECMAScript modules</a>, the required Kendo UI scripts must be included as modules as well. |
| 27 | + |
| 28 | +The {{ site.product }} versions before 2024.4.1112 are not compatible with PDF.js version 4.x. You must use either PDF.js version 2.x or 3.x. For reference, the example below shows the PDFViewer configured for PDF.js processing when using versions before 2024.4.1112. |
| 29 | + |
| 30 | +```HtmlHelper |
| 31 | + <script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.2.2/pdf.js"></script> |
| 32 | + <script> |
| 33 | + window.pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.2.2/pdf.worker.js'; |
| 34 | + </script> |
| 35 | +
|
| 36 | + @(Html.Kendo().PDFViewer() |
| 37 | + .Name("pdfviewer") |
| 38 | + .PdfjsProcessing(pdf => pdf |
| 39 | + .File("") |
| 40 | + ) |
| 41 | + .Height(1200) |
| 42 | + ) |
| 43 | +``` |
| 44 | +{% if site.core %} |
| 45 | +```TagHelper |
| 46 | + <script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.2.2/pdf.js"></script> |
| 47 | + <script> |
| 48 | + window.pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.2.2/pdf.worker.js'; |
| 49 | + </script> |
| 50 | +
|
| 51 | + <kendo-pdfviewer name="pdfviewer" height="1200"> |
| 52 | + <pdfjs-processing file="@Url.Content("")" /> |
| 53 | + </kendo-pdfviewer> |
| 54 | +``` |
| 55 | +{% endif %} |
| 56 | + |
| 57 | +When upgrading to version 2024.4.1112 or later, including the [required Kendo UI scripts]({% slug copyclientresources_aspnetmvc6_aspnetmvc%}) by adding `type="module"` in the script tags will throw the following client-side error: |
| 58 | + |
| 59 | + |
| 60 | +However, adding the scripts without `type="module"` is also not an option because the PDF.js library requires module scripts: |
| 61 | + |
| 62 | + |
| 63 | +## Solution |
| 64 | + |
| 65 | +Apply any of the following approaches when using {{ site.product }} PDFViewer (version 2024.4.1112 or later): |
| 66 | + |
| 67 | +- [Loading Kendo UI scripts twice](#loading-kendo-ui-scripts-twice) |
| 68 | +- [Loading only the required PDFViewer scripts](#loading-only-the-required-pdfviewer-scripts) |
| 69 | +- [Using RenderAsModule option for module-based script initialization](#using-renderasmodule-for-module-based-script-initialization) |
| 70 | +- [Compiling the PDF.js scripts to UMD modules](#compiling-pdfjs-scripts-to-umd-modules) |
| 71 | + |
| 72 | +### Loading Kendo UI Scripts Twice |
| 73 | + |
| 74 | +The easiest workaround is to include the Kendo UI scripts twice—with and without `type="module"`: |
| 75 | + |
| 76 | +```HtmlHelper |
| 77 | + <link href="https://kendo.cdn.telerik.com/themes/10.0.1/default/default-ocean-blue.css" rel="stylesheet" type="text/css" /> |
| 78 | +
|
| 79 | + <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script> |
| 80 | +
|
| 81 | + <!-- Add the Kendo UI scripts in standard fashion. --> |
| 82 | + <script src="https://cdn.kendostatic.com/2024.4.1112/js/kendo.all.min.js"></script> |
| 83 | + <script src="https://cdn.kendostatic.com/2024.4.1112/js/kendo.aspnetmvc.min.js"></script> |
| 84 | +
|
| 85 | + <!-- Add the "pdf.mjs" and "pdf.worker.mjs" module scripts. --> |
| 86 | + <script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/4.3.136/pdf.mjs" type="module"></script> |
| 87 | + <script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/4.3.136/pdf.worker.mjs" type="module"></script> |
| 88 | +
|
| 89 | + <!-- Add the Kendo UI script as a module afterwards --> |
| 90 | + <script src="https://cdn.kendostatic.com/2024.4.1112/js/kendo.all.min.js" type="module"></script> |
| 91 | +
|
| 92 | + @(Html.Kendo().PDFViewer().Name("pdfviewer") |
| 93 | + .PdfjsProcessing(pdf => pdf.File(@Url.Content("../sample.pdf"))) |
| 94 | + .Height(1200) |
| 95 | + ) |
| 96 | + @(Html.Kendo().DateTimePicker().Name("picker")) |
| 97 | +``` |
| 98 | +{% if site.core %} |
| 99 | +```TagHelper |
| 100 | + <link href="https://kendo.cdn.telerik.com/themes/10.0.1/default/default-ocean-blue.css" rel="stylesheet" type="text/css" /> |
| 101 | +
|
| 102 | + <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script> |
| 103 | +
|
| 104 | + <!-- Add the Kendo UI scripts in standard fashion. --> |
| 105 | + <script src="https://cdn.kendostatic.com/2024.4.1112/js/kendo.all.min.js"></script> |
| 106 | + <script src="https://cdn.kendostatic.com/2024.4.1112/js/kendo.aspnetmvc.min.js"></script> |
| 107 | +
|
| 108 | + <!-- Add the "pdf.mjs" and "pdf.worker.mjs" module scripts. --> |
| 109 | + <script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/4.3.136/pdf.mjs" type="module"></script> |
| 110 | + <script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/4.3.136/pdf.worker.mjs" type="module"></script> |
| 111 | +
|
| 112 | + <!-- Add the Kendo UI script as a module afterwards --> |
| 113 | + <script src="https://cdn.kendostatic.com/2024.4.1112/js/kendo.all.min.js" type="module"></script> |
| 114 | +
|
| 115 | + <kendo-pdfviewer name="pdfviewer" height="1200"> |
| 116 | + <pdfjs-processing file-url="@Url.Content("../sample.pdf")" /> |
| 117 | + </kendo-pdfviewer> |
| 118 | +
|
| 119 | + <kendo-datetimepicker name="picker"/> |
| 120 | +``` |
| 121 | +{% endif %} |
| 122 | + |
| 123 | +### Loading Only the Required PDFViewer Scripts |
| 124 | + |
| 125 | +Instead of loading the whole "kendo.all.min.js" script file twice, you can include only the specific PDFViewer scripts. Also, you can load the scripts and the PDFViewer declaration through a Partial View: |
| 126 | + |
| 127 | +```_Layout |
| 128 | + <link rel="stylesheet" href="https://kendo.cdn.telerik.com/themes/10.0.1/default/default-ocean-blue.css"> |
| 129 | + <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script> |
| 130 | + <script src="https://kendo.cdn.telerik.com/2024.4.1112/js/kendo.all.min.js"></script> |
| 131 | + <script src="https://kendo.cdn.telerik.com/2024.4.1112/js/kendo.aspnetmvc.min.js"></script> |
| 132 | +
|
| 133 | + <partial name="PDFViewer" /> |
| 134 | +``` |
| 135 | +```HtmlHelper |
| 136 | + <script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/4.3.136/pdf.mjs" type="module"></script> |
| 137 | + <script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/4.3.136/pdf.worker.mjs" type="module"></script> |
| 138 | + <script src="https://kendo.cdn.telerik.com/2024.4.1112/mjs/kendo.pdfviewer-common.cmn.chunk.js" type="module"></script> |
| 139 | + <script src="https://kendo.cdn.telerik.com/2024.4.1112/mjs/kendo.pdfviewer.js" type="module"></script> |
| 140 | +
|
| 141 | + @(Html.Kendo().PDFViewer() |
| 142 | + .Name("pdfviewer") |
| 143 | + .PdfjsProcessing(pdf => pdf.File("")) |
| 144 | + .Height(1200) |
| 145 | + ) |
| 146 | +``` |
| 147 | +{% if site.core %} |
| 148 | +```TagHelper |
| 149 | + <script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/4.3.136/pdf.mjs" type="module"></script> |
| 150 | + <script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/4.3.136/pdf.worker.mjs" type="module"></script> |
| 151 | + <script src="https://kendo.cdn.telerik.com/2024.4.1112/mjs/kendo.pdfviewer-common.cmn.chunk.js" type="module"></script> |
| 152 | + <script src="https://kendo.cdn.telerik.com/2024.4.1112/mjs/kendo.pdfviewer.js" type="module"></script> |
| 153 | +
|
| 154 | + <kendo-pdfviewer name="pdfviewer1" height="1200"> |
| 155 | + <pdfjs-processing /> |
| 156 | + </kendo-pdfviewer> |
| 157 | +``` |
| 158 | +{% endif %} |
| 159 | + |
| 160 | +### Using RenderAsModule for Module-Based Script Initialization |
| 161 | + |
| 162 | +Another solution is to include the required Kendo UI scripts as modules and enable the `RenderAsModule` option, which will add `type="module"` to the initialization scripts of all Telerik UI components in the application. |
| 163 | + |
| 164 | +Also, it is important to ensure that `type="module"` is added to all script tags that contain custom logic related to the Telerik UI components. |
| 165 | + |
| 166 | +{% if site.core %} |
| 167 | +```Program.cs |
| 168 | + builder.Services.AddKendo(x => x.RenderAsModule = true); |
| 169 | +``` |
| 170 | +{% else %} |
| 171 | +```Global.asax |
| 172 | + KendoMvc.Setup(options => |
| 173 | + { |
| 174 | + options.RenderAsModule = true; |
| 175 | + }); |
| 176 | +``` |
| 177 | +{% endif %} |
| 178 | +```_Layout |
| 179 | + <link href="https://kendo.cdn.telerik.com/themes/10.0.1/default/default-ocean-blue.css" rel="stylesheet" type="text/css" /> |
| 180 | + <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script> |
| 181 | + <script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/4.8.69/pdf.mjs" type="module"></script> |
| 182 | + <script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/4.8.69/pdf.worker.mjs" type="module"></script> |
| 183 | + <script src="https://cdn.kendostatic.com/2024.4.1112/js/kendo.all.min.js" type="module"></script> |
| 184 | + <script src="https://cdn.kendostatic.com/2024.4.1112/js/kendo.aspnetmvc.min.js" type="module"></script> |
| 185 | +``` |
| 186 | +```HtmlHelper |
| 187 | + @(Html.Kendo().PDFViewer() |
| 188 | + .Name("pdfviewer") |
| 189 | + .PdfjsProcessing(pdf => pdf.File("")) |
| 190 | + .Height(800) |
| 191 | + ) |
| 192 | +
|
| 193 | + <script type="module"> |
| 194 | + $(document).ready(() => { |
| 195 | + var pdfviewer = $('#pdfviewer').data('kendoPdfViewer'); |
| 196 | + console.log(pdfviewer); |
| 197 | + }) |
| 198 | + </script> |
| 199 | +``` |
| 200 | +{% if site.core %} |
| 201 | +```TagHelper |
| 202 | + <kendo-pdfviewer name="pdfviewer" height="800"> |
| 203 | + <pdfjs-processing file="@Url.Content("")" /> |
| 204 | + </kendo-pdfviewer> |
| 205 | +
|
| 206 | + <script type="module"> |
| 207 | + $(document).ready(() => { |
| 208 | + var pdfviewer = $('#pdfviewer').data('kendoPdfViewer'); |
| 209 | + console.log(pdfviewer); |
| 210 | + }) |
| 211 | + </script> |
| 212 | +``` |
| 213 | +{% endif %} |
| 214 | + |
| 215 | + |
| 216 | +### Compiling PDF.js Scripts to UMD Modules |
| 217 | + |
| 218 | +Theoretically, you can download the PDF.js scripts and use a JS bundler tool like [webpack](https://webpack.js.org/) to compile them to UMD modules. |
| 219 | + |
| 220 | +## More {{ site.framework }} PDFViewer Resources |
| 221 | + |
| 222 | +* [{{ site.framework }} PDFViewer Documentation]({%slug htmlhelpers_pdfviewer_aspnetcore%}) |
| 223 | + |
| 224 | +* [{{ site.framework }} PDFViewer Demos](https://demos.telerik.com/{{ site.platform }}/pdfviewer/index) |
| 225 | + |
| 226 | +{% if site.core %} |
| 227 | +* [{{ site.framework }} PDFViewer Product Page](https://www.telerik.com/aspnet-core-ui/pdf-viewer) |
| 228 | + |
| 229 | +* [Telerik UI for {{ site.framework }} Video Onboarding Course (Free for trial users and license holders)]({%slug virtualclass_uiforcore%}) |
| 230 | + |
| 231 | +* [Telerik UI for {{ site.framework }} Forums](https://www.telerik.com/forums/aspnet-core-ui) |
| 232 | + |
| 233 | +{% else %} |
| 234 | +* [{{ site.framework }} PDFViewer Product Page](https://www.telerik.com/aspnet-mvc/pdf-viewer) |
| 235 | + |
| 236 | +* [Telerik UI for {{ site.framework }} Video Onboarding Course (Free for trial users and license holders)]({%slug virtualclass_uiformvc%}) |
| 237 | + |
| 238 | +* [Telerik UI for {{ site.framework }} Forums](https://www.telerik.com/forums/aspnet-mvc) |
| 239 | +{% endif %} |
| 240 | + |
| 241 | +## See Also |
| 242 | + |
| 243 | +* [Client-Side API Reference of the PDFViewer for {{ site.framework }}](https://docs.telerik.com/kendo-ui/api/javascript/ui/pdfviewer) |
| 244 | +* [Server-Side API Reference of the PDFViewer for {{ site.framework }}](https://docs.telerik.com/{{ site.platform }}/api/pdfviewer) |
| 245 | +{% if site.core %} |
| 246 | +* [Server-Side TagHelper API Reference of the PDFViewer for {{ site.framework }}](https://docs.telerik.com/{{ site.platform }}/api/taghelpers/pdfviewer) |
| 247 | +{% endif %} |
| 248 | +* [Telerik UI for {{ site.framework }} Breaking Changes]({%slug breakingchanges_2023%}) |
| 249 | +* [Telerik UI for {{ site.framework }} Knowledge Base](https://docs.telerik.com/{{ site.platform }}/knowledge-base) |
0 commit comments