Skip to main content

Quick Start for PHP

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:

The Direct endpoint provides immediate PDF generation and returns the raw PDF file in the response.

Choose your source:

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";
}
}

Next Steps



Related reading