问题
I have PHP code that convert PDF files to text files.For this task I installed an external library using the composer in order to be able to use the library of the PDF.
The problem is that even when I required the installed library the system still not recognize the PDF Class.
The path of the library :
C:\xampp\htdocs\vendor\spatie\pdf-to-text\src\pdf.php
Error:
Fatal error: Uncaught Error: Class 'Pdf' not found in C:\xampp\htdocs\testwebsite\OSWebProject\test2.php:5 Stack trace: #0 {main} thrown in C:\xampp\htdocs\testwebsite\OSWebProject\test2.php on line 5
code : pdf class
<?php
namespace Spatie\PdfToText;
use Spatie\PdfToText\Exceptions\CouldNotExtractText;
use Spatie\PdfToText\Exceptions\PdfNotFound;
use Symfony\Component\Process\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();
}
}
code:
<?php
require_once('C:\xampp\htdocs\vendor\spatie\pdf-to-text\src\pdf.php');
$text = (new Pdf())
->setPdf('اجواء.pdf')
->text();
?>
回答1:
Per the docs and source, the Pdf
class is within the Spatie\PdfToText
namespace.
You'll need use Spatie\PdfToText\Pdf;
at the top of your PHP file, or you can reference it as new Spatie\PdfToText\Pdf()
when you call it.
来源:https://stackoverflow.com/questions/47641523/where-is-an-error-in-this-php-code