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-09-24 02:28:03 UTC


README

此存储库包含一个用于使用 pdf2img 服务将PDF文件转换为图片的PHP 8.2+库。

安装

安装此库的推荐方式是通过 Composer

composer require codeinc/pdf2img-client

用法

此客户端需要一个运行的 pdf2img 服务实例。服务可以通过Docker在本地上运行 使用Docker,或者部署到服务器上。

基本示例

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
}

许可证

此库根据MIT许可证发布(见 LICENSE 文件)。