0

Ok. I have a function call in a .js file to check the total number of pages a PDF has. The function is also in the .js file and it works beautiful unless I remove an alert before the return value in the function. I know since PDF.js works asynchronously, the process is way too fast to grab the variables and display them. Here's the function code:

function funcReadPDF(strPDFPath) {

    var intNumberOfPagesPDF;
        var objLibPDF = window['pdfjs-dist/build/pdf'], objDocPDF_ = null;

        //pdf.worker.js
        objLibPDF.GlobalWorkerOptions.workerSrc = './pdfjs/pdf.worker.js';

            objLibPDF.getDocument(strPDFPath).promise.then(function(objDocPDF_) {
                var objDocPDF = objDocPDF_;
                intNumberOfPagesPDF = objDocPDF.numPages;
            })

        alert("Pages: " + intNumberOfPagesPDF); // <-If I remove this alert, I don't get the numbers of the pages! If not, it works beautiful for every .PDF document! weird?

    return intNumberOfPagesPDF;
}

I already tried to check in the web and I think I need to use Promises, but all the examples studied led me to no avail. Any easy solution? Regards.

Joao
  • 85
  • 1
  • 9
  • Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Keith Mar 09 '20 at 16:48
  • Where's the closing bracket from your promise.then function? – James Mar 09 '20 at 17:09
  • @James - It was a typo. Done. Thanks! – Joao Mar 09 '20 at 17:17

0 Answers0