I've got a PHP code that selects a PDF file and converts it to a text file.
I'm using an external library pdf-to-text with the composer.
Following is the system display error:
Fatal error: Uncaught Error: Class 'Pdf' not found in C:\xampp\htdocs\testcomposer\test2.php:6 Stack trace: #0 {main} thrown in C:\xampp\htdocs\testcomposer\test2.php on line 6
Project Structure :
testcomposer
app
-Spatie -pdftotext -src -Exceptions -CouldNotExtractText.php -PdfNotFound.php -pdf.php -symfony
vendor
-composer -autoload.php
- composer.json
test2.php
xxx.pdf
codes
pdf.php
<?php
namespace Spatie\pdftotext\src;
use Spatie\pdftotext\src\Exceptions;
use Symfony\process;
class Pdf
{
protected $pdf;
protected $binPath;
public function __construct(string $binPath = null)
{
$this->binPath = $binPath ?? '/usr/bin/pdftotext';
}
public function setPdf(string $pdf) : Pdf
{
if (!file_exists($pdf)) {
throw new PdfNotFound("could not find pdf {$pdf}");
}
$this->pdf = $pdf;
return $this;
}
public function text() : string
{
$process = new Process("{$this->binPath} " . escapeshellarg($this->pdf) . " -");
$process->run();
if (!$process->isSuccessful()) {
throw new CouldNotExtractText($process);
}
return trim($process->getOutput(), " \t\n\r\0\x0B\x0C");
}
public static function getText(string $pdf, string $binPath = null) : string
{
return (new static($binPath))
->setPdf($pdf)
->text();
}
}
composer.json
{
"name": "spatie/pdf-to-text",
"description": "Extract text from a pdf",
"keywords": [
"spatie",
"pdftotext"
],
"homepage": "https://github.com/spatie/pdf-to-text",
"license": "MIT",
"authors": [
{
"name": "Freek Van der Herten",
"email": "freek@spatie.be",
"homepage": "https://spatie.be",
"role": "Developer"
}
],
"require": {
"php" : "^7.0",
"symfony/process": "^3.0"
},
"require-dev": {
"phpunit/phpunit" : "^6.4"
},
"autoload": {
"psr-4": {
"src\\": "app/Spatie/pdftotext/src"
}
},
"autoload-dev": {
"psr-4": {
"Spatie\\pdftotext\\Test\\": "tests"
}
},
"scripts": {
"test": "phpunit"
}
}
test2.php
<?php
require __DIR__ . '/vendor/autoload.php';
use Spatie\pdftotext\src;
$text = (new Pdf())
->setPdf('اجواء.pdf')
->text();
?>