question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

pdf.js doesn't work with requirejs

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:18 (7 by maintainers)

github_iconTop GitHub Comments

15reactions
dwaxweilercommented, Nov 30, 2017

As I was struggling a bit with correct referencing in RequireJS too, I will leave my solution, which is part of the RequireJS config:

paths: {
    'pdfjs-dist/build/pdf': 'myfolder/pdf.min',
    'pdfjs-dist/build/pdf.worker': 'myfolder/pdf.worker.min'
}

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.

4reactions
allandalycommented, Jan 8, 2018

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found