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.
Parameters
| Name | Type | Description |
|---|---|---|
text | string | The text of the PDF’s pages, separated by blank lines; empty for a scanned PDF without a text layer. |
attachment | WP_Post | The PDF’s attachment post. |
path | string | The path of the PDF file on the server. |
Returns
string: The text to index.
Example
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.