Quick Start for PHP
Integrate PDFBolt's REST API in PHP to generate PDFs from URLs, HTML, or templates. The examples below cover all three conversion modes (Direct, Sync, Async).
1. Get Your API Key
Find your API key on the API Keys page in your Dashboard. If you don't have an account, sign up – the free plan includes 100 document conversions per month.
2. Make Your First Request
Any HTTP client works – adjust the request structure to match your library.
Examples use Guzzle. Install it with:
composer require guzzlehttp/guzzle
Choose your endpoint:
- Direct
- Sync
- Async
The Direct endpoint provides immediate PDF generation and returns the raw PDF file in the response.
Choose your source:
- URL
- HTML
- Template
Convert a webpage to PDF:
<?php
require __DIR__ . '/vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
$headers = [
'API-KEY' => 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX',
'Content-Type' => 'application/json',
];
$body = [
'url' => 'https://example.com',
'format' => 'A4',
'printBackground' => true,
];
try {
$client = new Client();
$response = $client->post('https://api.pdfbolt.com/v1/direct', [
'headers' => $headers,
'json' => $body,
]);
file_put_contents('webpage.pdf', (string) $response->getBody());
echo "PDF generated successfully\n";
} catch (RequestException $e) {
if ($e->hasResponse()) {
echo "HTTP " . $e->getResponse()->getStatusCode() . "\n";
echo "Error Message: " . $e->getResponse()->getBody() . "\n";
} else {
echo "Error: " . $e->getMessage() . "\n";
}
}
Convert HTML to PDF (HTML must be base64 encoded):
<?php
require __DIR__ . '/vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
$htmlContent = '<html><body><h1>Hello!</h1><p>This is a sample PDF.</p></body></html>';
$base64Html = base64_encode($htmlContent);
$headers = [
'API-KEY' => 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX',
'Content-Type' => 'application/json',
];
$body = [
'html' => $base64Html,
'format' => 'A4',
'printBackground' => true,
'margin' => [
'top' => '30px',
'left' => '30px',
],
];
try {
$client = new Client();
$response = $client->post('https://api.pdfbolt.com/v1/direct', [
'headers' => $headers,
'json' => $body,
]);
file_put_contents('document.pdf', (string) $response->getBody());
echo "PDF generated successfully\n";
} catch (RequestException $e) {
if ($e->hasResponse()) {
echo "HTTP " . $e->getResponse()->getStatusCode() . "\n";
echo "Error Message: " . $e->getResponse()->getBody() . "\n";
} else {
echo "Error: " . $e->getMessage() . "\n";
}
}
Convert a template with data to PDF:
<?php
require __DIR__ . '/vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
$headers = [
'API-KEY' => 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX',
'Content-Type' => 'application/json',
];
$body = [
'templateId' => 'your-template-id',
'templateData' => [
'client_name' => 'John Doe',
'invoice_number' => 'INV-001',
'total_amount' => '$299.99',
'line_items' => [
['description' => 'Web Development', 'unit_price' => '$200.00'],
['description' => 'Design Services', 'unit_price' => '$99.99'],
],
],
];
try {
$client = new Client();
$response = $client->post('https://api.pdfbolt.com/v1/direct', [
'headers' => $headers,
'json' => $body,
]);
file_put_contents('invoice.pdf', (string) $response->getBody());
echo "PDF generated successfully\n";
} catch (RequestException $e) {
if ($e->hasResponse()) {
echo "HTTP " . $e->getResponse()->getStatusCode() . "\n";
echo "Error Message: " . $e->getResponse()->getBody() . "\n";
} else {
echo "Error: " . $e->getMessage() . "\n";
}
}
Create your first template in the app, then use its ID with JSON data in your API calls. Learn more about Templates
The Sync endpoint returns a JSON response with a download URL for the PDF (valid for 24 hours).
Choose your source:
- URL
- HTML
- Template
Convert a webpage and get a download URL:
<?php
require __DIR__ . '/vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
$headers = [
'API-KEY' => 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX',
'Content-Type' => 'application/json',
];
$body = [
'url' => 'https://example.com',
'format' => 'A4',
'printBackground' => true,
];
try {
$client = new Client();
$response = $client->post('https://api.pdfbolt.com/v1/sync', [
'headers' => $headers,
'json' => $body,
]);
$result = json_decode((string) $response->getBody(), true);
echo "PDF URL: " . $result['documentUrl'] . "\n";
} catch (RequestException $e) {
if ($e->hasResponse()) {
echo "HTTP " . $e->getResponse()->getStatusCode() . "\n";
echo "Error Message: " . $e->getResponse()->getBody() . "\n";
} else {
echo "Error: " . $e->getMessage() . "\n";
}
}
Convert HTML and get a download URL (HTML must be base64 encoded):
<?php
require __DIR__ . '/vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
$htmlContent = '<html><body><h1>Hello!</h1><p>This is a sample PDF.</p></body></html>';
$base64Html = base64_encode($htmlContent);
$headers = [
'API-KEY' => 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX',
'Content-Type' => 'application/json',
];
$body = [
'html' => $base64Html,
'format' => 'A4',
'printBackground' => true,
'margin' => [
'top' => '30px',
'left' => '30px',
],
];
try {
$client = new Client();
$response = $client->post('https://api.pdfbolt.com/v1/sync', [
'headers' => $headers,
'json' => $body,
]);
$result = json_decode((string) $response->getBody(), true);
echo "PDF URL: " . $result['documentUrl'] . "\n";
} catch (RequestException $e) {
if ($e->hasResponse()) {
echo "HTTP " . $e->getResponse()->getStatusCode() . "\n";
echo "Error Message: " . $e->getResponse()->getBody() . "\n";
} else {
echo "Error: " . $e->getMessage() . "\n";
}
}
Convert a template with data and get a download URL:
<?php
require __DIR__ . '/vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
$headers = [
'API-KEY' => 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX',
'Content-Type' => 'application/json',
];
$body = [
'templateId' => 'your-template-id',
'templateData' => [
'client_name' => 'John Doe',
'invoice_number' => 'INV-001',
'total_amount' => '$299.99',
'line_items' => [
['description' => 'Web Development', 'unit_price' => '$200.00'],
['description' => 'Design Services', 'unit_price' => '$99.99'],
],
],
];
try {
$client = new Client();
$response = $client->post('https://api.pdfbolt.com/v1/sync', [
'headers' => $headers,
'json' => $body,
]);
$result = json_decode((string) $response->getBody(), true);
echo "PDF URL: " . $result['documentUrl'] . "\n";
} catch (RequestException $e) {
if ($e->hasResponse()) {
echo "HTTP " . $e->getResponse()->getStatusCode() . "\n";
echo "Error Message: " . $e->getResponse()->getBody() . "\n";
} else {
echo "Error: " . $e->getMessage() . "\n";
}
}
Create your first template in the app, then use its ID with JSON data in your API calls. Learn more about Templates
The Async endpoint returns a requestId immediately and delivers the final result via webhook callback.
Choose your source:
- URL
- HTML
- Template
Convert a webpage and receive a webhook callback:
<?php
require __DIR__ . '/vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
$headers = [
'API-KEY' => 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX',
'Content-Type' => 'application/json',
];
$body = [
'url' => 'https://example.com',
'format' => 'A4',
'printBackground' => true,
'webhook' => 'https://your-app.com/webhook',
];
try {
$client = new Client();
$response = $client->post('https://api.pdfbolt.com/v1/async', [
'headers' => $headers,
'json' => $body,
]);
$result = json_decode((string) $response->getBody(), true);
echo "Request ID: " . $result['requestId'] . "\n";
echo "PDF will be sent to webhook when ready\n";
} catch (RequestException $e) {
if ($e->hasResponse()) {
echo "HTTP " . $e->getResponse()->getStatusCode() . "\n";
echo "Error Message: " . $e->getResponse()->getBody() . "\n";
} else {
echo "Error: " . $e->getMessage() . "\n";
}
}
Convert HTML and receive a webhook callback (HTML must be base64 encoded):
<?php
require __DIR__ . '/vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
$htmlContent = '<html><body><h1>Hello!</h1><p>This is a sample PDF.</p></body></html>';
$base64Html = base64_encode($htmlContent);
$headers = [
'API-KEY' => 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX',
'Content-Type' => 'application/json',
];
$body = [
'html' => $base64Html,
'format' => 'A4',
'printBackground' => true,
'margin' => [
'top' => '30px',
'left' => '30px',
],
'webhook' => 'https://your-app.com/webhook',
];
try {
$client = new Client();
$response = $client->post('https://api.pdfbolt.com/v1/async', [
'headers' => $headers,
'json' => $body,
]);
$result = json_decode((string) $response->getBody(), true);
echo "Request ID: " . $result['requestId'] . "\n";
echo "PDF will be sent to webhook when ready\n";
} catch (RequestException $e) {
if ($e->hasResponse()) {
echo "HTTP " . $e->getResponse()->getStatusCode() . "\n";
echo "Error Message: " . $e->getResponse()->getBody() . "\n";
} else {
echo "Error: " . $e->getMessage() . "\n";
}
}
Convert a template with data and receive a webhook callback:
<?php
require __DIR__ . '/vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
$headers = [
'API-KEY' => 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX',
'Content-Type' => 'application/json',
];
$body = [
'templateId' => 'your-template-id',
'templateData' => [
'client_name' => 'John Doe',
'invoice_number' => 'INV-001',
'total_amount' => '$299.99',
'line_items' => [
['description' => 'Web Development', 'unit_price' => '$200.00'],
['description' => 'Design Services', 'unit_price' => '$99.99'],
],
],
'webhook' => 'https://your-app.com/webhook',
];
try {
$client = new Client();
$response = $client->post('https://api.pdfbolt.com/v1/async', [
'headers' => $headers,
'json' => $body,
]);
$result = json_decode((string) $response->getBody(), true);
echo "Request ID: " . $result['requestId'] . "\n";
echo "PDF will be sent to webhook when ready\n";
} catch (RequestException $e) {
if ($e->hasResponse()) {
echo "HTTP " . $e->getResponse()->getStatusCode() . "\n";
echo "Error Message: " . $e->getResponse()->getBody() . "\n";
} else {
echo "Error: " . $e->getMessage() . "\n";
}
}
Create your first template in the app, then use its ID with JSON data in your API calls. Learn more about Templates
Next Steps
📄️ API Endpoints
📄️ Conversion Parameters
📄️ Error Handling
📄️ Templates
- Optimizing HTML for Professional PDF Output – HTML/CSS techniques: page breaks, fonts, and image optimization.
- Top HTML Template Engines for Dynamic PDF Generation – covers Twig and Smarty for PHP templating workflows.