Is it possible to extract how many images and if they have alt text, tables, interactive elements and page count from a PDF using PDF.js?

Viewed 101

I am wanting to build a simple tool that can evaluate any given PDF and see how many pages it has if it has images and if so how many (and if they have alt text) and to give me a total count of how many tables, interactive elements, etc. there are. I am getting the PDFs via user upload as such I am having to deal with buffers. Is this task possible using PDF.js or something else? I have taken a look at pdf-parser and it works well for getting page count, but not more details about how many images there are if they have alt text, etc. I also tried PDF2JSON but its parsebuffer method was not working. I haven't been able to find much on the docs or Google about this. Thank you all!

Update: If the PDF has some tags I am able to get those tags from the PDFs structure with the following code as they seem to be positioned in the ops.argsArray[i][0], however, I am not successfully locating the alt text. Any advice?

    let doc = await pdfJS.getDocument({ data }).promise;
    console.log("here is the document");
    console.log(doc);
    console.log("here is doc info");
    console.log(doc._pdfInfo);
    console.log("here is meta data");
    let metaData = await doc.getMetadata();
    console.log(metaData);
    pageCount = doc.numPages;
    Array.from({ length: doc.numPages }, async (v, i) => {
      pages = await doc.getPage(i + 1);
      console.log("here is page");
      console.log(pages);

      const ops = await pages.getOperatorList();
      console.log("here is ops");
      console.log(ops);
      console.log("here is the function array");
      console.table(ops.fnArray);
      console.log("here is args array");
      console.table(ops.argsArray);
      for (let i = 0; i < ops.fnArray.length; i++) {
        if (Array.isArray(ops.argsArray[i])) {
          console.log("this is data from ops");
          console.log(ops.argsArray[i]);
          console.log("this is info from ops.argsArray[i][0]");
          console.log(ops.argsArray[i][0]);
          console.log("this is data from ops.argsArray[i][0][0]");
          console.log(ops.argsArray[i][0][0]);
          if (typeof ops.argsArray[i][0] === "string") {
            stringArray.push(ops.argsArray[i][0]);
          }
          // pdfObjects.push(ops.argsArray[i][0]);
        } else if (ops.argsArray[i]) {
          // console.log("this is null");
        }
      }
      // console.log(stringArray);
    });
    return stringArray;
  }
0 Answers
Related