Where is an error in this PHP code?

孤街浪徒 提交于 2019-12-13 11:27:47

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!