legalthings / pdf.js-viewer

Compiled version of pdf.js viewer, modified to run embedded
Apache License 2.0
45 stars 55 forks source link

require('../build/pdf.js') not working #12

Open bastienmoulia opened 7 years ago

bastienmoulia commented 7 years ago

I got an error on pdfjsLib = require('../build/pdf.js'); (https://github.com/legalthings/pdf.js-viewer/blob/master/pdf.js#L12062)

Indeed there is no more build folder, maybe a patch for pdf.js is missing.

brandondrew commented 7 years ago

I ran into this too. The problem seems to be that pdf.js is assuming that nothing else is using require, and so it is branching on the presence or absence of require, based on the assumption that nothing else could have caused require to be present.

I have a very hacky solution, not remotely suitable for PR, but perhaps looking at the code that worked in my case might help you solve it for yours, and might lead to a general solution for everyone getting adopted for this project. I simply wrap the failing line in a try block, and recover by doing what is (in my case) the right thing:

  if (typeof require === 'function') {
   try {
    pdfjsLib = require('../build/pdf.js');
   }
   // when require fails, we do the right thing
   catch(e) {
    pdfjsLib = window['pdfjs-dist/build/pdf'];
   }
  } else {
   pdfjsLib = window['pdfjs-dist/build/pdf'];
  }
pdemilly commented 7 years ago

I am using webpack and having the same error. It seems to me that this part of code is trying to load pdfjs-dist pdf.js file. Does it need to be loaded first. if so would it not be something lke

require ('pdfjs-dist/build/pdf.js') better suited?

moesjarraf commented 7 years ago

This issue is known in the mozilla repo and has been fixed in the newer version of mozilla/pdf.js by changing the order of require. We have to upgrade our fork of pdfjs and then upgrade the source of pdf.js-viewer based on the fork.

The thing is, there hasn't been a stable release after the fix was made by mozilla on 11th of march. I recommend only upgrading the source after they create a new Latest release, so we can be sure that the release has no new undiscovered bugs. The current latest release i see is v1.7.225 from jan 26th.

netzwerkwelten commented 7 years ago

Will this issue be fixed next time?

PLQin commented 3 years ago

edit webpack config or vue.config.js

config.module.rules.push({
    test: /\.pdf$/,
    use: {
        loader: 'file-loader',
        options: {
            name: '[path][name].[ext]',
        },
    },
});

// vue.config.js configureWebpack 中
config.module
    .rule('pdf')
    .test(/\.pdf$/)
    .use('pdf')
    .loader('file-loader')
    .end();

config.module
    .rule('pdf')
    .test(/\.pdf$/)
    .use('file-loader?name=[path][name].[ext]')
    .loader('file-loader')
    .end();