viterbit/pdfimages-extractor

Extract images from pdf

v0.2.1 2024-08-19 15:56 UTC

This package is auto-updated.

Last update: 2024-12-19 16:37:51 UTC


README

This package provides a class to extract images from a pdf.

use Viterbit\PdfImagesExtractor\Pdf;

$images = Pdf::getImages('book.pdf'); //returns a FilesystemIterator

Requirements

Behind the scenes this package leverages pdfimages. You can verify if the binary installed on your system by issuing this command:

which pdfimages

If it is installed it 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

Installation

You can install the package via composer:

composer viterbit/pdfimages-extractor

Usage

Getting images from a pdf is easy.

$images = (new Pdf())
    ->setPdf('book.pdf')
    ->images();

Or easier:

$images = Pdf::getImages('book.pdf');

By default the package will assume that the pdftoimages command is located at /usr/bin/pdftoimages. If it is located elsewhere pass its binary path to constructor

$images = (new Pdf('/custom/path/to/pdftoimages'))
    ->setPdf('book.pdf')
    ->images();

or as the second parameter to the getImages static method:

echo Pdf::getText('book.pdf', '/custom/path/to/pdftoimages');

Sometimes you may want to use pdfimages options. To do so you can set them up using the setOptions method.

$images = (new Pdf())
    ->setPdf('book.pdf')
    ->setOptions(['j', 'f 1'])
    ->images()
;

or as the third parameter to the getImages static method:

$images = Pdf::getImages('book.pdf', null, ['j', 'f 1']);

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:

$images = (new Pdf())
    ->setPdf('book.pdf')
    ->addOptions(['f 1'])
    ->images()
;

Change log

Please see CHANGELOG for more information about what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email freek@spatie.be instead of using the issue tracker. design agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.

License

The MIT License (MIT). Please see License File for more information.