PHP code example of codeinc / pdf2img-client

1. Go to this page and download the library: Download codeinc/pdf2img-client library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

codeinc / pdf2img-client example snippets


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
}

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
}