PHP code example of mishagp / ocrmypdf

1. Go to this page and download the library: Download mishagp/ocrmypdf library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mishagp / ocrmypdf example snippets


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();

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();

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();

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

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

use mishahawthorn\OCRmyPDF\OCRmyPDF;

$ocr = OCRmyPDF::make('document.pdf')
    ->language('eng')
    ->extractText();

$pdfPath = $ocr->run();
$text = $ocr->getText(); //Recognized plaintext

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();

use mishahawthorn\OCRmyPDF\OCRmyPDF;

OCRmyPDF::make('document.pdf')
    ->setTimeout(120) //seconds
    ->run();

use mishahawthorn\OCRmyPDF\OCRmyPDF;

OCRmyPDF::make('document.pdf')
    ->setLogger($psr3Logger)
    ->run();