Skip to content

Latest commit

 

History

35 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SetaPDF-ImageExtractor

This package is a proof of concept to extract images from PDFs in pure PHP with the SetaPDF-Core component and an image render module. Currently, not all kinds of images are supported. This is also very dependent on your used image renderer library. GD, Imagick and vips (via php-vips, which requires ext-ffi and libvips) are currently implemented. We recommend you to use vips or Imagick as they're more feature complete as GD but may be a little harder to install.

Requirements

This package is developed and tested on PHP >= 8.1.

You'll need an active license of the SetaPDF-Core component.

Requirements of the SetaPDF-Core component can be found here.

Usage

This example here extracts all images from the first page of the pdf and converts them into PNGs:

use setasign\SetaPDF\ImageExtractor\ImageExtractor;

$document = SetaPDF_Core_Document::loadByFilename('your-pdf-file.pdf');
$images = ImageExtractor::getImagesByPageNo($document, 1);
foreach ($images as $imageData) {
    // toImage() returns the image as a PNG string.
    // Use ImageExtractor::IMAGE_RENDERER_GD or ImageExtractor::IMAGE_RENDERER_VIPS for the other renderers.
    $image = ImageExtractor::toImage($imageData, ImageExtractor::IMAGE_RENDERER_IMAGICK);
    echo '<img src="data:image/png;base64,' . base64_encode($image) . '"/>';
}

If you need the instance of the renderer library (e.g. \Imagick, a GD image or \Jcupitt\Vips\Image) instead of a PNG string, you can process the image stream directly:

// for inline images ($imageData['type'] === 'inlineImage') use $imageData['stream'] instead
$stream = \SetaPDF_Core_Type_Stream::ensureType($imageData['xObject']->getIndirectObject());
$image = ImageExtractor::processStream($stream, ImageExtractor::IMAGE_RENDERER_IMAGICK);
$imagick = $image->getResult();

Note that CMYK images are converted to sRGB by the Imagick renderer, while the vips renderer keeps the original color space in getResult(). Use VipsImage::toSrgb() to convert them (this is also done by toImage()).

The vips renderer requires the jcupitt/vips package, the FFI extension and libvips.

License

This package is open-sourced software licensed under the MIT license.

About

PoC to extract images from PDFs with SetaPDF-Core

Topics

Resources

Stars

2 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages