PHP PDF Generation SDK
The official cloudlayerio/sdk package provides PHP PDF generation for any PHP 8.1+ application. Convert HTML to PDF, capture URLs as documents, and render templates — with full type safety (PHPStan level 8), strict types, and a single runtime dependency (guzzlehttp/guzzle).
- Packagist Package: cloudlayerio/sdk
- Source Code: github.com/cloudlayerio/cloudlayerio-php
Installation
composer require cloudlayerio/sdk
Requirements: PHP 8.1+
Setup
Get your API key by creating a free account at cloudlayer.io. Your account comes with free API credits for testing.
use CloudLayer\CloudLayer;
$client = new CloudLayer(
apiKey: 'YOUR-API-KEY',
apiVersion: 'v2',
);
The SDK requires you to choose an API version:
| v1 | v2 | |
|---|---|---|
| Default mode | Synchronous (returns binary) | Asynchronous (returns Job) |
| Sync response | Raw binary (PDF/image bytes) | JSON Job object |
| Binary access | Direct from $result->data | Via downloadJobResult() |
URL to PDF
v2 (recommended)
v2 returns a Job object. Use downloadJobResult() to get the binary:
$result = $client->urlToPdf(['url' => 'https://example.com']);
$job = $client->waitForJob($result->data->id);
$pdfBytes = $client->downloadJobResult($job);
file_put_contents('output.pdf', $pdfBytes);
v1 (direct binary)
v1 returns the binary directly:
$client = new CloudLayer(apiKey: 'YOUR-API-KEY', apiVersion: 'v1');
$result = $client->urlToPdf(['url' => 'https://example.com']);
file_put_contents('output.pdf', $result->data); // raw PDF bytes
HTML to PDF
$html = base64_encode('<h1>Hello World</h1>');
$result = $client->htmlToPdf(['html' => $html]);
Template Rendering
$result = $client->templateToPdf([
'templateId' => 'your-template-id',
'data' => ['name' => 'John', 'total' => '$100'],
]);
Error Handling
use CloudLayer\Errors\AuthException;
use CloudLayer\Errors\RateLimitException;
use CloudLayer\Errors\CloudLayerException;
try {
$result = $client->urlToPdf(['url' => 'https://example.com']);
} catch (AuthException $e) {
echo "Invalid API key\n";
} catch (RateLimitException $e) {
echo "Rate limited, retry after {$e->retryAfter}s\n";
} catch (CloudLayerException $e) {
echo "Error: {$e->getMessage()}\n";
}
Full Documentation
See the README on GitHub for complete documentation including all conversion methods, data management, storage configuration, and performance notes.