codeinc/pdf2img-client

A PHP client for the pdf2img service

v1.5 2024-02-24 01:15 UTC

This package is auto-updated.

Last update: 2024-10-24 02:38:41 UTC


README

This repository contains a PHP 8.2+ library for converting PDF files to images using the pdf2img service.

Installation

The recommended way to install the library is through 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).