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.