mishahawthorn/ocrmypdf
Composer 安装命令:
composer require mishahawthorn/ocrmypdf
包简介
A simple PHP wrapper for OCRmyPDF
README 文档
README
A simple PHP wrapper for OCRmyPDF
Installation
Via Composer:
$ composer require mishahawthorn/ocrmypdf
This library depends on OCRmyPDF. Please see the GitHub repository for instructions on how to install OCRmyPDF on your platform.
Usage
Basic example
use mishahawthorn\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
Define invocation parameters for ocrmypdf. See ocrmypdf --help for a list of available parameters.
Important
Parameters configured via setParam will override any other parameters or configurations set otherwise.
use mishahawthorn\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
Pass image/PDF data loaded in memory into ocrmypdf directly via stdin.
use mishahawthorn\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
Specify a writable path where ocrmypdf should generate output PDF.
use mishahawthorn\OCRmyPDF\OCRmyPDF; OCRmyPDF::make('document.pdf') ->setOutputPDFPath('/outputDir/ocr_document.pdf') ->run();
setExecutable
Define a custom location of the ocrmypdf executable, if by any reason it is not present in the $PATH.
use mishahawthorn\OCRmyPDF\OCRmyPDF; OCRmyPDF::make('document.pdf') ->setExecutable('/path/to/ocrmypdf') ->run();
extractText / getText
Write the recognized plaintext to a sidecar file and read it back after run(). Call extractText() with no
argument to use a temporary file that is read and discarded, or pass a path to keep the text file.
use mishahawthorn\OCRmyPDF\OCRmyPDF; $ocr = OCRmyPDF::make('document.pdf') ->language('eng') ->extractText(); $pdfPath = $ocr->run(); $text = $ocr->getText(); //Recognized plaintext
Convenience setters
Thin, type-safe wrappers over the most common ocrmypdf parameters. Each is equivalent to the matching
setParam call.
use mishahawthorn\OCRmyPDF\OCRmyPDF; OCRmyPDF::make('document.pdf') ->language('eng', 'deu') // -l eng+deu ->deskew() // --deskew ->rotatePages() // --rotate-pages ->clean() // --clean ->removeBackground() // --remove-background ->optimize(3) // --optimize 3 (0-3) ->skipText() // --skip-text (or ->forceOcr() / ->redoOcr()) ->setThreadLimit(4) // --jobs 4 ->setTempDir('/var/tmp') ->run();
Boolean setters accept false to remove the flag, e.g. ->deskew(false).
setTimeout
Terminate the ocrmypdf process if it runs longer than the given number of seconds. A
ProcessTimeoutException is thrown when the limit is exceeded.
use mishahawthorn\OCRmyPDF\OCRmyPDF; OCRmyPDF::make('document.pdf') ->setTimeout(120) //seconds ->run();
setLogger
Attach any PSR-3 logger to receive the generated command, stderr output and any failures.
use mishahawthorn\OCRmyPDF\OCRmyPDF; OCRmyPDF::make('document.pdf') ->setLogger($psr3Logger) ->run();
License
ocrmypdf-php is released under the MIT License.
Credits
Development of ocrmypdf-php is based on the tesseract-ocr-for-php PHP wrapper library for tesseract
developed by thiagoalessio and associated contributors.
mishahawthorn/ocrmypdf 适用场景与选型建议
mishahawthorn/ocrmypdf 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.66k 次下载、GitHub Stars 达 9, 最近一次更新时间为 2025 年 10 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「wrapper」 「OCR」 「ocrmypdf」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mishahawthorn/ocrmypdf 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mishahawthorn/ocrmypdf 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mishahawthorn/ocrmypdf 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
LinkedIn API PHP SDK with OAuth 2.0 & CSRF support. Can be used for social sign in or sharing on LinkedIn. Examples. Documentation.
LinkedIn API PHP SDK with OAuth 2.0 & CSRF support. Can be used for social sign in or sharing on LinkedIn. Examples. Documentation.
The Best Image Ocr SDK For BAT.
The Best Image Ocr SDK For BAT.
腾讯 OCR SDK
gv config package
统计信息
- 总下载量: 1.66k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 10
- 点击次数: 8
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-10-08