No PDFJS.workerSrc specified

前端 未结 5 1194
借酒劲吻你
借酒劲吻你 2021-01-07 23:18

Trying to use PDF JS in a local Apache server and receiving the following error in console:

Uncaught Error: No PDFJS.workerSrc specified

Th

相关标签:
5条回答
  • 2021-01-07 23:51

    I had a similar error and I fixed it by specifying the pdf.worker.js explicitly at the end of the pdf.js

    if (!PDFJS.workerSrc && typeof document !== 'undefined') {
      // workerSrc is not set -- using last script url to define default location
      ****** I have no clue what the code below hope to accomplish ********
      ****** How can it locate the script container by assuming it ********
      ****** always would be at the end of <body> or <head> ????   ********
      PDFJS.workerSrc = (function () {
        'use strict';
        var scriptTagContainer = document.body ||
                                 document.getElementsByTagName('head')[0];
        var pdfjsSrc = scriptTagContainer.lastChild.src;
        return pdfjsSrc && pdfjsSrc.replace(/\.js$/i, '.worker.js');
      })();
    
    
      ****** Here I just hardcode the location of the needed file *********
      ****** This is the part that makes it work.                 *********
      ****** Obviously, tailor this to the same path of pdf.js    *********
      PDFJS.workerSrc = '/static/js/pdf.worker.js';
    }
    
    0 讨论(0)
  • 2021-01-08 00:01

    Specify psd.worker.js file path in your page where you want to use the pdf.js file (viewer.html in case you are using viewer.html come with distribution bundle) like this. It work for me.

    <script>
        PDFJS.workerSrc ='path to psd.worker.js';
    

    0 讨论(0)
  • 2021-01-08 00:05

    Go to pdf.js

    search function getWorkerSrc()

    replace this lines

    pdfjsFilePath = "YOUR_PATH_TO_JS_FILE/pdf.worker.js";
    if (pdfjsFilePath) {
      return pdfjsFilePath;
    } 
    
    0 讨论(0)
  • 2021-01-08 00:10

    Include compatibility.js to fix the "Uncaught Error: No PDFJS.workerSrc specified" error on IE11.

    https://github.com/mozilla/pdf.js/blob/master/src/shared/compatibility.js

    <script src="compatibility.js"></script>
    <script src="pdf.js"></script>
    

    compatibility.js implements any missing functionality required by PDFJS.

    Note: It should be loaded before PDFJS, not after.

    0 讨论(0)
  • 2021-01-08 00:15

    I added below code in end of pdf.js and working fine

    if (!PDFJS.workerSrc && typeof document !== 'undefined') {
      PDFJS.workerSrc = (function () {
        'use strict';
        var scriptTagContainer = document.body ||
                                 document.getElementsByTagName('head')[0];
        var pdfjsSrc = scriptTagContainer.lastChild.src;
        return pdfjsSrc && pdfjsSrc.replace(/\.js$/i, '.worker.js');
      })();
      PDFJS.workerSrc = 'pdf.worker.js';
    }
    
    0 讨论(0)
提交回复
热议问题