pdf.js doesn't work with requirejs
See original GitHub issueThe following HTML demonstrates the issue:
<head>
<title>Flexpaper React/Require Sandbox</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.2.0/require.js"></script>
<script>
require(['pdf'], function(pdf) {
console.log('PDFJS Module: ' + pdf);
console.log('PDFJS Global: ' + window.PDFJS);
});
</script>
</head>
</html>
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.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:18 (7 by maintainers)
Top Results From Across the Web
How to use pdf.js with RequireJS? - Stack Overflow
Stick to this long name. It will work. "pdfjs-dist/build/pdf": { "cwd": "node_modules/pdfjs-dist/build" ...
Read more >Require.js Missing for PDF.js - Google Groups
I've run into an issue with PDF.js as you can see below in the attached image, I get the template for the PDF.js...
Read more >RequireJS
RequireJS is a JavaScript file and module loader. It is optimized for in-browser use, but it can be used in other JavaScript environments,...
Read more >Plugin Issue - 3D FlipBook
you need to copy all libraries from the pro version three.min.js, pdf.min.js, pdf.worker.js and update all other files as well. this error happens...
Read more >Javascript - How to fix ReferenceError: require is not defined
requirejs (["lodash"], function (lodash) { const headerEl = document. · document. · // load library from node_modules const lodash = require(" ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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
PDFJS.workerSrc
, as suggested in the examples, because the worker will be found automatically.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.