web-dev-qa-db-fra.com

Aucun fichier PDFJS.workerSrc spécifié

Essayez d'utiliser PDF JS sur un serveur Apache local et de recevoir l'erreur suivante dans la console:

Uncaught Error: No PDFJS.workerSrc specified

C'est très bizarre, car je suis tout ce que les exemples spécifient ici http://mozilla.github.io/pdf.js/examples/ .

J'ai, dans mon dossier principal, un exemple de fichier appelé file.pdf et j'essaie simplement de l'afficher. Je l'ai fait en utilisant un iframe avec un paramètre file:

<iframe src="./web/viewer.html?file=http://localhost:99/PDF/arquivo.pdf" width="1800px" height="900px" />

Et maintenant, j'essaie d'utiliser l'API JavaScript pour l'afficher. J'essaie de faire:

<!DOCTYPE html>
<html>
    <head>
        <script src="./build/pdf.js" type="text/javascript"></script>       
        <script type="text/javascript">
            PDFJS.getDocument('arquivo.pdf').then(function(pdf) {
                // Here I use it
            })
        </script>
    </head>
    <body>
    </body>
</html>

Si j'essaie d'inclure pdf.worker.js manuellement, je reçois:

GET http://localhost:99/PDF/build/pdf.worker.worker.js 404 (Not Found)

car il inclut par programme pdf.worker.js.

Avec l'exemple de code que j'ai posté ici, je reçois un journal et une erreur:

Error: No PDFJS.workerSrc specified pdf.js:249
    at error (http://localhost:99/PDF/build/pdf.js:251:15)
    at Object.WorkerTransport (http://localhost:99/PDF/build/pdf.js:2305:9)
    at Object.getDocument (http://localhost:99/PDF/build/pdf.js:1805:15)
    at http://localhost:99/PDF/:6:10 pdf.js:251
Warning: Unsupported feature "unknown" pdf.js:234
Uncaught Error: No PDFJS.workerSrc specified

Dois-je spécifier manuellement pdf.worker.js? S'il vous plaît, que puis-je essayer de résoudre?

Merci beaucoup!

(*) - Je constate un manque de contenu et une documentation bien expliquée de PDF.JS.

14
Marcelo Camargo

J'ai eu une erreur similaire et je l'ai corrigée en spécifiant explicitement pdf.worker.js à la fin du fichier 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';
}
18
Will

Incluez compat.js pour corriger l'erreur "Erreur non capturée: aucun fichier PDFJS.workerSrc spécifié" sur IE11.

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

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

compatibilité.js implémente toute fonctionnalité manquante requise par PDFJS.

Note: Il devrait être chargé avant PDFJS, pas après.

11
ProgrammerGuy

Spécifiez le chemin du fichier psd.worker.js dans votre page vers laquelle vous souhaitez utiliser le fichier pdf.js (viewer.html si vous utilisez viewer.html fourni avec un ensemble de distribution), comme ceci. Ça marche pour moi.

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

6
R.K.Saini

J'ai ajouté le code ci-dessous à la fin de pdf.js et fonctionne bien

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
Pankaj Upadhyay