pdfsquid/client-php

PDFsquid API 的 PHP 客户端

0.9.2 2017-05-24 15:03 UTC

This package is not auto-updated.

Last update: 2024-09-29 03:00:47 UTC


README

这个库简化了用于 PDFsquid 转换机制 ZoneApi 的使用。

要求和依赖

库需要 PHP 5.3 及以上版本。另外,请确保您的服务器上已安装以下 PHP 扩展

您还需要 ZoneApi(API 密钥,API 密码)和区域名称才能使用此客户端。如果您没有,您可以

  • 客户端面板注册,如果您还没有账户
  • 添加新的 API 访问

或者

  • 向已注册的管理员请求 API 访问

通过 Composer 安装

这是首选的安装方法。如果您还没有安装 composer,请先从 Composer 安装它。然后运行以下命令

composer require pdfsquid/client-php

然后使用 Composer 的 自动加载 机制

require_once('vendor/autoload.php');

手动安装

可能存在无法使用 composer 安装此库的情况(例如,系统上不可用)。在这种情况下,您可以下载 最新版本,然后仅包含 autoload.php 文件。

require_once('/path/to/client-php/autoload.php');

示例用法

从 URL/HTML 同步下载 PDF 文件

<?php

try {
    // initialize the library
    // pass your credentials, $zone_name is zone associated with api access eg. 'eu1'
    $client = new \PDFsquid\ZoneApi($api_key, $api_secret, $zone_name);
    
    // convert synchronously
    $file = $client->url2pdf('https://google.com');
    // $file = $client->html2pdf('<b>Hello!</b>');
    
    // download file as attachment, you can pass file name as option (default is conversionId value)
    $file->downloadAsAttachment();
    // you can also download file to show directly in browser
    // $file->downloadInline();
    // see more methods on file in class ResponseFile
}
catch(\PDFsquid\Exceptions\PDFsquidException $e) {
    // Jump here on conversion error, authentication error or API is not available
    // get HTTP code
    $code = $e->getHttpCode();
    // get errors
    $errors = $e->getErrors();
    /*
     * $errors can be null or array e.g.:
     *   ["field"]=>
     *    string(9) "x-api-key"
     *    ["message"]=>
     *    string(18) "value is not valid"
     *    ["value"]=>
     *    string(35) "value passed to API"
     */
}

从 URL/HTML 异步订购 PDF 转换(例如,在大量转换场景中)

<?php

try {
    // initialize the library
    // pass your credentials, $zone_name is zone associated with api access eg. 'eu1'
    $client = new \PDFsquid\ZoneApi($api_key, $api_secret, $zone_name);
    
    // convert asynchronously
    $result = $client->url2pdfAsync('https://google.com');
    // $result = $client->html2pdf('<b>Hello!</b>', false);
    
    // now $result is object with conversionId
    // PDFsquid will call Client's handler defined in Client Panel when conversion ends (ping)
    $conversionId = $result->conversionId;
}
catch(\PDFsquid\Exceptions\PDFsquidException $e) {
    // Jump here on conversion error, authentication error or API is not available
    // get HTTP code
    $code = $e->getHttpCode();
    // get errors
    $errors = $e->getErrors();
    /*
     * $errors can be null or array e.g.:
     *   ["field"]=>
     *    string(9) "x-api-key"
     *    ["message"]=>
     *    string(18) "value is not valid"
     *    ["value"]=>
     *    string(35) "value passed to API"
     */
}

从异步转换获取文件:以下代码应在客户端面板指定的 webhook 中存在。可用的 GET 参数有:conversionIdstatus(成功/错误),errorCode(错误代码,错误时存在)

<?php

try {
    $conversionId = $_GET['conversionId'];
    
    // initialize the library
    // pass your credentials, $zone_name is zone associated with api access eg. 'eu1'
    $client = new \PDFsquid\ZoneApi($api_key, $api_secret, $zone_name);
    
    // convert asynchronously
    $file = $client->getFile($conversionId);
    
    // save file on server
    $file->saveFile('/var/www/files/', 'custom_file_name');
    // see more methods on file in class ResponseFile
}
catch(\PDFsquid\Exceptions\PDFsquidException $e) {
    // Jump here on conversion error, authentication error or API is not available
    // get HTTP code
    $code = $e->getHttpCode();
    // get errors
    $errors = $e->getErrors();
    /*
     * $errors can be null or array e.g.:
     *   ["field"]=>
     *    string(9) "x-api-key"
     *    ["message"]=>
     *    string(18) "value is not valid"
     *    ["value"]=>
     *    string(35) "value passed to API"
     */
}

文档

此 README 描述了与 PHP 客户端库一起使用的方法。完整的 ZoneApi 文档可在 https://docs.pdfsquid.com/ 找到。

问题

如果您发现此库有任何问题,请通过此项目页面提出问题。请尽量提供有关问题的描述、系统环境和复制步骤等信息。我们将尽快回复。