此PHP代码中的错误在哪里? [英] Where is an error in this PHP code?
问题描述
我有PHP代码,可将PDF文件转换为文本文件.为此,我使用作曲家安装了一个外部库,以便能够使用PDF的库.
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.
问题在于,即使我需要安装的库,系统仍然无法识别 PDF类.
The problem is that even when I required the installed library the system still not recognize the PDF Class.
库的路径:
C:\ xampp \ htdocs \ vendor \ spatie \ pdf-to-text \ src \ pdf.php
错误:
致命错误:未捕获错误:在C:\ xampp \ htdocs \ testwebsite \ OSWebProject \ test2.php:5中找不到类'Pdf':堆栈跟踪:#0 {main}抛出在C:\ xampp \ htdocs \ testwebsite中第5行的\ OSWebProject \ test2.php
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
代码:pdf类
<?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();
}
}
代码:
<?php
require_once('C:\xampp\htdocs\vendor\spatie\pdf-to-text\src\pdf.php');
$text = (new Pdf())
->setPdf('اجواء.pdf')
->text();
?>
推荐答案
每文档和源,Pdf
类在Spatie\PdfToText
名称空间内.
您将需要PHP文件顶部的use Spatie\PdfToText\Pdf;
,或者在调用它时将其引用为new Spatie\PdfToText\Pdf()
.
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.
这篇关于此PHP代码中的错误在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!