PHP SDK: Document: text, pages, and tables
$ctx->documents()->fromUrl(), fromBytes(), and $ctx->mail()->document() return this object. The examples first create $document from a small CSV file.
text
text(): string
The document text, including available Office headers, footers, and notes. PDF text stays divided by page. DOCX structure and formatting are also available through blocks().
$document = $ctx->documents()->fromBytes("SKU;Stock\nTEST-001;17", 'report.csv');
$text = $document->text();
format
format(): string
The detected format, such as xlsx, docx, pdf, or csv.
$document = $ctx->documents()->fromBytes('Stock: 17', 'report.txt');
$ctx->log($document->format());
tables
tables(): array
Excel sheets or Word tables, each with name and rows. Excel numbers remain numbers. Supported XLSX formulas are calculated locally with xlcalculator. External references and macros are never executed. rows() and cell() return the calculated result, formula() returns the original expression, and cachedValue() returns the value saved by the document author. Use error() and calculation() to inspect calculation problems.
$document = $ctx->documents()->fromBytes("SKU;Stock\nTEST-001;17", 'report.csv');
$tables = $document->tables();
rows
rows(int|string $sheet = 0): array
Rows from a sheet selected by index or name, including the first header row. A missing sheet returns an empty array. PDF parsing returns page text; it does not reconstruct PDF tables.
$document = $ctx->documents()->fromBytes("SKU;Stock\nTEST-001;17", 'report.csv');
foreach ($document->rows() as $index => $row) {
if ($index === 0) continue;
$ctx->emit(['sku' => $row[0], 'stock' => (int) $row[1]]);
}
pages
pages(): array
PDF pages with number, text, and ocr. Scanned pages are recognized locally in Russian and English. One parse is limited to five image pages and 100 text pages.
$document = $ctx->documents()->fromUrl('https://example.com/files/report.pdf');
foreach ($document->pages() as $page) {
$ctx->log('Page '.$page['number'].': '.$page['text']);
}
links
links(): array
Links found in the document. The document service does not follow them or download external images. If needed, explicitly request an allowed link through the network SDK.
$document = $ctx->documents()->fromBytes('Stock report', 'report.txt');
$links = $document->links();
complete
complete(): bool
Returns false if an extraction limit was reached. Check it before saving what should be a complete report. Limits: 20 sheets or tables, 5,000 rows, 100 columns, 50,000 cells in total, and 1 MiB of text.
$document = $ctx->documents()->fromBytes('Stock report', 'report.txt');
if (!$document->complete()) $ctx->fail('Document is too large: split it into smaller parts');
warnings
warnings(): array
Codes describing parsing limits or special cases: cached formula values, older Office formats, images in Word or Excel, and OCR limits. Extraction from older DOC/XLS files is limited; embedded objects are not executed.
$document = $ctx->documents()->fromBytes('Stock report', 'report.txt');
foreach ($document->warnings() as $warning) $ctx->log($warning);
sheetNames
sheetNames(): array
Names of extracted sheets or tables in their original order. Empty for PDFs and images.
$document = $ctx->documents()->fromBytes("SKU;Stock\nTEST-001;17", 'report.csv');
$names = $document->sheetNames();
sheet
sheet(int|string $sheet = 0): Worksheet
Select a sheet by zero-based index or exact name. Returns a Worksheet. A missing sheet raises a clear error; rows() keeps its earlier behavior and returns an empty array. Worksheet methods run locally without reading the file again.
$document = $ctx->documents()->fromBytes("SKU;Stock\nTEST-001;17", 'report.csv');
$sheet = $document->sheet(0);
$stock = $sheet->cell('B2');
page
page(int $number): string
Text from a PDF or raster-image page, using a one-based page number. A missing page returns an empty string; an invalid number raises an error. Use text() for Word documents.
$document = $ctx->documents()->fromBytes("SKU;Stock\nTEST-001;17", 'report.csv');
$pdf = $ctx->documents()->fromUrl('https://example.com/report.pdf');
$text = $pdf->page(1);
matches
matches(string $pattern, int $limit = 100): array
Search the extracted text locally with a regular expression. Returns matches and captured groups, up to limit (1–1000). Expressions can be up to 2,048 bytes; an invalid expression raises an error. Values are not sent to an AI model.
$document = $ctx->documents()->fromBytes("SKU;Stock\nTEST-001;17", 'report.csv');
$text = $ctx->documents()->fromBytes('Total: 1250', 'report.txt');
$matches = $text->matches('/Total: (\d+)/');
chunks
chunks(int $characters = 4000, int $overlap = 200): array
Split text into Unicode-character fragments. characters must be 100–16,000, and overlap must be between 0 and characters - 1. This method does not send fragments to external services. Do not pass private fragments to analyze() or transform() without a separate data-processing policy.
$document = $ctx->documents()->fromBytes("SKU;Stock\nTEST-001;17", 'report.csv');
$chunks = $document->chunks(4000, 200);
blocks
blocks(): array
DOCX paragraphs and table references in document order. A paragraph includes kind=paragraph, heading level, align, indent, before, after, marker, depth, and runs. Each run has text, bold, italic, underline, and point size. A kind=table block contains the table's index in tables(). Other formats return an empty array. Values are not sent to an external model.
$document = $ctx->documents()->fromUrl('https://example.com/report.docx');
foreach ($document->blocks() as $block) {
if ($block['kind'] === 'paragraph') {
foreach ($block['runs'] as $run) $ctx->log($run['text']);
}
}