PHP code example of pritset / pritset-php

1. Go to this page and download the library: Download pritset/pritset-php 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/ */

    

pritset / pritset-php example snippets




ritset\PritsetClient;

$pritset = new PritsetClient(
    accessToken: $_ENV['PRITSET_ACCESS_TOKEN'],
    secret: $_ENV['PRITSET_SECRET'],
);

$pdf = $pritset->documents()->generate('template-id', [
    'invoice' => [
        'number' => 'INV-1042',
        'customer' => 'Ada Lovelace',
    ],
]);

$pdf->saveToFile(__DIR__ . '/invoice.pdf');

echo $pdf->contentType;   // application/pdf
echo $pdf->contentLength; // int|null
echo $pdf->trace;         // processing diagnostics, when supplied

use Pritset\Model\ListTemplatesOptions;

$page = $pritset->templates()->list(new ListTemplatesOptions(
    query: 'invoice',
    page: 1,
    pageSize: 25,
    sortBy: 'name',
    sortDirection: 0, // 0 ascending, 1 descending
));

foreach ($page->data as $template) {
    echo $template->id . ': ' . $template->name . PHP_EOL;
}

$details = $pritset->templates()->get('template-id');
echo $details->fileInfo->objectName;

use Pritset\Value\Upload;

$created = $pritset->templates()->create(
    name: 'Monthly invoice',
    template: Upload::fromPath(__DIR__ . '/invoice.docx'),
    tags: 'invoice,monthly',
);

$updated = $pritset->templates()->update(
    id: $created->id,
    name: 'Monthly invoice v2',
    tags: 'invoice,monthly',
    template: Upload::fromPath(__DIR__ . '/invoice-v2.docx'), // optional
);

Upload::fromString($docxBytes, 'invoice.docx');
Upload::fromStream($stream, 'invoice.docx');

$download = $pritset->templates()->download('template-id');
$download->saveToFile(__DIR__ . '/template.docx');

$pritset->templates()->delete('template-id');

$valid = $pritset->templates()->validate(
    Upload::fromPath(__DIR__ . '/invoice.docx'),
    ['invoice' => ['number' => 'INV-1042']],
);

$job = $pritset->documents()->generateWebhook(
    templateId: 'template-id',
    data: ['invoice' => ['number' => 'INV-1042']],
    webhookUrl: 'https://example.com/webhooks/pritset',
);

echo $job->id;

$pdf = $pritset->documents()->generate('template-id', '{"name":"Ada"}');

use Pritset\Exception\PritsetApiException;
use Pritset\Exception\PritsetTransportException;

try {
    $pritset->documents()->generate('template-id', $data);
} catch (PritsetApiException $error) {
    echo $error->statusCode;
    print_r($error->fieldErrors); // normalized field => list<string>
    echo $error->traceId;
    echo $error->retryAfter;
} catch (PritsetTransportException $error) {
    // No HTTP response was received, or the response was malformed.
}

use GuzzleHttp\Client;

$pritset = new PritsetClient(
    accessToken: $_ENV['PRITSET_ACCESS_TOKEN'],
    secret: $_ENV['PRITSET_SECRET'],
    baseUrl: 'https://api.pritset.com',
    timeout: 60.0,
    httpClient: new Client(),
);
bash
composer