Solved problem with module PDF in Drupal 9

So today's problem - when using the PDF - PDF files module, we need to add a library pdf.js. 

After adding and adjusting the field in the material, we will get an error most of all because the module expects a library file to receive a module in format .js, but there only in the format .mjs.

In general, it is already solved by a patch of developers .

diff --git a/pdf.libraries.yml b/pdf.libraries.yml
index 1054e6c..f5239e1 100755
--- a/pdf.libraries.yml
+++ b/pdf.libraries.yml
@@ -5,7 +5,7 @@ mozilla.pdf.js:
     url: https://raw.githubusercontent.com/mozilla/pdf.js/master/LICENSE
     gpl-compatible: true
   js:
-    /libraries/pdf.js/build/pdf.js: {}
+    /libraries/pdf.js/build/pdf.mjs: { attributes: { type: module } }
     #//mozilla.github.io/pdf.js/build/pdf.js: { type: external, minified: false }
 drupal.pdf:
   dependencies:
diff --git a/src/Plugin/Field/FieldFormatter/PdfPages.php b/src/Plugin/Field/FieldFormatter/PdfPages.php
index 008f051..f894e40 100755
--- a/src/Plugin/Field/FieldFormatter/PdfPages.php
+++ b/src/Plugin/Field/FieldFormatter/PdfPages.php
@@ -72,7 +72,7 @@ class PdfPages extends FormatterBase {
       }
     }
     $elements['#attached']['library'][] = 'pdf/drupal.pdf';
-    $worker = \Drupal::service('file_url_generator')->generateAbsoluteString(base_path() . 'libraries/pdf.js/build/pdf.worker.js');
+    $worker = \Drupal::service('file_url_generator')->generateAbsoluteString(base_path() . 'libraries/pdf.js/build/pdf.worker.mjs');
     $elements['#attached']['drupalSettings'] = [
       'pdf' => [
         'workerSrc' => $worker,
diff --git a/src/Plugin/Field/FieldFormatter/PdfThumbnail.php b/src/Plugin/Field/FieldFormatter/PdfThumbnail.php
index 92bdbd3..23ce09d 100755
--- a/src/Plugin/Field/FieldFormatter/PdfThumbnail.php
+++ b/src/Plugin/Field/FieldFormatter/PdfThumbnail.php
@@ -92,7 +92,7 @@ class PdfThumbnail extends FormatterBase {
       }
     }
     $elements['#attached']['library'][] = 'pdf/drupal.pdf';
-    $worker = \Drupal::service('file_url_generator')->generateAbsoluteString(base_path() . 'libraries/pdf.js/build/pdf.worker.js');
+    $worker = \Drupal::service('file_url_generator')->generateAbsoluteString(base_path() . 'libraries/pdf.js/build/pdf.worker.mjs');
     $elements['#attached']['drupalSettings'] = [
       'pdf' => [
         'workerSrc' => $worker,

But after that we can get a mistake again, because our browser may not work the right image, especially if you have personal hosting and you have set up everything yourself. 

To solve the problem to file .htaccess add the line

AddType application/javascript .mjs

If you only use Nginx, the configuration file must be added

types {
    application/javascript mjs;
}

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
The comment language code.