This package provides a class to extract text from a pdf.
use Escarter\PopplerPhp\PdfToText;
use Escarter\PopplerPhp\getOutput;
echo PdfToText::getText('document.pdf','/opt/homebrew/bin/pdftotext'); //returns the text from the pdf
echo PdfSeparate::getOutput('document.pdf','/opt/homebrew/bin/pdfseparate','destination_path/page_%d.pdf'); //returns null
Behind the scenes this package leverages pdftotext and pdfseparate.You can verify if the binary installed on your system by issueing this command:
which pdftotext
which which pdfseparate
If they are installed the above commands will return the path to the binary.
To install the binary you can use this command on Ubuntu or Debian:
apt-get install poppler-utils
On a mac you can install the binary using brew
brew install poppler
If you're on RedHat or CentOS use this:
yum install poppler-utils
You can install the package via composer:
composer require escarter/poppler-wrapper-php
Extracting text from a pdf is easy.
$text = (new PdfToText())
->setPdf('document.pdf')
->text();
Or easier:
echo PdfToText::getText('document.pdf');
By default the package will assume that the pdftotext
command is located at /usr/bin/pdftotext
.
If it is located elsewhere pass its binary path to constructor
$text = (new PdfToText('/custom/path/to/pdftotext'))
->setPdf('document.pdf')
->text();
or as the second parameter to the getText
static method:
echo PdfToText::getText('document.pdf', '/custom/path/to/pdftotext');
Sometimes you may want to use pdftotext options. To do so you can set them up using the setOptions
method.
$text = (new PdfToText())
->setPdf('table.pdf')
->setOptions(['layout', 'r 96'])
->text()
;
or as the third parameter to the getText
static method:
echo PdfToText::getText('document.pdf', null, ['layout', 'opw myP1$$Word']);
Please note that successive calls to setOptions()
will overwrite options passed in during previous calls.
If you need to make multiple calls to add options (for example if you need to pass in default options when creating
the Pdf
object from a container, and then add context-specific options elsewhere), you can use the addOptions()
method:
$text = (new PdfToText())
->setPdf('table.pdf')
->setOptions(['layout', 'r 96'])
->addOptions(['f 1'])
->text()
;
Separating a single pdf file into multiple files for each page.
(new PdfSeparate())
->setPdf('document.pdf')
->setDestination('destination_path/page_%d.pdf')
->split();
Or easier:
PdfSeparate::getOutput('document.pdf','destination_path/page_%d.pdf');
Once this is executed you can check in the destination_path to see all the splitted files (page_1.pdf, page_2.pdf....)
Please see CHANGELOG for more information about what has changed recently.
composer test
Please see CONTRIBUTING for details.
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.