pdf.js: web/pdf_viewer.js cannot load build/pdf.js

Configuration:

  • Web browser and its version: Chrome, Version 62.0.3202.94 (Official Build) (64-bit)
  • Operating system and its version: Win 10
  • PDF.js version: 2.0.185, installed from npm

I’m using TypeScript with requirejs and trying to use pdfjs library.

My configuration is:

require.config({
  paths: {
    'pdfjs-dist': 'libs/pdfjs-dist',
  }
  shim: {
    'pdfjs-dist/web/pdf_viewer': {
      deps: ['pdfjs-dist/build/pdf']
    }
  }
});

The imports are:

import { PDFJSViewer } from "pdfjs-dist/web/pdf_viewer";
import * as PDFJSWorker from "pdfjs-dist/build/pdf.worker";

The problem is that when pdf_viewer tries to load the “…/build/pdf.js” with require from the “else” branch:

var pdfjsLib = void 0;
if (typeof window !== 'undefined' && window['pdfjs-dist/build/pdf']) {
  pdfjsLib = window['pdfjs-dist/build/pdf'];
} else {
  **pdfjsLib = require('../build/pdf.js');**
}
module.exports = pdfjsLib;

…but it throws the following error image

Since requirejs is using its own config one possible solution could be that giving the path defined in the require.config section. **pdfjsLib = require('pdfjs-dist/build/pdf');**

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 15 (1 by maintainers)

Most upvoted comments

Found a temporary solution.

<script>
    require(["pdfjs-dist/build/pdf"], function (pdfjs){
        window["pdfjs-dist/build/pdf"] = pdfjs;
        require(['app'], function (app) {
            new app.App();
        });
    });
</script>

Loading the pdfjs before bootstrapping the Application and attaching it to window

Since pdf_viewer.js has the following code this will work:

var pdfjsLib = void 0;
if (typeof window !== 'undefined' && window['pdfjs-dist/build/pdf']) {
    pdfjsLib = window['pdfjs-dist/build/pdf'];
} else {
    pdfjsLib = require('../build/pdf.js');
}

It’s not the proper way tho, but it works at least.

I will change the code as you mentioned but the problem is “build/pdf.js” doesn’t contain “PDFViewer” and “PDFLinkService”.

Have you tried the following import order instead?

import * as PDFJSMain from "pdfjs-dist/build/pdf";
import { PDFJSViewer } from "pdfjs-dist/web/pdf_viewer";