0

Good night people, I tell you.. I am working in node and express and I am getting the following error It turns out that my pdf at the moment has 3 pages, but it can vary. What I need to do is find a way to read the number of sheets that the PDF has, I'm using pdf.js.

So in summary: So what I need to do is do something in such a way that if the pdf has 3 pages, read me the 3 pages, if it has 4, read me the 4 pages and so on, I was reading the information that is https://mozilla.github.io /pdf.js/examples/ but it doesn't really fix much. Here's a picture of what I've done.

doc.numpages It returns the number of sheets, but when I use it by passing it to it, in this case, as numPages is = 3, it reads only the 3rd sheet

enter image description here

1 Answers1

0

It looks like you are only calling await doc.getPage() after counting all the pages, so you only ever get the last page.

I'd imagine you need to move the getPage and getTextContent calls into the for loop and save the results in a data structure like an array until you've read the whole PDF and are ready to return it. For example:

function getAllPages(doc) {
  let pages = [];
  for (let i = 1; i < doc.numPages; i++) {
    let page = await doc.getPage(i);
    let pageContent = await page.getTextContent();
    pages.push(pageContent);
  }
  return pages;
}

(P.S. it's much easier to help if you paste code as text instead of sharing a screenshot)

thehale
  • 913
  • 10
  • 18