mishagp / ocrmypdf

OCRmyPDF 的简单 PHP 封装

v0.3.0 2023-12-19 02:36 UTC

This package is auto-updated.

Last update: 2024-08-27 01:32:15 UTC


README

OCRmyPDF 的简单 PHP 封装

Latest Stable Version Total Downloads License PHP Version Require codecov

安装

通过 Composer

$ composer require mishagp/ocrmypdf

此库依赖于 OCRmyPDF 请参阅 GitHub 存储库中的说明,了解如何在您的平台上安装 OCRmyPDF。

用法

基本示例

use mishagp\OCRmyPDF\OCRmyPDF;

//Return file path of outputted, OCRed PDF
echo OCRmyPDF::make('document.pdf')->run();

//Return file contents of outputted, OCRed PDF
echo OCRmyPDF::make('scannedImage.png')->setOutputPDFPath(null)->run();

API

setParam

ocrmypdf 定义调用参数。请参阅 ocrmypdf --help 以获取可用参数列表。

重要

通过 setParam 配置的参数将覆盖任何其他参数或配置。

use mishagp\OCRmyPDF\OCRmyPDF;

//Passing a single parameter with a value
OCRmyPDF::make('document_zh-CN.pdf')
    ->setParam('-l', 'chi_sim')
    ->run();

//Passing a single parameter without a value
OCRmyPDF::make('document_withBackground.pdf')
    ->setParam('--remove-background')
    ->run();

//Passing multiple parameters
OCRmyPDF::make('document_withoutAttribution.pdf')
    ->setParam('--title', 'Lorem Ipsum')
    ->setParam('--keywords', 'Lorem,Ipsum,dolor,sit,amet')
    ->run();

setInputData

通过 stdin 直接将内存中加载的图像/PDF 数据传递给 ocrmypdf

use mishagp\OCRmyPDF\OCRmyPDF;

//Using Imagick
$data = $img->getImageBlob();
$size = $img->getImageLength();

//Using GD
ob_start();
imagepng($img, null, 0);
$size = ob_get_length();
$data = ob_get_clean();

OCRmyPDF::make()
    ->setInputData($data, $size)
    ->run();

setOutputPDFPath

指定 ocrmypdf 应生成输出 PDF 的可写路径。

use mishagp\OCRmyPDF\OCRmyPDF;
OCRmyPDF::make('document.pdf')
    ->setOutputPDFPath('/outputDir/ocr_document.pdf')
    ->run();

setExecutable

如果出于任何原因,ocrmypdf 可执行文件不在 $PATH 中,请定义其自定义位置。

use mishagp\OCRmyPDF\OCRmyPDF;
OCRmyPDF::make('document.pdf')
    ->setExecutable('/path/to/ocrmypdf')
    ->run();

许可证

ocrmypdf-php 在 AGPL-3.0 许可证 下发布。

致谢

ocrmypdf-php 的发展基于 tesseract-ocr-for-php PHP 封装库,用于 tesseract,由 thiagoalessio 开发并与其他贡献者合作。