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
pdf.js doesn't work with requirejs #7287
Comments
what's 'pdf' ? can you provide complete example? |
Sure, I'll setup a Github repo to demonstrate more fully. |
Here is the repo: https://github.com/MartinSnyder/pdfjs-with-requirejs 'pdf' is the module reference that require is attempting to return to that function. In that scenario, Requirejs is dynamically loading pdf.js and attempting to return the 'module reference' to the callback function, however the module reference is not set. You can see the dynamic load happen through the browser's network debugger. This would not be that big a deal normally, except that something inside pdf.js is detecting that a module loader is present and is then NOT setting the global PDFJS object. Since neither is present, you can't use the library when requirejs is on the page. Note that this happens even if you don't use require to load the object. I put a commented out script reference to pdf.js on the page. If that is placed before require, then the global will be set properly. If that is placed after require, then the global will NOT be set. |
There are two issues with the code:
Try something like:
Closing as answered. |
Working at this version is a sideeffect of requirejs loading a script as regular script tag -- does not indicate that it is a compatible with require.js
Global PDFJS will be removed in future versions, so if you are using requirejs you can use the same object via |
With respect to point 1: With respect to point 2: With respect to Global PDFJS being removed in future versions: |
I did not assume you are using node.js.
PDF.js is a library and consist from many files, e.g. pdfjs-dist/build/pdf module may load pdfjs-dist/build/pdf.worker module
You still need to use AMD name whatever is listed in the define, see https://github.com/MartinSnyder/pdfjs-with-requirejs/blob/master/pdf.js#L21 Did you try the example above with require.config pointing to the complete library? |
I just completed that now and it works as you said. Does that mean that you cannot use the "downloaded" distribution found here for this purpose? So long as you use the node 'pdfjs-dist' package you are fine, but the downloaded version isn't supposed to work with this? |
I think it's not related to the subject, but you still can use pdf.js/pdf.worker.js files from this package, point requirejs 'pdfjs-dist' to the zipped location (it will work since pdf.js and pdf.worker.js files are located in the build folder). |
This was the key bit that I was missing from my understanding. Thank you for taking the time to explain it to me. |
Hi, I have similar issue, so I am not going to open a new one, but ask here, I hope that is alright. Here is sample repo: https://github.com/Lazzi/pdfjs-bower-requirejs |
Yes, it is not AMD, but require.js supports also this kind of module definition: http://requirejs.org/docs/api.html#cjsmodule. However, your proposed change didn't fix my issue.. |
@yurydelendik It works! You have been very helpful. Thank you. |
this may work for you as well being placed in your require.config
The map function is essentially a global string replace and the '*' is which dependencies to use the string replace for -- mostly used for including different versions of libs for other libs. Like having 2 versions of JQuery running in parallel. Be careful using -- since it only does string replacement you might end up replacing paths in other dependencies if they match. The '*' in this case will prob only be the main pdf.js dependency. |
As I was struggling a bit with correct referencing in RequireJS too, I will leave my solution, which is part of the RequireJS config:
When you do it like that, you do not even have to assign the path to the worker to |
Thank you @dmaxweiler! I struggled with the same issue -- I was having trouble getting pdf.js to load correctly when using RequireJS also. Your solution worked for me! Awesome. Thanks for your help. |
The following HTML demonstrates the issue:
pdf.js, detects the existence of the "require" and "define" functions and accordingly does NOT set the global PDFJS instance. However, it also does not return a reference to that instance through the module loader.
Because both "pdf" and "window.PDFJS" are undefined in the example above, the library cannot be used in applications that use require. This is the case regardless of whether or not require is used to load pdf.js.
Note that this works correctly with version 1.0.1040 but fails with 1.4.20 and 1.5.188. I did not test with versions other than those 3. The behavior was consistent in all three versions between Chrome and Safari.
The text was updated successfully, but these errors were encountered: