Quick Start for PHP
While wkhtmltopdf is commonly used in PHP and Puppeteer requires workarounds due to its Node.js nature, PDFBolt's PDF generation API delivers a streamlined solution for generating PDFs from HTML, URLs, and dynamic templates without needing binary installations or complex setups.
1. Get Your API Key
- After signing up, locate your API Key in the Admin Dashboard under the API Keys section.
- This key is essential for authorizing your requests to the PDFBolt API. Remember to keep it secure.
2. Make Your First Request
Try these examples to start making requests to the PDFBolt API and create your PDFs.
Choose your preferred endpoint and source combination:
➡️ Endpoints:
- Direct
- Sync
- Async
The Direct endpoint provides immediate PDF generation and returns the raw PDF file in the response.
➡️ Sources:
- URL
- HTML
- Template
Convert any webpage into a PDF:
<?php
require_once 'vendor/autoload.php';
use GuzzleHttp\Client;
$jsonHeaders = '{"API-KEY":"XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX","Content-Type":"application/json"}';
$phpHeaders = json_decode($jsonHeaders, true);
$jsonBody = '{"url":"https://example.com","format":"A4","printBackground":true}';
$phpBody = json_decode($jsonBody, true);
try {
$client = new Client();
$response = $client->post('https://api.pdfbolt.com/v1/direct', [
'headers' => $phpHeaders,
'json' => $phpBody
]);
file_put_contents('webpage.pdf', $response->getBody());
echo "PDF generated successfully\n";
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}
?>
Convert HTML content directly into a PDF:
<?php
require_once 'vendor/autoload.php';
use GuzzleHttp\Client;
$htmlContent = '<html><body><h1>Hello!</h1><p>This is a sample PDF.</p></body></html>';
$base64Html = base64_encode($htmlContent);
$jsonHeaders = '{"API-KEY":"XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX","Content-Type":"application/json"}';
$phpHeaders = json_decode($jsonHeaders, true);
$jsonBody = '{"html":"' . $base64Html . '","margin":{"top":"30px","left":"30px"}}';
$phpBody = json_decode($jsonBody, true);
try {
$client = new Client();
$response = $client->post('https://api.pdfbolt.com/v1/direct', [
'headers' => $phpHeaders,
'json' => $phpBody
]);
file_put_contents('document.pdf', $response->getBody());
echo "PDF generated successfully\n";
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}
?>
Use reusable templates for dynamic PDF generation:
<?php
require_once 'vendor/autoload.php';
use GuzzleHttp\Client;
$jsonHeaders = '{"API-KEY":"XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX","Content-Type":"application/json"}';
$phpHeaders = json_decode($jsonHeaders, true);
$jsonBody = '{"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"}]}}';
$phpBody = json_decode($jsonBody, true);
try {
$client = new Client();
$response = $client->post('https://api.pdfbolt.com/v1/direct', [
'headers' => $phpHeaders,
'json' => $phpBody
]);
file_put_contents('invoice.pdf', $response->getBody());
echo "PDF generated successfully\n";
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}
?>
Create your first template in the app, then use its ID in your API calls.
The Sync endpoint returns a JSON response with a download URL for the PDF.
➡️ Sources:
- URL
- HTML
- Template
Convert webpage and get download URL:
<?php
require_once 'vendor/autoload.php';
use GuzzleHttp\Client;
$jsonHeaders = '{"API-KEY":"XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX","Content-Type":"application/json"}';
$phpHeaders = json_decode($jsonHeaders, true);
$jsonBody = '{"url":"https://example.com","format":"A4","printBackground":true}';
$phpBody = json_decode($jsonBody, true);
try {
$client = new Client();
$response = $client->post('https://api.pdfbolt.com/v1/sync', [
'headers' => $phpHeaders,
'json' => $phpBody
]);
$result = json_decode($response->getBody(), true);
echo "PDF URL: " . $result['documentUrl'] . "\n";
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}
?>
Convert HTML and get download URL:
<?php
require_once 'vendor/autoload.php';
use GuzzleHttp\Client;
$htmlContent = '<html><body><h1>Hello!</h1><p>This is a sample PDF.</p></body></html>';
$base64Html = base64_encode($htmlContent);
$jsonHeaders = '{"API-KEY":"XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX","Content-Type":"application/json"}';
$phpHeaders = json_decode($jsonHeaders, true);
$jsonBody = '{"html":"' . $base64Html . '","margin":{"top":"30px","left":"30px"}}';
$phpBody = json_decode($jsonBody, true);
try {
$client = new Client();
$response = $client->post('https://api.pdfbolt.com/v1/sync', [
'headers' => $phpHeaders,
'json' => $phpBody
]);
$result = json_decode($response->getBody(), true);
echo "PDF URL: " . $result['documentUrl'] . "\n";
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}
?>
Use templates and get download URL:
<?php
require_once 'vendor/autoload.php';
use GuzzleHttp\Client;
$jsonHeaders = '{"API-KEY":"XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX","Content-Type":"application/json"}';
$phpHeaders = json_decode($jsonHeaders, true);
$jsonBody = '{"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"}]}}';
$phpBody = json_decode($jsonBody, true);
try {
$client = new Client();
$response = $client->post('https://api.pdfbolt.com/v1/sync', [
'headers' => $phpHeaders,
'json' => $phpBody
]);
$result = json_decode($response->getBody(), true);
echo "PDF URL: " . $result['documentUrl'] . "\n";
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}
?>
The Async endpoint processes requests asynchronously and sends results via webhook.
➡️ Sources:
- URL
- HTML
- Template
Convert webpage with webhook notification:
<?php
require_once 'vendor/autoload.php';
use GuzzleHttp\Client;
$jsonHeaders = '{"API-KEY":"XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX","Content-Type":"application/json"}';
$phpHeaders = json_decode($jsonHeaders, true);
$jsonBody = '{"url":"https://example.com","format":"A4","printBackground":true,"webhook":"https://your-app.com/webhook"}';
$phpBody = json_decode($jsonBody, true);
try {
$client = new Client();
$response = $client->post('https://api.pdfbolt.com/v1/async', [
'headers' => $phpHeaders,
'json' => $phpBody
]);
$result = json_decode($response->getBody(), true);
echo "Request ID: " . $result['requestId'] . "\n";
echo "PDF will be sent to webhook when ready\n";
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}
?>
Convert HTML with webhook notification:
<?php
require_once 'vendor/autoload.php';
use GuzzleHttp\Client;
$htmlContent = '<html><body><h1>Hello!</h1><p>This is a sample PDF.</p></body></html>';
$base64Html = base64_encode($htmlContent);
$jsonHeaders = '{"API-KEY":"XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX","Content-Type":"application/json"}';
$phpHeaders = json_decode($jsonHeaders, true);
$jsonBody = '{"html":"' . $base64Html . '","margin":{"top":"30px","left":"30px"},"webhook":"https://your-app.com/webhook"}';
$phpBody = json_decode($jsonBody, true);
try {
$client = new Client();
$response = $client->post('https://api.pdfbolt.com/v1/async', [
'headers' => $phpHeaders,
'json' => $phpBody
]);
$result = json_decode($response->getBody(), true);
echo "Request ID: " . $result['requestId'] . "\n";
echo "PDF will be sent to webhook when ready\n";
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}
?>
Use templates with webhook notification:
<?php
require_once 'vendor/autoload.php';
use GuzzleHttp\Client;
$jsonHeaders = '{"API-KEY":"XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX","Content-Type":"application/json"}';
$phpHeaders = json_decode($jsonHeaders, true);
$jsonBody = '{"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"}';
$phpBody = json_decode($jsonBody, true);
try {
$client = new Client();
$response = $client->post('https://api.pdfbolt.com/v1/async', [
'headers' => $phpHeaders,
'json' => $phpBody
]);
$result = json_decode($response->getBody(), true);
echo "Request ID: " . $result['requestId'] . "\n";
echo "PDF will be sent to webhook when ready\n";
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}
?>
3. What's next?
Discover how PDFBolt can enhance your workflows. Begin with the API Endpoints and explore the Conversion Parameters to customize your setup and meet your specific needs.