---
name: kimaai_embedding_pdf_extracted_text
type: filter
since: 1.4.9
tier: premium
feature: embeddings
area: embedding-search
summary: Changes the text read from a PDF before it is indexed, for example to supply the text of a scanned PDF.
parameters:
  - name: text
    type: string
    description: The text of the PDF's pages, separated by blank lines; empty for a scanned PDF without a text layer.
  - name: attachment
    type: WP_Post
    description: The PDF's attachment post.
  - name: path
    type: string
    description: The path of the PDF file on the server.
returns:
  type: string
  description: The text to index.
---

# kimaai_embedding_pdf_extracted_text

## When it fires

In a sync of the knowledge base, for each PDF, after the text of its pages was read and before it is cleaned up and split into chunks. Encrypted PDFs and PDFs above `kimaai_embedding_pdf_max_bytes` stop before it.

KimaAI does no OCR. A scanned PDF has no text layer, so `$text` is empty, and if it is still empty after the filter the sync records the error "PDF does not contain extractable text" for it. A callback can supply the text from an OCR service or from a text version you keep of the document. Line breaks and runs of spaces are tidied after the filter.

## Example

```php
add_filter(
  'kimaai_embedding_pdf_extracted_text',
  function (string $text, WP_Post $attachment, string $path): string {
    // Scanned brochures have no text layer: the OCR run left a .txt file next to each of them.
    $ocr_file = preg_replace('/\.pdf$/i', '.txt', $path);
    if (trim($text) === '' && is_readable($ocr_file)) {
      return (string) file_get_contents($ocr_file);
    }

    return $text;
  },
  10,
  3
);
```

## Related

- `kimaai_embedding_pdf_max_bytes`: the largest PDF that is read.
- `kimaai_embedding_extra_text`: adds text to any post, PDFs included.
