Search by

viterbit / pdfimages-extractor

markitosgv

Extract images from pdf

Package info

github.com/viterbit/pdfimages-extractor

pkg:composer/viterbit/pdfimages-extractor

Statistics

Installs: 115 570

Dependents: 0

Suggesters: 0

Stars: 2

Open Issues: 0

v0.4.0 2026-11-24 09:43 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

PHP 7.3+ and symfony/process 4.x, 5.x, 6.x, 7.x or 8.x (Symfony 8 requires PHP 8.4+).

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 third parameter to the getImages static method:

$images = Pdf::getImages('book.pdf', null, '/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 fourth parameter to the getImages static method:

$images = Pdf::getImages('book.pdf', null, 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()
;

Timeout

Some PDFs make pdfimages loop forever, writing images until the disk runs out of inodes. pdfimages is killed after 60 seconds by default; you can set another limit, in seconds, with setTimeout():

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

or as the fifth parameter to the getImages static method:

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

When the timeout command is available (GNU coreutils, present on most Linux systems), pdfimages runs under timeout -s KILL, so the system kills it even if the PHP process that launched it dies first. Without it, only the Symfony Process timeout applies, and only while PHP is still waiting.

If pdfimages does not finish in time, images() throws ExtractionTimedOut; if it fails, CouldNotExtractImages (which ExtractionTimedOut extends). In both cases the destination folder and the images already written are removed.

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.