WorldBrain / pdfjs-dist

Local mirror of prebuild https://github.com/mozilla/pdfjs-dist with some changes from worldbrain/pdf.js
Apache License 2.0
0 stars 0 forks source link

Property 'str' does not exist on type 'TextItem | TextMarkedContent'. #1

Open sajanpknair opened 2 years ago

sajanpknair commented 2 years ago

Dear Team: I have been trying to extract text from pdf and I am following this tutorial : https://onthecode.co.uk/parse-pdf-documents-in-angular-with-pdfjs/ .

'countPromises.push(textContent.items.map((s) => s.str).join(''));' is throwing error. It says "Property 'str' does not exist on type 'TextItem | TextMarkedContent'. ", in s.str. Any help would be appreciated.

Angular CLI: 13.2.3 Node: 14.15.5 Package Manager: npm 6.14.11 OS: darwin x64

Angular: 13.2.2 ... animations, common, compiler, compiler-cli, core, forms ... platform-browser, platform-browser-dynamic, router

Package Version

@angular-devkit/architect 0.1302.3 @angular-devkit/build-angular 13.2.3 @angular-devkit/core 13.2.3 @angular-devkit/schematics 13.2.3 @angular/cli 13.2.3 @schematics/angular 13.2.3 rxjs 7.5.4 typescript 4.5.5

tejasvaidya01 commented 1 year ago

I am also having the exact same issue. Did you find a solution for it?

darylteo commented 1 year ago

Unfortunately I could not find a way around this other than forcing it with a "any" cast.

Since the TextItem and TextMarkedContent classes are not exported by pdfjs-dist, there's no way to use instanceof to test against.

Trying with hasOwnProperty also does not suppress the error either.

You should guard against TextMarkedContent by checking if 'str' is undefined or falsey.

cbuchert commented 1 year ago

Ditto here. pdfjs is remarkable. Improved type accuracy and availability around TextItem | TextMarkedContent would be a delight. :)

tejasvaidya01 commented 1 year ago

Changing s.str to s['str'] fixed the issue for me. Thanks for the help.

DanBotero commented 1 year ago
// item: TextItem | TextMarkedContent
if ("src" in item) {
  // access to item
}
rightpossible commented 8 months ago
// item: TextItem | TextMarkedContent
if ("src" in item) {
  // access to item
}

wow thank my case i did this

    for (const textItem of textItems.items) {
                  if ("str" in textItem) {
                    text += textItem?.str;
                  }

                }
                setExtractedText(text);
                // console.log(extr actedText)
              } catch (error) {
                console.error('Error extracting text:', error);
                // Handle the error, e.g., display an error message to the user
              }