How to read PDF text in PHP
12 June 2026 (Updated 12 June 2026)
Install the smalot/pdfparser Composer package:
composer require smalot/pdfparser
Parse a PDF file and extract its text:
<?php
require __DIR__ . '/vendor/autoload.php';
use Smalot\PdfParser\Parser;
$parser = new Parser();
$pdf = $parser->parseFile(__DIR__ . '/example.pdf');
$text = $pdf->getText();
echo $text;
# If you want to extract text page by page:
foreach ($pdf->getPages() as $pageNumber => $page) {
echo 'Page ' . ($pageNumber + 1) . PHP_EOL;
echo $page->getText() . PHP_EOL;
}
Tagged:
PHP recipes