这个问题是这个last question with different error的继续 我有一个PHP代码,可以选择一个PDF文件并将其转换为文本文件。
我在作曲家中使用了外部库 pdf-to-text 。
以下是系统显示错误:
致命错误:未捕获的Spatie \ PdfToText \ Exceptions \ CouldNotExtractText: 命令" / usr / bin / pdftotext" pdf.pdf" - "失败。退出代码: 1(常规错误)工作目录:C:\ xampp \ htdocs \ testcomposer 输出:================错误输出:================系统 找不到指定的路径。在 C:\ xampp \ htdocs \ testcomposer \ src \ Pdf.php:37堆栈跟踪:#0 C:\ XAMPP \ htdocs中\ testcomposer \ test2.php(8): 抛出Spatie \ PdfToText \ Pdf-> text()#1 {main} 第37行的C:\ xampp \ htdocs \ testcomposer \ src \ Pdf.php
testcomposer
的src
-Exceptions
-CouldNotExtractText.php
-PdfNotFound.php
-pdf.php
供应商
-composer
-autoload.php
composer.json
test2.php
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();
}
}
{
"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"
}
}
<?php
require 'vendor/autoload.php';
use Spatie\PdfToText\Pdf;
$text = (new Pdf())
->setPdf('pdf.pdf')
->text();
print_r($text);
?>
答案 0 :(得分:0)
我创建测试项目以获取pdf文件中的内容 检查这个git
https://github.com/akkapong/test-pdf
您使用的pdf-to-text包是必需的pdftotext命令,因此您需要先安装。
安装命令后会告诉您命令的路径 你可以在第6行替换文件test.php中的路径
==================运行test.php ================= $ php test.php
希望这个帮助