nilgems / laravel-textract
一个用于从DOC、XL、图片、PDF等文件中提取文本的Laravel包。我受“npm textract”的启发开发了此包。
v1.1
2022-06-13 08:35 UTC
Requires
- php: ^7.4|^8.0
- ext-fileinfo: *
- ext-gd: *
- ext-xml: *
- ext-zip: *
- html2text/html2text: ^4.3
- laravel/framework: ^8.0|^9.0
- lywzx/php-epub: ^0.1.2
- phpoffice/phpspreadsheet: ^1.23
- phpoffice/phpword: ^0.18
- stechstudio/laravel-php-cs-fixer: ^3.1
- symfony/process: ^6.1
- thiagoalessio/tesseract_ocr: ^2.12
Requires (Dev)
- friendsofphp/php-cs-fixer: ^v3.8.0
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-09-10 23:21:48 UTC
README
Laravel Textract
一个用于从DOC、Excel、图片、PDF等文件中提取文本的Laravel包。
版本和兼容性
- 需要Laravel 10或更高版本。
- 需要Php 8.2或更高版本
支持文件格式
目前支持以下文件格式。您需要将适当的扩展安装到服务器上才能处理以下扩展相关的文件。在执行之前,包将检查文件内容的MIME类型。
- HTML
- TEXT
- DOC
- DOCX
- XLS、XLSX、XLSM、XLTX、XLTM、XLT
- CSV
- 图片
- jpeg
- png
- gif
- ODT
- ODS
- RTF
- PPTX(新功能)
我们正在努力使这个Laravel插件变得有用。如果您发现任何问题,请在讨论区发帖。
安装
composer require nilgems/laravel-textract
安装完成后,您可以执行以下操作
# Run the extractor
$output = Textract::run('/path/to/file.extension');
# Display the extracted text
echo $output->text;
# Display the extracted text word count
echo $output->word_count;
# Display the extracted text with direct string conversion
echo (string) $output;
运行提取器以处理任何支持的文件
Textract::run(string $file_path, [string $job_id],[TesseractOcrOptions $extra_data]);
配置
- 您可以在您的Laravel项目的
config
文件夹下的app.php
中添加provider
。这是可选的,包会自动在您的应用程序中加载服务提供者。'providers' => [ ... Nilgems\PhpTextract\Providers\ServiceProvider, ... ]
- 您可以在您的Laravel项目的
config
文件夹下的app.php
中添加alias
。这是可选的,包会自动在您的应用程序中加载facade
。'aliases' => [ ... 'Textract' => Nilgems\PhpTextract\Textract::class, ... ]
- 要发布配置文件,请运行
php artisan vendor:publish --tag=textract
示例
示例 1
您可以从支持的文件格式中提取文本。
建议使用Laravel Queue Job来提高性能。
在php
中,存在在php.ini
文件中定义的限制,包括max_execution_time
和memory_limit
选项。如果文件很大,超过限制时可能会被强制终止。您可以使用queue - database/redis
或Laravel horizon
在后台运行进程。
........
Route::get('/textract', function(){
return Textract::run('/path/to/image/example.png');
});
........
示例 2
如果您需要指定图像文件中的语言以获得更好的图像提取输出。
........
Route::get('/textract', function(){
return Textract::run('/path/to/image/example.png', null, [
'lang' => ['eng', 'jpn', 'spa']
]);
});
........
依赖项
- 要启用图像提取功能,您需要安装Tesseract OCR
- 要启用PDF提取功能,您需要安装pdftotext
- 为了正常工作,您的服务器必须安装以下php扩展 -
- ext-fileinfo
- ext-zip
- ext-gd或ext-imagick
- ext-xml
Tesseract OCR 安装
Ubuntu
- 更新系统:
sudo apt update
- 将Tesseract OCR 5 PPA添加到您的系统:
sudo add-apt-repository ppa:alex-p/tesseract-ocr-devel
- 在Ubuntu 20.04 | 18.04上安装Tesseract:
sudo apt install -y tesseract-ocr
- 安装完成后,更新您的系统:
sudo apt update
- 验证安装:
tesseract --version
Windows
- 有许多方法可以在您的系统上安装Tesseract OCR,但如果您只是想快速启动,我建议使用Capture2Text包和Chocolatey。
- Choco安装:
choco install capture2text --version 5.0
注意:最近版本的Capture2Text停止提供tesseract
二进制文件
PdfToText 安装
Ubuntu
- 更新系统:
sudo apt update
- 在Ubuntu 20.04 | 18.04上安装PdfToText:
sudo apt-get install poppler-utils
- 验证安装:
pdftotext -v
Windows
- 很抱歉,通过poppler和poppler提供的
pdftotext
目前尚不可用Windows。但您可以通过Windows Linux子系统WLS安装并使用库。或者,您可以在项目中安装Laravel Homestead,然后通过vagrant虚拟化在Ubuntu虚拟服务器中运行项目。