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.

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

See original GitHub issue

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');**

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:15 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
szabocsabicommented, Dec 13, 2017

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.

1reaction
Snuffleupaguscommented, Dec 5, 2017

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";
Read more comments on GitHub >

github_iconTop Results From Across the Web

pdf.js not loading pdf file - Stack Overflow
So I moved what I had in my js file into my html file and put it in script tags and now it...
Read more >
Easy Web PDF Viewer With jQuery And pdf.js - pdfjs-viewer
A jQuery Plugin that provides a simple way for your users to view PDF documents directly in their web browser.
Read more >
How to create a PDF Viewer in JavaScript - Educative.io
PDF.js is a JavaScript library maintained by Mozilla and designed for handling PDFs in JavaScript. We are going to create a PDF viewer...
Read more >
Rendering PDF Files in the Browser with PDF.js - PSPDFKit
In this tutorial, we'll go a bit deeper and examine one of the most popular open source libraries for rendering PDF files in...
Read more >
Reinstatement Form - Investors Heritage Life Insurance ...
web/pdfviewer.js cannot load build/pdf.js Issue #9239 ... Landers. esq gmail.com Counsel for Relator Dick Flanagan Christopher J. Gagin 0062820 Of Counsel ...
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