PHP SDK
Use the official Pritset PHP SDK to manage DOCX templates and generate PDFs from your PHP application.
View the official PHP SDK repository on GitHub
Requirementsโ
- PHP 8.3 or newer
- Composer 2
- A Pritset access token and secret from the Profile page
Compatibilityโ
This page documents SDK version 0.1.5, which implements Pritset SDK contract 1.0.0.
Install the packageโ
Install version 0.1.5 from Packagist:
composer require pritset/pritset-php:0.1.5
Create a clientโ
Store credentials in environment variables rather than source code. Pritset sends the access token directly in the Authorization header and the secret in X-Secret; do not add a Bearer prefix.
<?php
require __DIR__ . '/vendor/autoload.php';
use Pritset\PritsetClient;
$client = new PritsetClient(
accessToken: $_ENV['PRITSET_ACCESS_TOKEN'],
secret: $_ENV['PRITSET_SECRET'],
);
Credentials are redacted from normal object debug output. Do not commit or log either value.
Generate a PDFโ
Upload a DOCX template first and copy its ID. See Template management for upload requirements and the portal workflow.
generate() returns a streaming response, so large documents do not need to be buffered in memory.
$pdf = $client->documents()->generate('YOUR_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
The underlying PSR-7 stream is available as $pdf->stream. Both getContents() and saveToFile() consume the stream from its current position. Your document data must match the placeholders and loops in the DOCX file; see Templating syntax.
Manage templatesโ
Use client->templates() to list, retrieve, create, update, download, validate, and delete templates.
use Pritset\Model\ListTemplatesOptions;
use Pritset\Value\Upload;
$page = $client->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;
}
$created = $client->templates()->create(
name: 'Monthly invoice',
template: Upload::fromPath(__DIR__ . '/invoice.docx'),
tags: 'invoice,monthly',
);
Retrieve the complete template record, including file metadata, then update its name and tags. The template argument is optional on update; provide it to replace the DOCX file.
$details = $client->templates()->get($created->id);
echo $details->fileInfo->objectName . ': ' . $details->fileInfo->size . PHP_EOL;
$updated = $client->templates()->update(
id: $created->id,
name: 'Monthly invoice 2026',
tags: 'invoice,monthly,2026',
template: Upload::fromPath(__DIR__ . '/invoice-2026.docx'), // optional replacement file
);
Uploads can come from a path, a string, a PHP resource, or a PSR-7 stream:
Upload::fromString($docxBytes, 'invoice.docx');
Upload::fromStream($stream, 'invoice.docx');
$download = $client->templates()->download($created->id);
$download->saveToFile(__DIR__ . '/downloaded-invoice.docx');
$valid = $client->templates()->validate(
Upload::fromPath(__DIR__ . '/invoice.docx'),
['invoice' => ['number' => 'INV-1042']],
);
echo 'Template valid: ' . ($valid ? 'yes' : 'no') . PHP_EOL;
$client->templates()->delete($created->id);
For endpoint behavior and limits, see the Template management API reference.
Raw JSONโ
Document data may be an array, JsonSerializable object, or an already encoded JSON string. Raw strings are validated before a request is sent.
$pdf = $client->documents()->generate('YOUR_TEMPLATE_ID', '{"name":"Ada"}');
Generate through a webhookโ
Use generateWebhook() when Pritset should send the generation result to your endpoint. The webhook URL must be an absolute HTTP(S) URL without embedded credentials.
$job = $client->documents()->generateWebhook(
templateId: 'YOUR_TEMPLATE_ID',
data: ['invoice' => ['number' => 'INV-1042']],
webhookUrl: 'https://example.com/webhooks/pritset',
);
echo $job->id;
The SDK submits the URL to Pritset; it does not call the webhook itself. See Direct and webhook processing for the API workflow.
Handle errorsโ
The SDK raises PritsetApiException for API responses with an error status and PritsetTransportException when no response is received or it is malformed. It does not retain the original Guzzle exception because that may include credential-bearing headers. The SDK performs no automatic retries.
use Pritset\Exception\PritsetApiException;
use Pritset\Exception\PritsetTransportException;
try {
$client->documents()->generate('YOUR_TEMPLATE_ID', $data);
} catch (PritsetApiException $error) {
echo $error->statusCode;
print_r($error->fieldErrors);
echo $error->traceId;
echo $error->retryAfter;
} catch (PritsetTransportException $error) {
// No HTTP response was received, or the response was malformed.
}
See API errors for status codes and retry guidance.
Local development and custom HTTP clientsโ
The SDK uses https://api.pritset.com by default and disables redirects to prevent credentials from being forwarded to another origin. Set the timeout in seconds when creating the client:
use GuzzleHttp\Client;
use Pritset\PritsetClient;
$client = new PritsetClient(
accessToken: $_ENV['PRITSET_ACCESS_TOKEN'],
secret: $_ENV['PRITSET_SECRET'],
timeout: 60.0,
httpClient: new Client(),
);
HTTPS is required except for exact localhost, 127.0.0.1, or ::1 development endpoints.