Install the dependence: composer require smalot/pdfparser
.
Run the following code:
<?php
require __DIR__ . '/vendor/autoload.php';
use Smalot\PdfParser\Parser; # composer require smalot/pdfparser
function parsePdf($filePath)
{
$parser = new Parser();
$pdf = $parser->parseFile($filePath);
if ($pdf === false) {
throw new Exception("Error parsing PDF");
}
$text = $pdf->getText();
return $text;
}
$filePath = 'document.pdf';
try {
$text = parsePdf($filePath);
if (!empty($text)) {
echo $text;
} else {
echo "No text found.";
}
} catch (Exception $e) {
echo "Failed to parse PDF: " . $e->getMessage();
}