Important
This client is deprecated and is replaced by the Document Cloud PHP Client
This repository contains a PHP 8.2+ library for converting PDF files to images using the pdf2img service.
The library is available on Packagist. The recommended way to install it is via Composer:
composer require codeinc/pdf2img-client
This client requires a running instance of the pdf2img service. The service can be run locally using Docker or deployed to a server.
Base example:
use CodeInc\Pdf2ImgClient\Pdf2ImgClient;
use CodeInc\Pdf2ImgClient\Exception;
$apiBaseUri = 'http://localhost:3000/';
$localPdfPath = '/path/to/local/file.pdf';
try {
$client = new Pdf2ImgClient($apiBaseUri);
// convert
$image = $client->convert(
$client->createStreamFromFile($localPdfPath)
);
// display the image
header('Content-Type: image/webp');
echo (string)$image;
}
catch (Exception $e) {
// handle exception
}
With options:
use CodeInc\Pdf2ImgClient\Pdf2ImgClient;
use CodeInc\Pdf2ImgClient\ConvertOptions;
$apiBaseUri = 'http://localhost:3000/';
$localPdfPath = '/path/to/local/file.pdf';
$destinationPath = '/path/to/destination/file.jpg';
$convertOption = new ConvertOptions(
format: 'jpg',
page: 3,
density: 300,
height: 800,
width: 800,
background: 'red',
quality: 90,
);
try {
$client = new Pdf2ImgClient($apiBaseUri);
// convert
$image = $client->convertLocalFile(
$client->createStreamFromFile($localPdfPath),
$convertOption
);
// saves the image to a file
$client->saveStreamToFile($image, $destinationPath);
}
catch (Exception $e) {
// handle exception
}
The library is published under the MIT license (see LICENSE
file).