codeinc/pdf2img-client

This package is abandoned and no longer maintained. The author suggests using the codeinc/document-cloud-client package instead.

A PHP client for the pdf2img service

v1.10 2024-12-10 18:05 UTC

This package is auto-updated.

Last update: 2024-12-11 01:39:31 UTC


README

Code Inc. Tests

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.

Installation

The library is available on Packagist. The recommended way to install it is via Composer:

composer require codeinc/pdf2img-client

Usage

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
}

License

The library is published under the MIT license (see LICENSE file).