Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
727 views
in Technique[技术] by (71.8m points)

pdf - Where is an error in this PHP code?

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:xampphtdocsvendorspatiepdf-to-textsrcpdf.php

Error:

Fatal error: Uncaught Error: Class 'Pdf' not found in C:xampphtdocs estwebsiteOSWebProject est2.php:5 Stack trace: #0 {main} thrown in C:xampphtdocs estwebsiteOSWebProject est2.php on line 5

code : pdf class

<?php

namespace SpatiePdfToText;

use SpatiePdfToTextExceptionsCouldNotExtractText;
use SpatiePdfToTextExceptionsPdfNotFound;
use SymfonyComponentProcessProcess;

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(), " 	

x0Bx0C");
    }

    public static function getText(string $pdf, string $binPath = null) : string
    {
        return (new static($binPath))
            ->setPdf($pdf)
            ->text();
    }
}

code:

<?php

require_once('C:xampphtdocsvendorspatiepdf-to-textsrcpdf.php');

$text = (new Pdf())
    ->setPdf('?????.pdf')
    ->text();
?>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Per the docs and source, the Pdf class is within the SpatiePdfToText namespace.

You'll need use SpatiePdfToTextPdf; at the top of your PHP file, or you can reference it as new SpatiePdfToTextPdf() when you call it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...