Skip to content

Textract grafting strips visible text on some PDFs #336

Closed

Description

@duckduckgrayduck

What I did:
I'm adding the ability to optionally force OCR a document after uploading them via URL in the Python wrapper.

urls= ["https://www.chicago.gov/content/dam/city/depts/dcd/tif/24reports/T_063_CanalCongressAR24.pdf", "https://www.chicago.gov/content/dam/city/depts/dcd/tif/24reports/T_072_24thMichiganAR24.pdf"]
uploaded_docs = client.documents.upload_urls(urls, force_ocr=True, ocr_engine="textract")

https://www.documentcloud.org/documents/26105901-t_063_canalcongressar24/

This left the document in a failed state, which could probably be handled better (like present a working version of the PDF)

Activity

  1. added this to the Processing milestone on Oct 6, 2025
  2. allanlasser commented on Oct 6, 2025

    @allanlasser
    Member

    If we track processing jobs in the database, we could fall back to the result of the last successful job (ex. OCR job fails, fall back to upload job result).

    We'll need to report these errors more clearly to users, so they know what went wrong and what steps they need to take next to get the result they want.

    Right now, we show the error state but don't do any user-friendly fixes for the error message.

  3. eyeseast commented on Sep 15, 2026

    @eyeseast
    Contributor

    Ran into this in a demo: https://www.documentcloud.org/documents/28623996-pressure-cooking-demo/

    We should disable Textract in the UI until we can solve this. We can keep testing using the API.

  4. duckduckgrayduck commented on Nov 3, 2026

    @duckduckgrayduck
    ContributorAuthor

    I spent some time debugging this as the issue intersects with a push to disable grafting for now.
    This issue seems to occur on digitally created PDFs such as this one:
    https://www.documentcloud.org/documents/28865374-requests20to20follow20up20-20sheet1/

    This is a document that was originally an excel spreadsheet that I exported as a PDF in excel.

    This is what it is supposed to look like, before Textract -> Force OCR breaks it:
    https://www.documentcloud.org/documents/28865400-0001-requests20to20follow20up20-20sheet1/

    I created a project on production that I will replicate on staging that is comprised of various edge cases:
    https://www.documentcloud.org/projects/226005-test-ocr/

    These kinds of documents seem to work fine:

    It breaks on digitally created PDFs, the kind exported from Word, Excel or a reporting system, or filled-in forms like the TIF report originally noted in the issue where the visible text is real text objects.

  5. changed the title [-]Error in grafting ocr'd text back into PDF[/-] [+]Force OCR with Textract strips visible text on some PDFs[/+] on Nov 3, 2026
  6. duckduckgrayduck commented on Nov 3, 2026

    @duckduckgrayduck
    ContributorAuthor

    I had Claude go look at the processing code and it flagged this:

    Cause: graft_ocr_in_pdf in documents/processing/info_and_image/main.py applies a full-page text redaction to every page before overlaying the Textract layer. This is fine when existing text is invisible, but it deletes real text. The Django graft path guards against this with _check_visible_text; the processing path doesn’t. The original PDF is overwritten, so affected documents need re-uploading.

  7. duckduckgrayduck commented on Nov 3, 2026

    @duckduckgrayduck
    ContributorAuthor

    I will add an environment variable in Lambda to turn grafting off completely and also add this _check_visible_text path that the Django grafting path does. That will allow me to see if this fixes this on staging.

  8. duckduckgrayduck commented on Nov 3, 2026

    @duckduckgrayduck
    ContributorAuthor

    Checking if the page had visible text and skipping those pages did fix this issue. It means those pages with visible text don't get the text grafted back in, but it at least doesn't destroy the PDF. It is a path to remedy this before we build out time to properly address this.

  9. duckduckgrayduck commented on Nov 3, 2026

    @duckduckgrayduck
    ContributorAuthor

    The longer term fix for this I think is to treat the pages differently depending on if they have underlying text or not.

    • No visible text: keep the current behavior. Redact the hidden layer and overlay Textract. This already works.

    • Visible text: Don’t redact at all. Overlay only the Textract words that don’t overlap existing visible text. The real text stays untouched, and OCR fills in what the PDF’s text layer lacks, such as text inside a pasted screenshot, a scanned signature page or a chart image. This requires some level of heuristics. We need a deduplication filter that drops Textract words that overlap with existing visible text so that we don't ruin PDFs. This requires a lot of testing, so for now we must just disable grafting on pages with visible text.

    I've captured this in this issue: #531

    For now, the data destruction concern from this issue is closed in pending PR #529

  10. changed the title [-]Force OCR with Textract strips visible text on some PDFs[/-] [+]Textract grafting strips visible text on some PDFs[/+] on Nov 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions