Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trouble upgrading pdfjs #2

Closed
hoclun-rigsep opened this issue Nov 23, 2023 · 3 comments
Closed

Trouble upgrading pdfjs #2

hoclun-rigsep opened this issue Nov 23, 2023 · 3 comments

Comments

@hoclun-rigsep
Copy link

Following your approach, I'd hoped to upgrade pdfjs by changing

<script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.8.335/pdf.min.js" integrity="sha512-SG4yH2eYtAR5eK4/VL0bhqOsIb6AZSWAJjHOCmfhcaqTkDviJFoar/VYdG96iY7ouGhKQpAg3CMJ22BrZvhOUA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

to

 <script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/4.0.189/pdf.min.mjs" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

but if I don't put type="module" in the script tag the file won't load—I get Uncaught SyntaxError: Unexpected identifier 'r' (at pdf.min.mjs:21:283861).

If I do put type="module" in the script tag, I get the following;

TypeError: Cannot read properties of undefined (reading 'GlobalWorkerOptions')
    at Object.demo$app$init [as init] (app.cljs:71:13)
    at eval (shadow.module.main.append.js:4:16)
    at eval (<anonymous>)
    at goog.globalEval (main.js:486:12)
    at env.evalLoad (main.js:1549:12)
    at main.js:1701:12

I am not sure what to do about this, as in both cases, window.pdfjsLib is defined, at least in the browser console, and has a 'GlobalWorkerOptions' prop.

@hoclun-rigsep
Copy link
Author

Ok I have just spent way too long on this but here is where I am with it. My patch:

diff --git a/package-lock.json b/package-lock.json
index edf98c7..a596505 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -5,6 +5,7 @@
   "requires": true,
   "packages": {
     "": {
+      "name": "reagent-pdfjs",
       "version": "0.0.1",
       "dependencies": {
         "react": "^17.0.2",
diff --git a/public/index.html b/public/index.html
index 55332af..64c5e22 100644
--- a/public/index.html
+++ b/public/index.html
@@ -3,11 +3,15 @@
 <head>
     <meta charset="UTF-8">
     <title>demo</title>
-    <script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.8.335/pdf.min.js" integrity_no="sha512-SG4yH2eYtAR5eK4/VL0bhqOsIb6AZSWAJjHOCmfhcaqTkDviJFoar/VYdG96iY7ouGhKQpAg3CMJ22BrZvhOUA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
+    <script id="pdfjsscript" defer type="module" onload="console.log">
+      import * as pdfjs  from "https://cdnjs.cloudflare.com/ajax/libs/pdf.js/4.0.189/pdf.min.mjs";
+      window.pdfjs = pdfjs;
+      window.pdfjs.GlobalWorkerOptions.workerSrc =     "https://cdnjs.cloudflare.com/ajax/libs/pdf.js/4.0.189/pdf.worker.mjs";
+    </script>
 </head>
 <body>
 
 <div id="root"></div>
 <script src="/js/main.js"></script>
 </body>
-</html>
\ No newline at end of file
+</html>
diff --git a/src/main/demo/app.cljs b/src/main/demo/app.cljs
index 54832fe..8dedd8f 100644
--- a/src/main/demo/app.cljs
+++ b/src/main/demo/app.cljs
@@ -12,9 +12,7 @@
 ;; included via script tag in public/index.html
 ;; it provides the window['pdfjs-dist/build/pdf'] global variable
 
-(def ^js pdfjs (gobj/get js/window "pdfjs-dist/build/pdf"))
-
-(defn pdf-canvas [{:keys [url]}]
+(defn pdf-canvas [{:keys [url pdfjs]}]
   ;; ref
   (let [canvas-ref (react/useRef nil)]
 
@@ -57,17 +55,20 @@
     [:canvas {:ref canvas-ref}]))
 
 (defn app []
-  [:f> pdf-canvas {:url "https://raw.githubusercontent.com/mozilla/pdf.js/ba2edeae/examples/learning/helloworld.pdf"}])
+  [:f> pdf-canvas {:url "https://raw.githubusercontent.com/mozilla/pdf.js/ba2edeae/examples/learning/helloworld.pdf"
+                   :pdfjs pdfjs}])
 
 (def root-el
   (js/document.getElementById "root"))
 
 (defn ^:dev/after-load start []
-  (rdom/render [app] root-el))
-
-(defn init []
+  (def ^js pdfjs (gobj/get js/window "pdfjs"))
 
   ;; need to tell the lib where to load the worker from, also using same CDN
-  (set! (.. pdfjs -GlobalWorkerOptions -workerSrc) "https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.8.335/pdf.worker.min.js")
+  #_(set!
+    (.. pdfjs -GlobalWorkerOptions -workerSrc)
+    "https://cdnjs.cloudflare.com/ajax/libs/pdf.js/4.0.189/pdf.worker.mjs")
+  (rdom/render [app] root-el))
 
-  (start))
+(defn init []
+  (.addEventListener js/document "DOMContentLoaded" start))

As you see, I put "defer" on the script tag, and waited for DOMContentLoaded before running (start). This all seems good except for one thing:

Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "". Strict MIME type checking is enforced for module scripts per HTML spec.

That's in the browser console and the trace points to blob:http://localhost:8400/ea30bcbc-cc16-4b83-a766-6c17b2f8ee5b:1 . Clicking on that gets you

await import("https://cdnjs.cloudflare.com/ajax/libs/pdf.js/4.0.189/pdf.worker.mjs");

I have yet to determine what this does to functionality but the network tab confirms that the worker file does get successfully retrieved.

@hoclun-rigsep
Copy link
Author

Seems to be mozilla/pdf.js#17265

@thheller
Copy link
Owner

I updated the example to use the CDN ESM variant. Your approach wasn't ideal.

Getting the same MIME type error as you, but seems to work regardless.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants