Pdf.js: Uncaught ReferenceError: PDFJS is not defined

Created on 23 Nov 2018  路  2Comments  路  Source: mozilla/pdf.js

Attach (recommended) or Link to PDF file here:

Configuration:

  • Web browser and its version:
    Chrome 70.0
  • Operating system and its version:
    Win 7 Sp1

  • PDF.js version:
    2.0.943

  • Is a browser extension:
    No

Steps to reproduce the problem:

  1. Instaled from oficial page in Web https://mozilla.github.io/pdf.js/
  2. Make all steps from tutorial: https://ourcodeworld.com/articles/read/405/how-to-convert-pdf-to-text-extract-text-from-pdf-with-javascript
  3. I wrote code below:
    `



    PDF JS Test




    File open
    File open
<script>
    // Path to PDF file
    var PDF_URL = 'web/mandado_1.pdf';
    // Specify the path to the worker
    PDFJS.workerSrc = 'build/pdf.worker.js';

    /*
    /* Retrieves the text of a specif page within a PDF Document obtained through pdf.js 
     * 
     * @param {Integer} pageNum Specifies the number of the page 
     * @param {PDFDocument} PDFDocumentInstance The PDF document obtained 
     **/
    function getPageText(pageNumber, PDFDocumentInstance) {
        // Return a Promise that is solved once the text of the page is retrieven
        return new Promise(function (resolve, reject) {
            PDFDocumentInstance.getPage(pageNum).then(function (pdfPage) {
                // The main trick to obtain the text of the PDF page, use the getTextContent method
                pdfPage.getTextContent().then(function (textContent) {
                    var textItems = textContent.items;
                    var finalString = "";

                    // Concatenate the string of the item to the final string
                    for (var i = 0; i < textItems.length; i++) {
                        var item = textItems[i];

                        finalString += item.str + " ";
                    }

                    // Solve promise with the text retrieven from the page
                    resolve(finalString);
                });
            });
        });
    }

    PDFJS.getDocument(PDF_URL).then(function (PDFDocumentInstance) {

        // Use the PDFDocumentInstance To extract the text later

    }, function (reason) {
        // PDF loading error
        console.error(reason);
    });     

</script>



`

What is the expected behavior? (add screenshot)
show converted text

What went wrong? (add screenshot)
the error above in title:
Uncaught ReferenceError: PDFJS is not defined

Link to a viewer (if hosted on a site other than mozilla.github.io/pdf.js or as Firefox/Chrome extension):

All 2 comments

The global PDFJS object is removed in version 2.0, so the tutorial you're using is out of date for that version. Refer to the examples folder for how to use PDF.js 2.0, for example https://github.com/mozilla/pdf.js/blob/master/examples/learning/helloworld.html.

i just download pdf.js 1.9 version and this code worked . You can use this code with old pdf.worker.js

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ousia picture ousia  路  29Comments

collinanderson picture collinanderson  路  29Comments

edwardmeng picture edwardmeng  路  31Comments

syssgx picture syssgx  路  29Comments

Richard-Mlynarik picture Richard-Mlynarik  路  32Comments