pdf.js: pdf.js doesn't work with requirejs
The 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.
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 1
- Comments: 18 (7 by maintainers)
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.
There are two issues with the code:
Try something like:
Closing as answered.