Skip to content

Missing dots in extracted text #12100

Closed
Closed

Description

@TimotheAlbouy

Link to PDF file: https://www.welivesecurity.com/wp-content/uploads/2019/07/ESET_Okrum_and_Ketrican.pdf

Configuration:

  • Web browser and its version: Chrome 83.0.4103.116
  • Operating system and its version: macOS Catalina 10.15.5
  • PDF.js version: 2.4.456
  • Is a browser extension: No

Steps to reproduce the problem:

  1. Load a PDF using the PDFViewer component
  2. Extract the text of the PDF using viewer.getPageView(i).textLayer.textContentItemsStr for each page

What is the expected behavior?

What went wrong?

Link to a viewer: N/A

As we can see the dots are properly displayed in the <canvas> but they are not present in the text layer, which prevents extracting them using the method I described. It is very important for me to retrieve those dots because I'm doing natural-language processing on the PDF text to detect the sentences, but if there are no dots to close the sentences they are considered to be one big sentence instead of several ones.

Here is a question on Stack Overflow that features a code snippet for you to see it firsthand: https://stackoverflow.com/questions/62950684/pdf-js-missing-dots-in-extracted-text

Activity

  1. Snuffleupagus commented on Jul 17, 2020

    @Snuffleupagus
    Collaborator

    Extract the text of the PDF using viewer.getPageView(i).textLayer.textContentItemsStr for each page

    Please don't use that, since that's internal functionality which is not guaranteed to work (it could theoretically stop working tomorrow) and it's also not guaranteed to even be defined in general. E.g. not all pages even have a textLayer, the textLayer property is only defined after a page has finished rendering, and finally note that the actual textLayer-content is built asynchronously as well.
    The correct approach here would be use this API method:

    pdf.js/src/display/api.js

    Lines 1199 to 1204 in a604973

    /**
    * @param {getTextContentParameters} params - getTextContent parameters.
    * @returns {Promise} That is resolved a {@link TextContent}
    * object that represent the page text content.
    */
    getTextContent(params = {}) {

    as is e.g. done when searching in the default viewer:
    return this._pdfDocument
    .getPage(i + 1)
    .then(pdfPage => {
    return pdfPage.getTextContent({
    normalizeWhitespace: true,
    });
    })
    .then(
    textContent => {


    As for the text-selection/text-extraction issue at hand: This is a bug in the /ToUnicode data of the font that's used for this text, since it explicitly maps . to the space-character.

  2. TimotheAlbouy commented on Jul 17, 2020

    @TimotheAlbouy
    Author

    As for the text-selection/text-extraction issue at hand: This is a bug in the /ToUnicode data of the font that's used for this text, since it explicitly maps . to the space-character.

    Thank you for your answer. I have used pdf2htmlEX (https://github.com/coolwanglu/pdf2htmlEX) on this PDF and it worked well, the dots were correctly parsed. When I check the output of pdf2htmlEX I see that there are indeed problems with the font, but in order to work properly it drops the font for a better one, and honestly I think that the result is seamless.

    Is it possible to do that with PDF.JS as well?

  3. Snuffleupagus commented on Jul 17, 2020

    @Snuffleupagus
    Collaborator

    Simply ignoring the entire /ToUnicode data is (most likely) bound to lead to much worse text-selection behaviour in general, even if you manage to limit it to "bad" data (which you'd need to detect without false positives), hence I don't think that it's a good idea. Also, it's not always possible to tell with code if a particular /ToUnicode data is "bad", without a human doing the actual checking.

  4. TimotheAlbouy commented on Jul 17, 2020

    @TimotheAlbouy
    Author

    Do you know how the detection of bad /ToUnicode data was made for pdf2htmlEX then?

  5. Snuffleupagus commented on Jul 17, 2020

    @Snuffleupagus
    Collaborator

    Do you know how the detection of bad /ToUnicode data was made for pdf2htmlEX then?

    No, since that's a completely different project which we don't know anything about here :-)

    Generally speaking: We're currently attempting to parse as much as possible of the /ToUnicode data, which is most likely improving text-selection in some documents. Hence it's unfortunately quite unlikely that we'd accept a patch which limits this, since that'd risk regressing text-selection in other documents (and we may not have sufficient test-coverage to catch that).

  6. TimotheAlbouy commented on Jul 17, 2020

    @TimotheAlbouy
    Author

    Ok thank you for your help, I guess I cannot use PDF.js then because extracting the dots is critical for my project.

    But I don't really understand why you wouldn't accept outright a patch that fixes this issue for PDF.js the same way that pdf2htmlEX handles it. You admitted that you don't know how it works, so maybe it deals with bad /ToUnicode problems in a really clever way. I get that maybe this solution improves text-selection only for the PDF I gave but for other PDFs it performs worse, but having used both technologies I must say that pdf2htmlEX has always done a proper job for me.

    But if you want to be sure of this, you could create a dataset of several PDFs associated with their true text content. You can test this dataset using the 2 methods: the current PDF.js method and the pdf2htmlEX method. The method that gives the extracted text the closest to the true text content is the best one. This test method could be useful to check every time there is a modification to the text parsing algorithm of PDF.js, to see if it improved or not the performance.

  7. Snuffleupagus commented on Jul 17, 2020

    @Snuffleupagus
    Collaborator

    But I don't really understand why you wouldn't accept outright a patch that fixes this issue for PDF.js the same way that pdf2htmlEX handles it.

    We don't (generally) accept any patches which regress existing test-cases. If you're able to fix this issue, without regressing anything else, there's nothing stopping you from submitting a PR (although it's impossible to guarantee beforehand that it'll be accepted); please see https://github.com/mozilla/pdf.js/wiki/Contributing#4-run-lint-and-testing

    you could create a dataset of several PDFs associated with their true text content.

    We already have reference-tests of the text kind, which will thus catch text-selection regressions; my point was simply that our test-coverage may be limited and that dealing with broken PDF documents is tricky in general.

  8. TimotheAlbouy commented on Jul 18, 2020

    @TimotheAlbouy
    Author

    Sorry to drag this out, but I checked the same PDF with PDFium, the PDF renderer of Chrome, and all the characters (including the dots) were parsed perfectly. The problem is, unlike PDF.js, PDFium does not expose an API for developers. What I'm trying to say is that there is a solution to the problem of bad /ToUnicode maps, and it is not OCR. I'm just a bit disappointed that Firefox is a bit behind Chrome for this particular issue.

  9. THausherr commented on Jul 20, 2020

    @THausherr
    Contributor

    The reason that it works with other software is that the content stream of this PDF uses "/ActualText" to repair the mess done by a (probably) incomplete /ToUnicode stream in that font.

    /Span << /Lang (en-US) /MCID 106 >> BDC
      BT
        0.259 0.302 0.337 rg
        /T1_0 1 Tf
        9 0 0 9 152.5669 726.2613 Tm
        (, also known as APT15, is a threat group believed to be operating out of China) Tj
        /Span << /ActualText (\376\377\000.) >> BDC
          (.) Tj
        EMC
        [ ( Its) 20 ( ) ] TJ
      ET
    EMC
    
  10. TimotheAlbouy commented on Jul 20, 2020

    @TimotheAlbouy
    Author

    @THausherr Yes thank you, I had a similar answer when I asked how Acrobat Reader does to correctly detect all the characters. And will this feature be implemented in PDF.js in a foreseeable future?

  11. THausherr commented on Jul 20, 2020

    @THausherr
    Contributor

    @TimotheAlbouy I'm not with this project, I just hang around here, and look at weird PDFs for "my" PDF project (which has the same problem with this file).

  12. timvandermeij commented on Aug 18, 2020

    @timvandermeij
    Collaborator

    Closing in favor of #12237 which has more details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions