Extraction · PHP
Extract text from a PDF in PHP
Pull the text out of any PDF from PHP. rust-pdf walks the content stream, maps shown glyph codes back to Unicode through each font's ToUnicode map, and infers spaces and line breaks, including two-byte Type0 fonts and CJK.
Last updated: 2026-06-29
Why PHP needs this
Search, full-text indexing, data extraction and compliance review all start from one question: what does this PDF say? Getting clean text back out is deceptively hard, because the codes in a content stream are font-specific, not Unicode.
rust-pdf walks the content stream, maps each shown code back to Unicode through the font's ToUnicode CMap (including two-byte Type0 fonts), and infers spaces from large negative adjustments and line breaks from text positioning. Japanese, Greek and Arabic come back as the text a human reads.
A queued Laravel job can call Pdf::extractText and stream the result straight into Elasticsearch or a database, all in-process. No pdftotext binary and no Python microservice to keep alive. composer require installs the binding, ext-ffi connects it to the core, and image-only scans are the one case that still needs OCR first.
- Maps glyph codes to Unicode via each font's ToUnicode map, including two-byte Type0 and CJK.
- Infers spaces from large negative adjustments and line breaks from text positioning.
- Pull raster images out too: JPEGs verbatim as .jpg, everything else as .png.
Extract text in PHP with rust-pdf
Install via Composer, then call the same idiomatic API every binding shares. The extraction snippet below is real PHP from the reference docs.
composer require rust-pdf/rustpdf
use RustPdf\Pdf;
$data = file_get_contents('report.pdf');
echo Pdf::extractText($data);
$n = Pdf::extractImagesToDir($data, 'out_images/'); // returns how many were written
echo "wrote $n image(s)\n";
Extraction is part of the free PHP tier, no license token required.
The complete extraction API is in the PHP documentation.
Text extraction in PHP: FAQ
Does it extract Unicode and CJK text in PHP?
Yes. Each shown code is mapped back to Unicode through the font's ToUnicode CMap, including two-byte Type0 fonts, so Japanese, Greek, Arabic and other scripts come back correctly.
Can it extract scanned PDFs?
No. Extraction reads the text layer of a PDF. A scanned document is an image with no text layer, which needs OCR first. For PDFs that contain real text, extraction is exact.
Is text extraction free?
Yes. Text and image extraction are part of the free tier in PHP. No license token is required.
Can I index PDF text in a Laravel queue without a Python service?
Yes. Pdf::extractText returns a UTF-8 string in-process, so a queued job can feed Elasticsearch or a database full-text index directly. There is no pdftotext binary or Python microservice to deploy and keep alive, though scanned, image-only PDFs still need OCR upstream before any text exists to read.
Turn your PDFs into searchable data with PHP
One Rust core, the same extraction in every language. Text and image extraction are free; you only license corporate features like PDF/A, signing and encryption.