Printgraph Docs

PHP SDK

Printgraph API を PHP から利用するための SDK

概要

Printgraph PHP SDK は、PHP アプリケーションから Printgraph API を簡単に利用できるライブラリです。テンプレートからの PDF 生成を数行のコードで実現できます。

動作要件

  • PHP 8.1 以上

インストール

Composer を使用してインストールできます:

composer require printgraph/php-sdk

初期化

SDK を使用するには、まず Printgraph クライアントを初期化する必要があります。API キーは環境変数として設定することを推奨します。

<?php

use Printgraph\PhpSdk\Client\ClientFactory;
use Printgraph\PhpSdk\Printgraph;

require 'vendor/autoload.php';

$printgraph = new Printgraph(
    ClientFactory::createHttpClient(getenv('PRINTGRAPH_TOKEN'))
);

API キーの取得

API キーの取得方法はこちらを参照してください。

基本的な使い方

PDF の生成

GenerateRequest クラスを使用して、テンプレートから PDF を生成できます:

<?php

use Printgraph\PhpSdk\Api\Pdf\Generator\GenerateRequest;
use Printgraph\PhpSdk\Client\ClientFactory;
use Printgraph\PhpSdk\Printgraph;

require 'vendor/autoload.php';

$printgraph = new Printgraph(
    ClientFactory::createHttpClient(getenv('PRINTGRAPH_TOKEN'))
);

// PDF 生成リクエストを作成
$request = new GenerateRequest(
    'HELLO_TEMPLATE_KEY',
    ['name' => 'world']
);

// PDF を生成
$response = $printgraph->pdf()->generate($request)->expect(
    new \RuntimeException('Failed to generate PDF')
);

// ファイルに保存
file_put_contents('hello_world.pdf', $response->getContents());

パラメータの指定

GenerateRequest の第2引数に連想配列を渡すことで、テンプレートに動的なデータを渡すことができます:

$request = new GenerateRequest(
    'INVOICE_TEMPLATE_KEY',
    [
        'invoiceNumber' => 'INV-001',
        'customerName' => '山田太郎',
        'amount' => 10000,
        'items' => [
            ['name' => '商品A', 'price' => 5000, 'quantity' => 1],
            ['name' => '商品B', 'price' => 2500, 'quantity' => 2],
        ],
    ]
);

API リファレンス

new Printgraph(ClientInterface $client)

Printgraph クライアントのインスタンスを作成します。

パラメータ:

  • $client (ClientInterface): HTTP クライアントインスタンス

戻り値:

  • Printgraph クライアントインスタンス

ClientFactory::createHttpClient(string $token): ClientInterface

認証済みの HTTP クライアントを作成します。

パラメータ:

  • $token (string): Printgraph API トークン

戻り値:

  • ClientInterface インスタンス

new GenerateRequest(string $templateKey, array $params)

PDF 生成リクエストを作成します。

パラメータ:

  • $templateKey (string): 使用するテンプレートのキー
  • $params (array): テンプレートに渡すパラメータ

$printgraph->pdf()->generate(GenerateRequest $request)

テンプレートから PDF を生成します。

パラメータ:

  • $request (GenerateRequest): PDF 生成リクエスト

戻り値:

  • Result オブジェクト(expect() メソッドでレスポンスを取得)

$result->expect(Throwable $exception)

結果を取得します。エラーが発生した場合は指定した例外をスローします。

パラメータ:

  • $exception (Throwable): エラー時にスローする例外

戻り値:

  • レスポンスオブジェクト(getContents() で PDF バイナリを取得)

環境変数の設定

.env ファイルに API トークンを設定することを推奨します:

PRINTGRAPH_TOKEN=your_api_token_here

PHP アプリケーションでは、vlucas/phpdotenv パッケージを使用して環境変数を読み込めます:

composer require vlucas/phpdotenv
<?php

use Dotenv\Dotenv;
use Printgraph\PhpSdk\Client\ClientFactory;
use Printgraph\PhpSdk\Printgraph;

require 'vendor/autoload.php';

$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();

$printgraph = new Printgraph(
    ClientFactory::createHttpClient($_ENV['PRINTGRAPH_TOKEN'])
);

リポジトリ

SDK のソースコードは GitHub で公開されています: https://github.com/printgraph/php-sdk