codeinc / office2pdf-client

office2pdf API 的 PHP 客户端

v1.4 2024-02-24 01:26 UTC

This package is auto-updated.

Last update: 2024-09-24 02:41:42 UTC


README

该仓库包含一个 PHP 8.2+ 库,用于通过 office2pdf 服务将 Office 文件转换为 PDF。有关 office2pdf 服务的更多信息,请访问 office2pdf

安装

推荐通过 Composer 安装库

composer require codeinc/office2pdf-client

用法

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

示例

use CodeInc\Office2PdfClient\Office2PdfClient;
use CodeInc\Office2PdfClient\ConvertOptions;
use CodeInc\Office2PdfClient\Format;

$apiBaseUri = 'http://localhost:3000/';
$srcDocPath = '/path/to/local/file.docx';
$destPdfPath = '/path/to/local/file.pdf';
$convertOption = new ConvertOptions(
    firstPage: 2,
    lastPage: 3,
    format: Format::json
);

try {
    $client = new Office2PdfClient($apiBaseUri);

    // convert 
    $pdfStream = $client->convert(
        $client->createStreamFromFile($srcDocPath), 
        $convertOption
    );
    
   // save the PDF
   $client->saveStreamToFile($pdfStream, $destPdfPath); 
}
catch (Exception $e) {
    // handle exception
}

验证文件格式的支持

use CodeInc\Office2PdfClient\Office2PdfClient;
use CodeInc\Office2PdfClient\Exception;

$filename = 'a-file.docx';

$client = new Office2PdfClient('http://localhost:3000/');

$client->isSupported("a-file.docx"); // returns true
$client->isSupported("a-file"); // returns true 
$client->isSupported("a-file", false); // returns false (the second argument is the strict mode)
$client->isSupported("a-file.pdf"); // returns false

许可证

该库根据 MIT 许可证发布(请参阅 LICENSE 文件)。